2016-04-08 21 views
2

我正在使用一些氣象數據在底圖上繪製輪廓線。我之前完成的完整工作示例代碼在這裏是How to remove/omit smaller contour lines using matplotlib。一切正常,我不會抱怨等高線圖。然而,有一種特殊情況是我必須隱藏底圖上特定區域(不規則經緯圖& lon)上的所有輪廓線。如何隱藏底圖上特定區域的輪廓線/數據

我能想到的唯一可能的解決方案是在所需區域上繪製一個ploygon線並填充與Basemap相同的顏色。很多搜索後,我發現這個鏈接How to draw rectangles on a Basemap(下面的代碼)

from mpl_toolkits.basemap import Basemap 
import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.patches import Polygon 

def draw_screen_poly(lats, lons, m): 
    x, y = m(lons, lats) 
    xy = zip(x,y) 
    poly = Polygon(xy, facecolor='red', alpha=0.4) 
    plt.gca().add_patch(poly) 

lats = [ -30, 30, 30, -30 ] 
lons = [ -50, -50, 50, 50 ] 

m = Basemap(projection='sinu',lon_0=0) 
m.drawcoastlines() 
m.drawmapboundary() 
draw_screen_poly(lats, lons, m) 

plt.show() 

它似乎部分工作。但是,我想繪製一個不規則的區域。

任何解決方案表示讚賞。

編輯:1

我已經明白問題出在哪裏。似乎填充在多邊形區域內的任何顏色(facecolor)都不會隱藏下面的任何東西。總是隻有透明,不管alpha值是否使用。爲了說明這個問題,我裁剪了所有三個區域的圖像,即。輪廓,底圖區域和多邊形區域。多邊形區域填充了紅色,但您可以看到,等高線始終可見。我在上面的代碼中使用的特定行是: -

poly = Polygon(xy, facecolor='red', edgecolor='b') 

因此問題不在於上面的代碼。這似乎是多邊形填充的問題。但仍然沒有解決這個問題。由此產生的圖像(裁剪圖像)低於(見附件圖像低於我的第二個編輯): -

enter image description here

編輯2: 從這個http://matplotlib.1069221.n5.nabble.com/Clipping-a-plot-inside-a-polygon-td41950.html擁有礦山的類似要求,以線索,我可以刪除一些數據。但是,刪除的數據僅來自多邊形區域之外,而不是來自多邊形區域之內。這裏是我採取的線索代碼: -

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.patches import RegularPolygon 

data = np.arange(100).reshape(10, 10) 
fig = plt.figure() 
ax = fig.add_subplot(111) 
ax.contourf(data) 
poly = RegularPolygon([ 0.5, 0.5], 6, 0.4, fc='none', 
         ec='k', transform=ax.transAxes) 
for artist in ax.get_children(): 
    artist.set_clip_path(poly) 

現在我的問題是,什麼命令用於刪除多邊形區域內的數據?

+0

移交的多邊形頂點不起作用? – tacaswell

+0

對不起,延誤了。它在提供所有經緯度後運作。但是,我使用的底圖顏色是白色/無顏色。當我使用這個命令poly = Polygon(xy,facecolor ='white')'給出多邊形顏色爲'white'時,它不會隱藏它後面的數據/輪廓線。基本上'白色'顏色充當透明。代碼有效,但問題與現在的顏色有關。 –

+0

我用屏幕截圖更新了我的問題。 –

回答

2

沒有注意到有這方面的索賠,所以我可能只給出已提出的解決方案here。你可以用ZORDER鼓搗隱藏你的多邊形背後的東西:

import matplotlib 
import matplotlib.mlab as mlab 
import matplotlib.pyplot as plt 

matplotlib.rcParams['xtick.direction'] = 'out' 
matplotlib.rcParams['ytick.direction'] = 'out' 

delta = 0.025 
x = np.arange(-3.0, 3.0, delta) 
y = np.arange(-2.0, 2.0, delta) 
X, Y = np.meshgrid(x, y) 
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) 
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1) 
# difference of Gaussians 
Z = 10.0 * (Z2 - Z1) 


# Create a simple contour plot with labels using default colors. The 
# inline argument to clabel will control whether the labels are draw 
# over the line segments of the contour, removing the lines beneath 
# the label 
fig = plt.figure() 
ax = fig.add_subplot(111) 
CS = plt.contour(X, Y, Z,zorder=3) 
plt.clabel(CS, inline=1, fontsize=10) 
plt.title('Simplest default with labels') 

rect1 = matplotlib.patches.Rectangle((0,0), 2, 1, color='white',zorder=5) 

ax.add_patch(rect1) 

plt.show() 

,結果是:在拉特/經度

Hiding contour line with polygon

+0

我已經開始了一個新的線程,因爲這個線程沒有任何迴應。無論如何,我也接受這個答案。享受賞金;-))。 –