使用matplotlib绘制地形作为背景

5

这里是我的问题。

  • 地形是通过这些区域的海拔高度来表示的。我已经上传了这里

现在,我可以使用matplotlib中的contourf将地形绘制为背景。

fig =plt.figure(figsize=(10,8))
ax = plt.subplot()
xi,yi = np.linspace(195.2260,391.2260,50), np.linspace(4108.9341,4304.9341,50)


height = np.array(list(csv.reader(open("terr_grd.csv","rb"),delimiter=','))).astype('float')

terrf = plt.contourf(xi, yi, height,15, cmap=plt.cm.Blues)
terr = plt.contour(xi, yi, height, 15,
             colors='k',alpha=0.5)
plt.clabel(terr, fontsize=9, inline=1)

http://i13.tietuku.com/1d32bfe631e20eee.png

作为背景,它可能会影响覆盖图层(不同颜色混合在一起)。

我在一本书上找到的下面上传的图片是一个很好的可视化艺术品。

http://i11.tietuku.com/4d4178c16eb7aaf6.png

地形作为背景对于叠加的流域(绿色)没有任何影响。

因此,如何使用 matplotlib 绘制这种类似的阴影地形,我尝试了一些色图,但我无法获得类似的绘图。

更新

我已经尝试了下面代码中的 alpha 设置:

# Z is 2-d array represent the value of each grid    
CS = plt.contourf(xi,yi, Z,2, cmap=plt.cm.Greens,
              vmax=abs(Z).max(), vmin=abs(Z).min(),alpha=0.8,zorder = 3)

图像如下:

http://i11.tietuku.com/5bb0b4cb0102ec32.png

无论我使用何种数据,与流域面积无关。我致力于像我在此处发布的图2一样更加真实地描绘地形起伏。

1
你可以尝试创建一个带有 alpha 通道的自定义多边形补丁。 - Erotemic
谢谢您的回复。我已经了解了alpha设置。 - Han Zhengzu
1个回答

1

您只需要a)对齐两个图形b)为图形添加alpha关键字:

fig =plt.figure(figsize=(10,8))
ax = plt.subplot()
xi,yi = np.linspace(195.2260,391.2260,50), np.linspace(4108.9341,4304.9341,50)
## the image
img=mpimg.imread('4d4178c16eb7aaf6.png')

ax.imshow(img,
        extent=[min(xi), max(xi), min(yi), max(yi)])
## the rest


height = np.array(list(csv.reader(open("terr_grd.csv","tr"),delimiter=','))).astype('float')

terrf = ax.contourf(xi, yi, height,15, cmap=plt.cm.Blues,alpha=0.5)
terr = ax.contour(xi, yi, height, 15,
            colors='k',alpha=0.5)
ax.clabel(terr, fontsize=9, inline=1)



fig.savefig("theplot.png")

plt.show()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接