2012-03-21 30 views
7

contourplを使用してmatplotlibに塗りつぶしプロットを作成しようとしています。プロットの下部にギザギザのパターンでデータが欠落しています。等高線プロットは、元のデータがマスクされている場所だけでなく、良好なデータの隣接が不十分であるため輪郭アルゴリズムがきれいに補間できないポケットでも空白になります。matplotlibのcontourfプロットの一部をマスキング

データセットを拡張して、これらのポケットに妥当な輪郭を作成する方法を知っています。しかし、私が拡張データをプロットすると、私はどこにでも輪郭を塗りつぶします。私は元のデータが黒または白で欠けていた地域を覆いたい。

前のスレッドでは、最初の画像をプロットして、それを隠す別の画像で覆うことで、画像に対してこれを行う方法を学びました。アナログは以下のコードスニペットですが、輪郭線では機能しません...拡張contourfプロットを隠すためにbad_data imshowを得ることはできません。出来ますか?私が間違っている場合は、私はあなたがこのような状況を持って理解し

おかげで、 イーライ

import matplotlib.pyplot as plt 
lev = [0.0,0.1,0.2,0.5,1.0,2.0,4.0,8.0,16.0,32.0]   
norml = colors.BoundaryNorm(lev, 256) 
# this is the contour plot, using extended_data so that the contours are plausibly extended 
cs = plt.contourf(x,z,extended_data,levels = lev, cmap = cm.RdBu_r,norm = norml) 
# now the attempt to cover it up -- but imshow will not cover up the original plot as it will with another image 
bad_data = np.ma.masked_where(~data.mask, data.mask, copy=True) 
plt.imshow(bad_data, interpolation='nearest', aspect = 'auto', cmap=cm.gray) 
plt.show() 
+0

私は地域を遮断するために)この記事以来、私は地域を特定し、塗りつぶしを(使用することができます実現することを追加する必要があります。緊急度は低いですが、これが可能かどうかを知ることはまだ価値があります。 –

+0

5×5の配列があり、インデックス[1、1]が見つからなかったとすると、どのように見えますか?インデックス[1、1]はマーカーとして描かれるだろうか? – pelson

+1

最小の実例を掲載できますか? – EnricoGiampieri

答えて

10

は私を修正:

import numpy as np 
import matplotlib.pyplot as plt 
# generate some data with np.nan values (the missing values) 
d = np.random.rand(10, 10) 
d[2, 2], d[3, 5] = np.nan, np.nan 
# and in your case you actually have masked values too: 
d = np.ma.array(d, mask=d < .2) 
# now all of the above is just for us to get some data with missing (np.nan) and 
# masked values 

contourfで上記をプロットすることにより、

plt.contourf(d) 
plt.show() 

入手方法:

マスクされた値(D < .2)もnp.nan値(ブランク)は表示されません

enter image description here

(D [2,2]、D [3,5])! matplotlibにマスクされた値だけが表示されないようにしたいとします。だから我々はこれを行うことができます:

# the following line is replaced by your interpolation routine for 
# removing np.nan values 
d[np.isnan(d)] = 1 
# then because we use the masked array only the masked values will still be masked 
# but the np.nan values which were replaced through the interpolation algorithm 
# will show up if we do the contourf plot 
plt.contourf(d) 
plt.show() 

enter image description here

私にはわからないがマスクされた配列を使用して、どのくらいの速この場合ですが、とにかくこれは私がそれを行うだろう方法です。

# make the background dark gray (call this before the contourf) 
plt.gca().patch.set_color('.25') 
plt.contourf(d) 
plt.show() 

へ:あなたの代わりに空白のスポット(聖霊降臨祭)の異なる色をしたい場合は、contourfは、実際にデータが存在しないもの、またはマスクされたデータをプロットしていないための下軸のパッチを着色する必要があります取得:

enter image description here

関連する問題