代码中的注释包括使用np.linespace限制的建议修复程序。尽管问题“ contourf()在有限数据上绘制空白”给出了以下给出的答案,但仍不能解决问题:如果要确保包括所有数据,则可以定义自己的级别使用
plt.contourf(x,y,Z,np.linspace(Z.min(),Z.max(),100)) 提供的解决方案被认为可以正常工作,然后在较旧的帖子中却找不到。该声明是指出问题未被发现或无法重复,因此从未给出答案。此处的代码在pi和win7平台上均重现了该问题,并且是可重复的。多余的空白区域似乎与平行的轮廓线相关联,这些轮廓线不会在图像中循环。注意代码中的注释,其中可以修改xc来更改数据的形状。
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import griddata
fig = plt.figure(figsize=(16,12)) #fill the screen
fig.canvas.set_window_title('<Test>')
ax = fig.gca() # to work in 2d contour
x=[ 274.0, 3174.6, 6075.2, 8975.8, 11876.4, 14777.0, 14777.0 , 11876.4, 8975.8,
6075.2, 3174.6, 274.0, 274.0, 3174.6, 6075.2, 8975.8, 11876.4, 14777.0,
14777.0, 11876.4, 8975.8, 6075.2, 3174.6, 274.0, 274.0, 3174.6, 6075.2,
8975.8, 11876.4, 14777.0, 14777.0, 11876.4, 8975.8, 6075.2, 3174.6, 274.0 ]
y=[ 6737.2, 6737.2, 6737.2, 6737.2, 6737.2, 6737.2, 9907.4, 9907.4, 9907.4,
9907.4, 9907.4, 9907.4, 13077.6, 13077.6,13077.6, 13077.6, 13077.6, 13077.6,
16247.6, 16247.6, 16247.6, 16247.6, 16247.6, 16247.6, 19418.0, 19418.0, 19418.0,
19418.0, 19418.0, 19418.0, 22588.2, 22588.2, 22588.2, 22588.2, 22588.2, 22588.2]
z=[154.11000061, 142.88999939, 137.19000244, 137.5, 143.42999268,
155.47000122, 140.53999329, 126.16000366, 118.51999664, 118.43000031,
125.22000122, 138.96000671, 131.03999329, 116.23999786, 108.23999786,
108.90000153, 116.66999817, 132.6000061, 132.75999451, 117.56999969,
111.65000153, 109.80000305, 117.29000092, 132.11000061, 141.44000244,
127.08000183, 120.48000336, 120.58999634, 127.70999908, 141.05999756,
156.22999573, 145.16000366, 139.33999634, 139.27999878, 145.63000488,
157.00999451]
print(z)
xmax=(np.amax(x))
xmin=(np.amin(x))
ymax=(np.amax(y))
ymin=(np.amin(y))
zmax=(np.amax(z))
zmin=(np.amin(z))
xc=1 #change this from -40 to 1 to 40
yc=xc
Zheight=zmin
if xc==0:
xc=.001
if yc==0:
yc=.001
xcurv=int(1000000/xc)
ycurv=int(1000000/yc)
z_surf = ((((x-(xmax+xmin)/2)/10)*((x-(xmax+xmin)/2)/10))/-xcurv + (((y-(ymax+ymin)/2)/10*(y-(ymax+ymin)/2)/10))/-ycurv ) +Zheight
zcorr=z-z_surf
zcorrmin=(np.amin(zcorr))
zcorrmax=(np.amax(zcorr))
X,Y= np.meshgrid(x,y)
Z = griddata((x, y), zcorr, (X, Y),method='nearest')
print("Zmin=",zmin,"Zmax=",zmax)
print("Zcorrmin=",zcorrmin,"Zcorrmax=",zcorrmax)
#im=ax.contourf(X, Y, Z, 15, alpha=.75, cmap = 'rainbow') #white areas in contour map
im=ax.contourf(X, Y, Z, np.linspace(Z.min(), Z.max(), 15), alpha=.75, cmap = 'rainbow') #supposed to fix white space but doesn't (3d surface and wireframe work fine with this data)
C = plt.contour(X, Y, Z, 15, colors='black')
plt.clabel(C, inline=1, fontsize=10)
v = np.linspace(zcorrmin, zcorrmax, 15, endpoint=True)
fig.colorbar(im,ax=ax,ticks=v)
plt.xticks(())
plt.yticks(())
plt.show()
答案 0 :(得分:0)
问题是网格太粗糙。图中的某些部分不会出现轮廓。
解决方案是将数据插值到更细的网格上。
import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import griddata
fig = plt.figure(figsize=(16,12)) #fill the screen
fig.canvas.set_window_title('<Test>')
ax = fig.gca() # to work in 2d contour
x=[ 274.0, 3174.6, 6075.2, 8975.8, 11876.4, 14777.0, 14777.0 , 11876.4, 8975.8,
6075.2, 3174.6, 274.0, 274.0, 3174.6, 6075.2, 8975.8, 11876.4, 14777.0,
14777.0, 11876.4, 8975.8, 6075.2, 3174.6, 274.0, 274.0, 3174.6, 6075.2,
8975.8, 11876.4, 14777.0, 14777.0, 11876.4, 8975.8, 6075.2, 3174.6, 274.0 ]
y=[ 6737.2, 6737.2, 6737.2, 6737.2, 6737.2, 6737.2, 9907.4, 9907.4, 9907.4,
9907.4, 9907.4, 9907.4, 13077.6, 13077.6,13077.6, 13077.6, 13077.6, 13077.6,
16247.6, 16247.6, 16247.6, 16247.6, 16247.6, 16247.6, 19418.0, 19418.0, 19418.0,
19418.0, 19418.0, 19418.0, 22588.2, 22588.2, 22588.2, 22588.2, 22588.2, 22588.2]
z=[154.11000061, 142.88999939, 137.19000244, 137.5, 143.42999268,
155.47000122, 140.53999329, 126.16000366, 118.51999664, 118.43000031,
125.22000122, 138.96000671, 131.03999329, 116.23999786, 108.23999786,
108.90000153, 116.66999817, 132.6000061, 132.75999451, 117.56999969,
111.65000153, 109.80000305, 117.29000092, 132.11000061, 141.44000244,
127.08000183, 120.48000336, 120.58999634, 127.70999908, 141.05999756,
156.22999573, 145.16000366, 139.33999634, 139.27999878, 145.63000488,
157.00999451]
xmax=(np.amax(x))
xmin=(np.amin(x))
ymax=(np.amax(y))
ymin=(np.amin(y))
zmax=(np.amax(z))
zmin=(np.amin(z))
z_surf = ((((x-(xmax+xmin)/2)/10)*((x-(xmax+xmin)/2)/10))/-1e6 + \
(((y-(ymax+ymin)/2)/10*(y-(ymax+ymin)/2)/10))/-1e6 ) + zmin
zcorr=z-z_surf
X,Y= np.meshgrid(np.linspace(xmin, xmax, 51),np.linspace(ymin, ymax, 51))
Z = griddata((x, y), zcorr, (X, Y),method='linear')
im=ax.contourf(X, Y, Z, 15, alpha=.75, cmap = 'rainbow')
C = ax.contour(X, Y, Z, 15, colors='black')
ax.clabel(C, inline=1, fontsize=10)
fig.colorbar(im,ax=ax)
ax.scatter(x,y,c="k", label="original grid")
ax.scatter(X,Y,c="crimson", s=4, label="fine grid")
ax.legend(framealpha=1)
plt.xticks(())
plt.yticks(())
plt.show()
在这里,我用黑点标记原始网格,并用红点标记精细的网格。插值仍然是线性的。但是对于更平滑的曲线,您可以使用method='cubic'
。