Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting terrain as background using matplotlib

Here is my question.

  • the terrain was represent by the altitude across these area. I uploaded here

Now, I can plot the terrain as background using contourf in matplotlib.

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

As the background, it may affect the overlay plot(different color mingle together).

The figure I found on a book which I upload below is a good visualization art.

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

The terrain as the background doesn't bother the overlayed drainage basin(green) at all.

So, how to plot this similar-kind of shady terrain using matplotlib, I tried some colormap, but I can't get an alike plotting.

Update

I have already tried the alpha setting with code below:

# 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)

The figure shows like this:

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

Regardless of the data I was using is different from the watershed area. What I'm committed to is to depict the topographic relief more realistic like fig.2 which I post here.

like image 832
Han Zhengzu Avatar asked Nov 09 '22 19:11

Han Zhengzu


1 Answers

You just needed to a) align both plots b) add the alpha keyword for plots:

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()
like image 187
Felipe Lema Avatar answered Nov 14 '22 23:11

Felipe Lema