2015-09-18 78 views
3

我想在matplotlib/pyplot中的圖像文件上繪製透明輪廓圖。使用matplotlib在圖像文件上繪製半透明輪廓圖

這是我走到這一步......

我有一個600×600像素的正方形圖像文件test.png看起來像這樣:

enter image description here

我想繪製了這個等值線圖使用matplotlib和pyplot將圖像(圖像文件置於'下面'和等值線圖的半透明版本疊加在一起)。作爲獎勵,圖像將被自動縮放以適應當前的繪圖邊界。我的例子繪製腳本如下:

from matplotlib import pyplot 
from matplotlib.ticker import MultipleLocator, FormatStrFormatter 
from matplotlib.colors import BoundaryNorm 
from matplotlib.ticker import MaxNLocator 
from pylab import * 
import numpy as np 
import random 

# ----------------------------- # 

dx, dy = 500.0, 500.0 
y, x = np.mgrid[slice(-2500.0, 2500.0 + dy, dy),slice(-2500.0, 2500.0 + dx, dx)] 

z = [] 
for i in x: 
    z.append([]) 
    for j in y: 
     z[-1].append(random.uniform(80.0,100.0)) 

# ----------------------------- # 

plot_aspect = 1.2 
plot_height = 10.0 
plot_width = int(plot_height*plot_aspect) 

# ----------------------------- # 

pyplot.figure(figsize=(plot_width, plot_height), dpi=100) 
pyplot.subplots_adjust(left=0.10, right=1.00, top=0.90, bottom=0.06, hspace=0.30) 
subplot1 = pyplot.subplot(111) 

# ----------------------------- # 

cbar_max = 100.0 
cbar_min = 80.0 
cbar_step = 1.0 
cbar_num_colors = 200 
cbar_num_format = "%d" 

# ---------- 

levels = MaxNLocator(nbins=cbar_num_colors).tick_values(cbar_min, cbar_max) 
cmap = pyplot.get_cmap('jet') 
norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True) 
pp = pyplot.contourf(x,y,z,levels=levels,cmap=cmap) 
cbar = pyplot.colorbar(pp, orientation='vertical', ticks=np.arange(cbar_min, cbar_max+cbar_step, cbar_step), format=cbar_num_format) 
cbar.ax.set_ylabel('Color Scale [unit]', fontsize = 16, weight="bold") 

# ---------- 

CS = pyplot.contour(x,y,z, alpha=0.5) 

# ---------- 

majorLocator1 = MultipleLocator(500) 
majorFormatter1 = FormatStrFormatter('%d') 
minorLocator1 = MultipleLocator(250) 

subplot1.xaxis.set_major_locator(majorLocator1) 
subplot1.xaxis.set_major_formatter(majorFormatter1) 
subplot1.xaxis.set_minor_locator(minorLocator1) 

pyplot.xticks(fontsize = 16) 
pyplot.xlim(-2500.0,2500.0) 

# ---------- 

majorLocator2 = MultipleLocator(500) 
majorFormatter2 = FormatStrFormatter('%d') 
minorLocator2 = MultipleLocator(250) 

subplot1.yaxis.set_major_locator(majorLocator2) 
subplot1.yaxis.set_major_formatter(majorFormatter2) 
subplot1.yaxis.set_minor_locator(minorLocator2) 

pyplot.yticks(fontsize = 16) 
pyplot.ylim(-2500.0,2500.0) 

# ---------- 

subplot1.xaxis.grid() 
subplot1.yaxis.grid() 

# ---------- 

subplot1.axes.set_aspect('equal') 

# ---------- 

pyplot.suptitle('Main Title', fontsize = 24, weight="bold") 

# ---------- 

pyplot.xlabel('X [m]', fontsize=16, weight="bold") 
pyplot.ylabel('Y [m]', fontsize=16, weight="bold") 

# ---------- 

implot = subplot1.imshow(pyplot.imread('test.png') , interpolation='nearest', alpha=0.5) 

# ---------- 
pyplot.show() 
#pyplot.savefig("tmp.png", dpi=100) 
pyplot.close() 

...但我沒有得到我想要的結果,而不是...我只是看等高線圖的一部分。喜歡的東西:

enter image description here

我應該在我的代碼做的就是我想要的嗎?

回答

9

你基本上需要做兩件事情,在後臺設置你想要的圖像的範圍。如果你不這樣做,座標被認爲是像素座標,在這種情況下,x和y都是0到600。因此,調整你imshow命令:

implot = subplot1.imshow(pyplot.imread(r'test.png'), interpolation='nearest', 
         alpha=0.5, extent=[-2500.0,2500.0,-2500.0,2500.0]) 

如果你想自動圖像伸展到劇情的限制,你可以抓住與程度:

extent = subplot1.get_xlim()+ subplot1.get_ylim() 

,並傳遞給imshowextent=extent

由於它的背景圖像,將α設置爲0.5使其非常微弱,我將它設置爲1.0。其次,你設置了等高線的alpha值,但是你可能(或者特別)要設置filled contours的alpha值。而當您使用填充輪廓的alpha時,啓用抗鋸齒功能可減少僞像。因此,改變你的contourf命令:

pp = pyplot.contourf(x,y,z,levels=levels,cmap=cmap, alpha=.5, antialiased=True) 

既然你已經創建的插曲反對自己,我也勸告用它做的,而不是繪製pyplot接口,這對當前活動軸運行的。

所以:

subplot1.contourf() 
etc 

相反的:

pyplot.contourf() 

隨着上述兩個變化,我的結果是這樣的:

enter image description here