2012-06-08 75 views
9

残念ですが、matplotlib.pyplotで楕円をプロットする簡単な方法はありますか? matplotlib.pyplot.arrowに似たものがあることを期待していましたが、何も見つかりませんでした。matplotlib.pyplot(Python)でプロット楕円

matplotlib.patchesをdraw_artistなどで使用する唯一の方法はありますか?私は、より簡単な方法があることを願っていますが、ドキュメンテーションはあまり役に立ちません。

アドバイスありがとうございます!

答えて

7

matplotlib ellipse demoを見ましたか?ここではmatplotlib.patches.Ellipseを使用します。

+0

を標準のプロット方法に近いものを望んでいましたが、次にこれを見ていきます。ありがとう! – casper

+0

matplotlib.pyplotで何かを探していることに気がつきました。申し訳ありませんが、それに気付かなかったからです。 'matplotlib.pyplot' APIドキュメントを検索しても何も明らかにならないので、' matplotlib.patches.Ellipse'を使って暮らす必要があります。 – Chris

+0

ありがとう、それは私がしなければならないことです。私はpyplotにいくつかの基本的な形状プロット機能を含めることを期待していましたが、すべてを持つことはできません! – casper

10

matplotlib楕円デモは素晴らしいです。しかし、forループなしで自分のコードに実装することはできませんでした。私は軸の数字のエラーを取得していた。ここで私が代わりにしたのは、もちろんxyの中心は、私が楕円をプロットしたイメージに基づいてそれぞれの幅と高さを持つ私自身の座標です。

from matplotlib.patches import Ellipse 

plt.figure() 
ax = plt.gca() 

ellipse = Ellipse(xy=(157.18, 68.4705), width=0.036, height=0.012, 
         edgecolor='r', fc='None', lw=2) 
ax.add_patch(ellipse) 

このコードは、部分的にthis pageの最初のコードボックスに基づいています。 matplotlib.patches.Ellipseへのリンクについては、上記のChrisの回答をご覧ください。

0

patcheを使用したくない場合は、楕円のパラメトリック方程式を使用できます。x = u + a.cos(t);与えY = V + b.sin(T)

import numpy as np 
from matplotlib import pyplot as plt 
from math import pi 

u=1.  #x-position of the center 
v=0.5 #y-position of the center 
a=2.  #radius on the x-axis 
b=1.5 #radius on the y-axis 

t = np.linspace(0, 2*pi, 100) 
plt.plot(u+a*np.cos(t) , v+b*np.sin(t)) 
plt.grid(color='lightgray',linestyle='--') 
plt.show() 

x-oriented ellipse with parametric equation楕円は2次元回転行列のおかげで回転させることができる。

import numpy as np 
from matplotlib import pyplot as plt 
from math import pi, cos, sin 

u=1.  #x-position of the center 
v=0.5  #y-position of the center 
a=2.  #radius on the x-axis 
b=1.5  #radius on the y-axis 
t_rot=pi/4 #rotation angle 

t = np.linspace(0, 2*pi, 100) 
Ell = np.array([a*np.cos(t) , b*np.sin(t)]) 
    #u,v removed to keep the same center location 
R_rot = np.array([[cos(t_rot) , -sin(t_rot)],[sin(t_rot) , cos(t_rot)]]) 
    #2-D rotation matrix 

Ell_rot = np.zeros((2,Ell.shape[1])) 
for i in range(Ell.shape[1]): 
    Ell_rot[:,i] = np.dot(R_rot,Ell[:,i]) 

plt.plot(u+Ell[0,:] , v+Ell[1,:])  #initial ellipse 
plt.plot(u+Ell_rot[0,:] , v+Ell_rot[1,:],'darkorange') #rotated ellipse 
plt.grid(color='lightgray',linestyle='--') 
plt.show() 

戻り値:

rotated ellipse with parametric equation