Seite 1 von 1
Pandas, meshgrid und contourf
Verfasst: Montag 9. März 2015, 19:17
von Lienz20013
Hi, ich versuch mit einem Panda DataFrame einen contourf Plot zu erstellen jedoch bekomme ich den Fehler:
Code: Alles auswählen
TypeError: ufunc multiply cannot use operands with types dtype('<M8[ns]') and dtype('int32')
und zwar gehe ich dabei wie folgt vor:
Code: Alles auswählen
x = data.index.values
In [27]: x
Out[27]:
array(['2014-11-18T12:35:00.000000000+0100',
'2014-11-18T12:36:00.000000000+0100',
'2014-11-18T12:37:00.000000000+0100'])
y = depth
In [28]: y
Out[28]:
array([ 0. , 0.5, 1. , 1.5, 2. , 2.5 ])
z = data.values[:,::-1].T
In [28]: z
Out[29]:
array([[ 11.8841629 , 12.17295074, 12.60788727, 10.25720978,
9.79065704, 9.86179352],
[ 1.89908528, 1.68134236, 3.22510457, 1.32907808,
1.25055635, 1.00592744],
[ 4.82364607, 4.84348249, 6.08075666, 4.99564314,
5.06781721, 4.50266218]])
xi,yi = np.meshgrid(x,y)
temp = plt.contourf(yi,xi,z, alpha = 0.7,cmap = cmap)
Das klappt nicht

Was mache ich falsch. Ich denke es hat was mit den Zeit-Angaben zu tun und das diese in Nanosekunde sind? Oder?
Re: Pandas, meshgrid und contourf
Verfasst: Dienstag 10. März 2015, 11:36
von Lienz20013
Kann keiner helfen?

Re: Pandas, meshgrid und contourf
Verfasst: Dienstag 10. März 2015, 11:45
von MagBen
Du hast zwei Fehler:
contourf kommt nicht mit einem Array mit Strings zurecht. Du musst die Zeitangaben in Float Werte umrechnen.
xi, yi und z müssen die gleiche shape haben
xi und yi haben aber (6, 3) und z hat (3, 6), das kriegst Du mit "z.T" geregelt.
Code: Alles auswählen
import numpy as np
import matplotlib.pyplot as plt
#x = np.array(['2014-11-18T12:35:00.000000000+0100',
# '2014-11-18T12:36:00.000000000+0100',
# '2014-11-18T12:37:00.000000000+0100'])
x = np.array([35,36,37])
y = np.array([ 0. , 0.5, 1. , 1.5, 2. , 2.5 ])
z = np.array([[ 11.8841629 , 12.17295074, 12.60788727, 10.25720978,
9.79065704, 9.86179352],
[ 1.89908528, 1.68134236, 3.22510457, 1.32907808,
1.25055635, 1.00592744],
[ 4.82364607, 4.84348249, 6.08075666, 4.99564314,
5.06781721, 4.50266218]])
xi,yi = np.meshgrid(x,y)
print z.shape, xi.shape, yi.shape # (3, 6) (6, 3) (6, 3)
plt.contourf(yi,xi,z.T, alpha = 0.7)
plt.show()
Re: Pandas, meshgrid und contourf
Verfasst: Dienstag 10. März 2015, 16:15
von Lienz20013
Danke das hat ganz gut geklappt aber jetzt habe ich ein weiteres Problem. Und zwar möchte ich cotoure und imshow in einem plotdarstellen. Also so eigentlich:
Code: Alles auswählen
import numpy as np
from pandas import *
from matplotlib import gridspec
fig2 = plt.figure()
fig2.set_size_inches(11.69,8.27)
gs = gridspec.GridSpec(1, 2, width_ratios=(10, 0.2))
gs.update(wspace = 0.1, hspace = 0.2)
ax2 = plt.subplot(gs[0,0])
cmap = plt.cm.spectral
cmap.set_over(color='w',alpha=10)
cmap.set_under(color='k',alpha=10)
#___________________________________________________________________________
#Data
x = np.array([ 1.41631050e+18, 1.41631056e+18, 1.41631062e+18])
x = x.astype(float)
y = np.array([ 0. , 0.5, 1. , 1.5, 2. , 2.5 ])
z = np.array([[ 11.8841629 , 12.17295074, 12.60788727, 10.25720978,
9.79065704, 9.86179352],
[ 1.89908528, 1.68134236, 3.22510457, 1.32907808,
1.25055635, 1.00592744],
[ 4.82364607, 4.84348249, 6.08075666, 4.99564314,
5.06781721, 4.50266218]])
#___________________________________________________________________________
#plot
ax2.imshow(z.T,cmap=cm.jet,vmin=2,vmax=13,aspect='auto',interpolation='gaussian')
xi,yi = np.meshgrid(x,y)
ax2.contour(yi,xi,z.T,50, alpha = 0.7,colors = 'k')
Einzeln klappts aber zusammen nicht.
Re: Pandas, meshgrid und contourf
Verfasst: Dienstag 10. März 2015, 17:19
von MagBen
Das geht so
Code: Alles auswählen
ax2.imshow(z.T,cmap=plt.cm.jet,vmin=2,vmax=13,aspect='auto',interpolation='gaussian')
ax2.contour(z.T,50, alpha = 0.7,colors = 'k')
oder so
Code: Alles auswählen
ax2.pcolorfast(x,y,z.T,cmap=plt.cm.jet,vmin=2,vmax=13)
ax2.contour(x,y,z.T,50, alpha = 0.7,colors = 'k')
Wobei beides bei Deinen Testdaten nicht besonders gut aussieht.
Es ist nicht schwer geeignete Testdaten für einen hübschen Plot zu erzeugen:
Code: Alles auswählen
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-1,1,100)
y = np.linspace(-1,1,30)
x_2d, y_2d = np.meshgrid(x, y)
z_2d = np.cos(np.pi * x_2d) * np.cos(np.pi * y_2d)
plt.figure()
ax = plt.gca()
ax.pcolorfast(x,y,z_2d)
ax.contour(x,y,z_2d,15, alpha = 0.7,colors = 'k')
plt.show()