subplot(row, col, index) मेथड का उपयोग करके, हम एक फिगर को row*col भागों में विभाजित कर सकते हैं, और index पोजीशन पर फिगर प्लॉट कर सकते हैं। निम्नलिखित प्रोग्राम में, हम एक ही फिगर में दो डायग्राम बनाएंगे।
चरण
NumPy का उपयोग करके x, y1, y2 पॉइंट्स बनाना।
nrows = 1, ncols = 2, index = 1 के साथ, subplot() मेथड का उपयोग करके वर्तमान फिगर में सबप्लॉट जोड़ें।
plot() मेथड का उपयोग करके x और y1 पॉइंट्स का उपयोग करके लाइन प्लॉट करें।
plt.title(), plt.xlabel() और plt.ylabel() मेथड्स का उपयोग करके फिगर 1 के लिए शीर्षक, X और Y एक्सिस के लिए लेबल सेट करें।
nrows = 1, ncols = 2, index = 2 के साथ, subplot() मेथड का उपयोग करके वर्तमान फिगर में सबप्लॉट जोड़ें।
plot() मेथड का उपयोग करके x और y2 पॉइंट्स का उपयोग करके लाइन प्लॉट करें।
plt.title(), plt.xlabel() और plt.ylabel() मेथड्स का उपयोग करके फिगर 2 के लिए शीर्षक, X और Y एक्सिस के लिए लेबल सेट करें।
फिगर दिखाने के लिए plt.show() मेथड का उपयोग करें।
उदाहरण
from matplotlib import pyplot as plt
import numpy as np
xPoints = np.array([2, 4, 6, 8, 10, 12, 14, 16, 18, 20])
y1Points = np.array([12, 14, 16, 18, 10, 12, 14, 16, 18, 120])
y2Points = np.array([12, 7, 6, 5, 4, 3, 2, 2, 1, 12])
plt.subplot(1, 2, 1) # row 1, col 2 index 1
plt.plot(xPoints, y1Points)
plt.title("My first plot!")
plt.xlabel('X-axis ')
plt.ylabel('Y-axis ')
plt.subplot(1, 2, 2) # index 2
plt.plot(xPoints, y2Points)
plt.title("My second plot!")
plt.xlabel('X-axis ')
plt.ylabel('Y-axis ')
plt.show()आउटपुट
