2012-04-17 27 views
4

私はmatplotlibの中でクリッピング使用して円や楕円のような形をクリップしようとするんだけど、私が行方不明です何かがなければならない。.. しないのはなぜこのクリップ私は半分?:Matplotlibでのクリッピング。なぜこれは機能しませんか?

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.patches import Circle 
from matplotlib.transforms import Bbox 

clip_box = Bbox(((-2,-2),(2,0))) 
circle = Circle((0,0),1,clip_box=clip_box,clip_on=True) 

plt.axes().add_artist(circle) 
plt.axis('equal') 
plt.axis((-2,2,-2,2)) 
plt.show() 

答えて

5

の円あなたのコードがうまくいかない理由は分かりませんが、次のスニペットは期待通りに機能します。

私undestandingから、clip_onは、表示領域にクリップする必要がある形状ではなく形状天気を与えられたクリッピングを適用するとは関係ありません。

import matplotlib.pyplot as plt 
from matplotlib.patches import Circle, Rectangle 

rect = Rectangle((-2,-2),4,2, facecolor="none", edgecolor="none") 
circle = Circle((0,0),1) 

plt.axes().add_artist(rect) 
plt.axes().add_artist(circle) 

circle.set_clip_path(rect) 

plt.axis('equal') 
plt.axis((-2,2,-2,2)) 
plt.show() 
+0

私は対話的に完全に長方形内に含まれる領域にズームするときに、この溶液を用いて、円が消えていることに気づきます。この動作を削除する方法はありますか? – Eskil

+0

@Eskilそれは私のために働いています(Linux、Matplotlib 0.99.3):円はまだ表示されますが、数字の表示領域をオーバーシュートします... – FabienAndre

+0

Linux、Matplotlib 1.0.1 here。私は、ボックス内の完全な領域にズームしたときに、円がある場合があることがわかります。ただし、青い部分の中央の小さな領域にズームしてみてください。私のために青は消えてしまい、私のアプリケーションではやや迷惑になります。 – Eskil

0

私はこれで苦労してきたので、ここで私のバージョンです:(?!)

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib.patches import Circle 
from matplotlib.transforms import Bbox 



# This is in PIXELS 
# first tuple : coords of box' bottom-left corner, from the figure's bottom-left corner 
# second tuple : coords of box' top-right corner, from the figure's bottom-left corner 
clip_box = Bbox(((0,0),(300,300))) 
circle = Circle((0,0),1) 

plt.axis('equal') 
plt.axis((-2,2,-2,2)) 
plt.axes().add_artist(circle) 

# You have to call this after add_artist() 
circle.set_clip_box(clip_box) 

plt.show() 

2つの違いは、ボックス」coordsのはピクセルであることであり、それはset_clip_box()後にのみ動作しますadd_artists()(これが理由でclip_box=clip_boxは機能しません)。私は、これを "軸単位"で動作させるために何が構成されるべきかを知りたい。

私はこれをトラブルシューティングするために使用したハックです。このクリッププロットを含むすべて、軸、等:

for o in plt.findobj(): 
    o.set_clip_on(True) 
    o.set_clip_box(clip_box) 
関連する問題