holto1
holto1

Reputation: 121

Python - using pcolor with panda dataframes

I have a dataframe of 4397L, 39L that looks like this:

timestamp             0    1    2    3    4    5    6    7    8    9  ...   \                                                            
2013-04-17 05:00:00   46   46   46   45   45   45   45   45   45   45 ...    
2013-04-17 05:10:00   46   46   45   45   45   45   45   45   45   45 ...    
2013-04-17 05:20:00   45   46   45   45   45   45   45   45   45   45 ...    
2013-04-17 05:30:00   45   46   45   45   45   45   45   45   45   45 ...

and when I plot a pcolor using:

ax.pcolor(df) 

a plot is produced of the data.

I however would like the time (df.index) to be labeled along the x axis and the y axis to be labelled with a new vector x:

[-13.73038435 -12.73038435 -12.73038435 -11.73038435 -11.73038435
 -10.73038435 -10.73038435  -9.73038435  -9.73038435  -8.73038435
  -8.73038435  -7.73038435  -7.73038435  -6.73038435  -6.73038435
  -5.73038435  -5.73038435  -4.73038435  -4.73038435  -3.73038435
  -3.73038435  -2.73038435  -2.73038435  -1.73038435  -1.73038435
  -0.73038435  -0.73038435   0.26961565   0.26961565   1.26961565
   1.26961565   2.26961565   2.26961565   3.26961565   3.26961565
   4.26961565   4.26961565   5.26961565   5.26961565]

In matlab it would simply be the case of:

ax.pcolor(df.index, x, df.transpose())

the sizes are:

df = (4397, 39)
x = (, 39L)
df.index = (4397L,)

However this same logic does not work in Python. The error given is:

AttributeError: 'DatetimeIndex' object has no attribute 'reshape'

Any ideas?

Upvotes: 2

Views: 3523

Answers (1)

Fabian Rost
Fabian Rost

Reputation: 2414

You need to generate a meshgrid from x and time. Here is an example, where I generate df first (the function x * time beeing arbitrarily chosen). And it also seems like you have to convert the DataFrame to an array before plotting.

time = sp.linspace(0, 10, 5)
x = sp.linspace(0, 1, 10)
TIME, X = sp.meshgrid(time, x)
df = pd.DataFrame(X * TIME).transpose()
df.index.name = 'time'
df.columns.name = 'x'

X, TIME = sp.meshgrid(x, time)
plt.pcolor(X, TIME, sp.array(df))

enter image description here

Upvotes: 1

Related Questions