1

I am working with pandas DataFrames and I would like to plot columns as colormaps.

The DataFrames that I am working with have the following dimensions:

  • powers = [21 rows x 7 columns]

  • Fit_ABs = [21 rows x 7 columns]

  • Taus_lin_inv = [1 row x 7 columns]

My code is:

import os
import time
import numpy as np
import pandas as pd
import pylab as plt
import scipy
import re
from scipy.optimize import curve_fit
from scipy import stats
from sklearn import preprocessing
from matplotlib import cm

colorsP= plt.cm.jet_r(np.linspace(0.1, 0.9, len(powers.columns)))
colorsF= plt.cm.Greys_r(np.linspace(0.0,0.8,len(powers.index)))

fig, axs = plt.subplots(1, 2)

for c in range(len(colorsP)):
    for row in range(len(powers.index)):
    axs[0].semilogy(powers.iloc[row,:], Taus_lin_inv.iloc[0,:], marker='*', markersize=10, linestyle='', color=colorsP[c])
    axs[0].semilogy(powers.iloc[row,:], Fit_ABs.iloc[row,:], linestyle='solid', color=colorsF[row])
    axs[0].set_xlabel(r'$\sqrt{Amplitude}$' + ' ' + r'[$\sqrt{V}$]')
    axs[0].set_ylabel(r'$1/\tau}$')
    axs[0].grid(True, which='both')
    axs[0].legend(powers, loc='best', fontsize=15)
plt.show()

Obtaining the next plot:

enter image description here

In the plot the horizontal points are the values of every column. I would like that all these sets of points change the color as jet. Also I would like that the legend is in agreement with the previous statement.

Thanks in advance

felixpradoh
  • 135
  • 7

1 Answers1

1

I'm sorry but I don't understand what you want re the legends, you said

I would like that the legend is in agreement with the previous statement.

but I hope that you could elaborate a little bit that concept, that to you is perfectly clear, I know…

In [93]: import matplotlib.pyplot as plt 
    ...: import numpy as np 
    ...:  
    ...: Taus_lin_inv = np.arange(1,8) 
    ...: powers = np.outer(np.arange(1,22),np.sqrt(Taus_lin_inv)) 
    ...: Fit_ABs = np.outer(np.ones(21), Taus_lin_inv)+np.random.rand(21, 7)*0.2-0.1 
    ...:  
    ...: for i in range(21): 
    ...:     plt.semilogy(powers[i], Fit_ABs[i], c=plt.cm.gray_r(0.6*(i/20)+0.2)) 
    ...:     plt.scatter(powers[i], Taus_lin_inv, c=plt.cm.jet(Taus_lin_inv/7))   

                                                              

enter image description here

gboffi
  • 22,939
  • 8
  • 54
  • 85
  • Thanks for your answer. What I meant is that in the legend is represented the colors and the header of my columns in the DataFrame 'powers'. I have seen that you have transformed the data into numpy arrays. Isn't there any way to do it with DataFrames directly? – felixpradoh Jul 29 '20 at 11:50
  • @FélixdelPradoHurtado I have not your data, I had to supply my own and Numpy is what I know best, the shape of the data arrays is the same so you shouldn't have too much trouble to use your "data frames". Re the label in the legends, you can explicitly label the individual curves, `plt.plot(x[i], y[i], label=xlabel[i])` where you use the pandish equivalent of `xlabel[i]` – gboffi Jul 29 '20 at 13:30
  • @FélixdelPradoHurtado I have to add that 21 labels in a single _standard_ legend is possibly too much, you have to explore the possibility of placing the [legend outside the plot](https://stackoverflow.com/a/4701285/2749397), using a multi column arrangement – gboffi Jul 29 '20 at 13:35