python - شرح - مؤامرة مع دائرة pyplot
matplotlib شرح (4)
من المستغرب لم أجد وصفاً مستقيماً عن كيفية رسم دائرة باستخدام matplotlib.pyplot (من فضلك لا pylab) أخذ كمركز الإدخال (س ، ص) ونصف قطرها r. حاولت بعض المتغيرات من هذا:
import matplotlib.pyplot as plt
circle=plt.Circle((0,0),2)
# here must be something like circle.plot() or not?
plt.show()
... ولكن لا يزال لا يعمل.
إذا كنت تريد رسم مجموعة من الدوائر ، فقد ترغب في مشاهدة هذه المشاركة أو هذه الرسالة (أحدث قليلاً). عرضت الوظيفة وظيفة تسمى circles
.
تعمل الدالات الوظيفية مثل scatter
، لكن أحجام الدوائر المرسومة في وحدة البيانات.
إليك مثال على ذلك:
from pylab import *
figure(figsize=(8,8))
ax=subplot(aspect='equal')
#plot one circle (the biggest one on bottom-right)
circles(1, 0, 0.5, 'r', alpha=0.2, lw=5, edgecolor='b', transform=ax.transAxes)
#plot a set of circles (circles in diagonal)
a=arange(11)
out = circles(a, a, a*0.2, c=a, alpha=0.5, edgecolor='none')
colorbar(out)
xlim(0,10)
ylim(0,10)
إذا كنت تهدف إلى أن تحافظ "الدائرة" على نسبة عرض إلى ارتفاع بصرية 1 بغض النظر عن إحداثيات البيانات ، فيمكنك استخدام طريقة التشتت (). matplotlib.org/1.3.1/api/…
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [10, 20, 30, 40, 50]
r = [100, 80, 60, 40, 20] # in points, not data units
fig, ax = plt.subplots(1, 1)
ax.scatter(x, y, s=r)
fig.show()
توسيع الإجابة المقبولة لعنصر مشترك. خاصه:
عرض الدوائر في نسبة العرض إلى الارتفاع الطبيعية.
تمديد حدود المحاور تلقائيًا لتضمين الدوائر المرسومة حديثًا.
مثال مستقل بذاته:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
ax.add_patch(plt.Circle((0, 0), 0.2, color='r', alpha=0.5))
ax.add_patch(plt.Circle((1, 1), 0.5, color='#00ffff', alpha=0.5))
ax.add_artist(plt.Circle((1, 0), 0.5, color='#000033', alpha=0.5))
#Use adjustable='box-forced' to make the plot area square-shaped as well.
ax.set_aspect('equal', adjustable='datalim')
ax.plot() #Causes an autoscale update.
plt.show()
لاحظ الفرق بين ax.add_patch(..)
و ax.add_artist(..)
: من الاثنين ، فقط السابق يجعل الآلات autoscaling تأخذ الدائرة في الاعتبار (المرجع: discussion ) ، وذلك بعد تشغيل التعليمات البرمجية المذكورة أعلاه نحصل على:
انظر أيضا: set_aspect(..)
الوثائق .
#!/usr/bin/python
import matplotlib.pyplot as plt
import numpy as np
def xy(r,phi):
return r*np.cos(phi), r*np.sin(phi)
fig = plt.figure()
ax = fig.add_subplot(111,aspect='equal')
phis=np.arange(0,6.28,0.01)
r =1.
ax.plot( *xy(r,phis), c='r',ls='-' )
plt.show()
أو ، إذا كنت تفضل ذلك ، فراجع path
، http://matplotlib.sourceforge.net/users/path_tutorial.html