数据集由包含dfList
pandas
的列表DataFrames
组成,每个DataFrame
由Y
列和相同的index
组成}列。我试图将所有DataFrames绘制为2D图,其中像素颜色代表Y
值。
需要的绘图风格示例
问题:然而,将scipy.interpolate.griddata
与matplotlib.pyplot.imshow
一起使用会产生一个空白的情节!可能是什么问题?
我添加了pickle.dump
dfList
的链接以重现问题。任何帮助表示赞赏!!
代码
import scipy
# Meshgrid
xgrid = dfList[0].index.tolist()
ygrid = np.linspace(266, 1, 532)
Xgrid, Ygrid = np.meshgrid(xgrid, ygrid)
# Points
xo = dfList[0].index.tolist()
yo = [266, 300, 350, 400, 450, 500, 532] # one for each DataFrame
points = [ [x, y] for y in yo for x in xo]
points = np.array(points)
# Values
values = []
for df in dfList:
values.extend(df['Y'].real)
# values = [ item for item in df['Y'].real for df in dfList] # faster way of collapsing list
values = np.array(values)
# Griddata
resampled = scipy.interpolate.griddata(points, values, (Xgrid, Ygrid), method='cubic')
plt.imshow(resampled.T, extent=[365,1099,266,532], origin='lower')
dfList
:泡菜转储
答案 0 :(得分:1)
为了使这个答案在某种程度上对其他人有用,请先在此处找到一般性解释。下面是一个更具体的问题解决方案。
np.meshgrid
时的一般解释,np.mgrid
与scipy.interpolate.griddata
。我在这里提供了一个示例,用于比较np.meshgrid
与np.mgrid
在使用scipy.interpolate.griddata
进行插值时的使用情况。一般来说,np.meshgrid
的回报是np.mgrid
对同一网格的转置回报。
import numpy as np; np.random.seed(0)
import scipy.interpolate
import matplotlib.pyplot as plt
# np. meshgrid
xgrid = np.arange(21)[::2]
ygrid = np.linspace(0,5,6)
Xgrid, Ygrid = np.meshgrid(xgrid, ygrid)
# np. mgrid
Xgrid2, Ygrid2 = np.mgrid[0:20:11j,0:5:6j]
# points for interpolation
points = np.random.rand(200, 2)
points[:,0] *= 20
points[:,1] *= 5
# values
f = lambda x,y: np.sin(x)+ y
values = f(points[:,0], points[:,1])
# initerpolation using grid defined with np.meshgrid
resampled = scipy.interpolate.griddata(points, values, (Xgrid2, Ygrid2), method='cubic')
# interpolation using grid defined with np.mgrid
resampled2 = scipy.interpolate.griddata(points, values, (Xgrid.T, Ygrid.T), method='cubic')
fig, (ax1, ax2, ax3) = plt.subplots(3,1)
kws = dict( extent=[-1,21,-0.5,5.5], vmin=-1, vmax=6, origin="lower")
ax1.set_title("function evaluated on grid")
ax1.imshow(f(Xgrid, Ygrid), **kws)
ax2.set_title("interpolation using grid defined with np.meshgrid")
ax2.imshow(resampled.T, **kws)
ax3.set_title("interpolation using grid defined with np.mgrid")
ax3.imshow(resampled2.T, **kws)
for ax in (ax1, ax2, ax3):
ax.set_yticks(range(6))
ax.set_xticks(range(21)[::2])
plt.tight_layout()
plt.show()
<小时/> 现在问题及其解决方案。
(可省略,因为更有经验的用户在提问时自己创建)
import numpy as np; np.random.seed(0)
import scipy.interpolate
import matplotlib.pyplot as plt
import pandas as pd
a = np.random.rand(532, 7)
dfList = [pd.DataFrame(a[:,i], columns=["Y"]) for i in range(7)]
# Meshgrid
xgrid = dfList[0].index.tolist()
ygrid = np.linspace(266, 1, 532)
Xgrid, Ygrid = np.meshgrid(xgrid, ygrid)
# Points
xo = dfList[0].index.tolist()
yo = [266, 300, 350, 400, 450, 500, 532] # one for each DataFrame
points = [ [x, y] for y in yo for x in xo]
points = np.array(points)
# Values
values = []
for df in dfList:
values.extend(df['Y'].real)
values = np.array(values)
# Griddata
resampled = scipy.interpolate.griddata(points, values, (Xgrid, Ygrid), method='cubic')
plt.imshow(resampled.T, extent=[365,1099,266,532], origin='lower')
plt.show()
创建
我们看到一个空白的情节,图像的左侧只有一小部分点,而我们希望整个图形填充一个形状为(266, 532)
的图像。
使用scipy.interpolate.griddata
我们需要将网格作为元组xi
提供给(Xgrid.T, Ygrid.T)
参数,其中网格是通过numpy.meshgrid
:Xgrid, Ygrid = np.meshgrid(xgrid, ygrid)
生成的。请注意,meshgrid
与numpy.mgrid
不同。
与样本点相比,meshgrid的点存在一些其他不一致性,因此我假设您希望插入266和532之间的值。
import numpy as np; np.random.seed(0)
import scipy.interpolate
import matplotlib.pyplot as plt
import pandas as pd
a = np.random.rand(532, 7)
dfList = [pd.DataFrame(a[:,i], columns=["Y"]) for i in range(7)]
# Meshgrid
xgrid = dfList[0].index.values
ygrid = np.arange(266,532)
Xgrid, Ygrid = np.meshgrid(xgrid, ygrid)
# Points
xo = dfList[0].index.tolist()
yo = [266, 300, 350, 400, 450, 500, 532] # one for each DataFrame
points = [ [x, y] for y in yo for x in xo]
points = np.array(points)
print points.shape
# Values
values = []
for df in dfList:
values.extend(df['Y'].real)
values = np.array(values)
# Griddata
resampled = scipy.interpolate.griddata(points, values, (Xgrid.T, Ygrid.T), method='cubic')
print resampled.T.shape
plt.imshow(resampled.T, extent=[365,1099,266,532], origin='lower') #,
plt.show()