المؤامرات الفرعية Matplotlib
عرض قطع متعددة
باستخدام subplots()
الوظيفة ، يمكنك رسم عدة قطع في شكل واحد:
مثال
ارسم 2 قطع أرض:
import matplotlib.pyplot as plt
import numpy as np
#plot 1:
x =
np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x,y)
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30,
40])
plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.show()
نتيجة:
وظيفة المؤامرات الفرعية ()
تأخذ الوظيفة subplots()
ثلاث حجج تصف تخطيط الشكل.
يتم تنظيم التخطيط في صفوف وأعمدة ، يتم تمثيلها بواسطة الوسيطة الأولى والثانية .
تمثل الوسيطة الثالثة فهرس المؤامرة الحالية.
plt.subplot(1, 2, 1)
#the figure has 1 row, 2 columns, and this plot is the first plot.
plt.subplot(1, 2, 2)
#the figure has 1 row, 2 columns, and this plot is the second plot.
لذلك ، إذا أردنا شكلًا به صفين في عمود واحد (بمعنى أنه سيتم عرض المخططين فوق بعضهما البعض بدلاً من عرضهما جنبًا إلى جنب) ، فيمكننا كتابة الصيغة على النحو التالي:
مثال
ارسم قطعتين فوق بعضهما البعض:
import matplotlib.pyplot as plt
import numpy as np
#plot 1:
x =
np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(2, 1, 1)
plt.plot(x,y)
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30,
40])
plt.subplot(2, 1, 2)
plt.plot(x,y)
plt.show()
نتيجة:
يمكنك رسم أي عدد تريده من المخططات على شكل واحد ، ما عليك سوى وصف عدد الصفوف والأعمدة وفهرس الحبكة.
مثال
ارسم 6 قطع:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([0,
1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(2, 3, 1)
plt.plot(x,y)
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30,
40])
plt.subplot(2, 3, 2)
plt.plot(x,y)
x = np.array([0, 1,
2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(2, 3, 3)
plt.plot(x,y)
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(2, 3, 4)
plt.plot(x,y)
x = np.array([0, 1, 2, 3])
y =
np.array([3, 8, 1, 10])
plt.subplot(2, 3, 5)
plt.plot(x,y)
x
= np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])
plt.subplot(2,
3, 6)
plt.plot(x,y)
plt.show()
نتيجة:
عنوان
يمكنك إضافة عنوان لكل قطعة أرض باستخدام title()
الوظيفة:
مثال
قطعتان بالعناوين:
import matplotlib.pyplot as plt
import numpy as np
#plot 1:
x =
np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x,y)
plt.title("SALES")
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30,
40])
plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.title("INCOME")
plt.show()
نتيجة:
عنوان سوبر
يمكنك إضافة عنوان إلى الشكل بأكمله باستخدام suptitle()
الوظيفة:
مثال
أضف عنوانًا للرسم بالكامل:
import matplotlib.pyplot as plt
import numpy as np
#plot 1:
x =
np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x,y)
plt.title("SALES")
#plot 2:
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30,
40])
plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.title("INCOME")
plt.suptitle("MY SHOP")
plt.show()
نتيجة: