परिचय
Matplotlib एक ही ग्राफ में एक से अधिक प्लॉट जोड़ने की अनुमति देता है। इस ट्यूटोरियल में, मैं आपको दिखाऊंगा कि डेटा को एक ही प्लॉट में, दो अलग-अलग अक्षों पर कैसे प्रस्तुत किया जाए।
कैसे करें
1. Matplotlib इंस्टॉल करें:
pip install matplotlib
2. प्रदर्शित करने के लिए डेटा तैयार करें:
import matplotlib.pyplot as plt
# डेटा तैयारी (मैंने डेटा बनाया है, इन आंकड़ों में कोई सटीकता नहीं)
mobile = ['Iphone', 'Galaxy', 'Pixel']
# 4 तिमाहियों में मिलियन यूनिट्स में मोबाइल यूनिट्स बिक्री का डेटा
units_sold = (
('2016', 12, 8, 6),
('2017', 14, 10, 7),
('2018', 16, 12, 8),
('2019', 18, 14, 10),
('2020', 20, 16, 5),
)
3. प्रत्येक कंपनी के मोबाइल यूनिट्स के लिए डेटा को एरे में विभाजित करें:
# डेटा तैयारी - डेटा विभाजित करना
Years, IPhone_Sales, Galaxy_Sales, Pixel_Sales = zip(*units_sold)
# पोजिशन सेट करें
Position = list(range(len(units_sold)))
# चौड़ाई सेट करें
Width = 0.2
4. पहला सबप्लॉट बनाएं:
plt.subplot(2, 1, 1)
5. IPhone बिक्री की जानकारी के साथ बार ग्राफ बनाएं:
Iphone = plt.bar(Position, IPhone_Sales, color='green')
plt.ylabel('IPhone Sales')
plt.xticks(Position, Years)
6. Samsung Galaxy बिक्री की जानकारी जोड़ने के लिए एक और y अक्ष बनाएं:
plt.twinx()
Galaxy = plt.plot(Position, Galaxy_Sales, 'o-', color='blue')
plt.ylabel('Galaxy Sales')
plt.xticks(Position, Years)
7. Google Pixel बिक्री प्लॉट करें:
plt.subplot(2, 1, 2)
plt.plot(Position, Pixel_Sales, color='yellow')
plt.gca().set_ylim(ymin=0)
plt.xticks(Position, Years)
8. सब कुछ एक साथ रखकर चार्ट सेव करें:
import matplotlib.pyplot as plt
# डेटा तैयारी
mobile = ['Iphone', 'Galaxy', 'Pixel']
units_sold = (
('2016', 12, 8, 6),
('2017', 14, 10, 7),
('2018', 16, 12, 8),
('2019', 18, 14, 10),
('2020', 20, 16, 5),
)
Years, IPhone_Sales, Galaxy_Sales, Pixel_Sales = zip(*units_sold)
Position = list(range(len(units_sold)))
Width = 0.2
plt.subplot(2, 1, 1)
Iphone = plt.bar(Position, IPhone_Sales, color='green')
plt.ylabel('IPhone Sales')
plt.xticks(Position, Years)
plt.twinx()
Galaxy = plt.plot(Position, Galaxy_Sales, 'o-', color='blue')
plt.ylabel('Galaxy Sales')
plt.xticks(Position, Years)
plt.subplot(2, 1, 2)
plt.plot(Position, Pixel_Sales, color='yellow')
plt.ylabel('Pixel Sales')
plt.gca().set_ylim(ymin=0)
plt.xticks(Position, Years)
plt.savefig('CombiningGraphs.png', dpi=72)