Computer >> कंप्यूटर >  >> प्रोग्रामिंग >> Python

पायथन में एकाधिक ग्राफ़ को कैसे संयोजित करें

परिचय

Matplotlib एक ही ग्राफ में एक से अधिक प्लॉट जोड़ने की अनुमति देता है। इस ट्यूटोरियल में, मैं आपको दिखाऊंगा कि एक ही प्लॉट में दो अलग-अलग अक्षों पर डेटा कैसे प्रस्तुत किया जाता है।

इसे कैसे करें..

1.पायथन कमांड प्रॉम्प्ट को खोलकर और पाइप इंस्टाल मैटप्लोटलिब को फायर करके मैटप्लोटलिब स्थापित करें।

import matplotlib.pyplot as plt

2. प्रदर्शित होने वाला डेटा तैयार करें।

import matplotlib.pyplot as plt

# data prep (I made up data no accuracy in these stats)
mobile = ['Iphone','Galaxy','Pixel']

# Data for the mobile units sold for 4 Quaters in Million
units_sold = (('2016',12,8,6),
('2017',14,10,7),
('2018',16,12,8),
('2019',18,14,10),
('2020',20,16,5),)

3. प्रत्येक कंपनी कंपनी की मोबाइल इकाइयों के लिए डेटा को सरणियों में विभाजित करें।

# data prep - splitting the data
Years, IPhone_Sales, Galaxy_Sales, Pixel_Sales = zip(*units_sold)

# set the position
Position = list(range(len(units_sold)))

# set the width
Width = 0.2

4.पहला सबप्लॉट बनाएं।

plt.subplot(2, 1, 1)
<matplotlib.axes._subplots.AxesSubplot at 0x214185d4e50>

पायथन में एकाधिक ग्राफ़ को कैसे संयोजित करें

5.IPhone_Sales के बारे में जानकारी के साथ एक बार ग्राफ बनाएं।

Iphone = plt.bar(Position, IPhone_Sales,color='green')
plt.ylabel('IPhone Sales')
plt.xticks(Position, Years)


([<matplotlib.axis.XTick at 0x214186115e0>,
<matplotlib.axis.XTick at 0x21418611580>,
<matplotlib.axis.XTick at 0x2141861fc40>,
<matplotlib.axis.XTick at 0x21418654e20>,
<matplotlib.axis.XTick at 0x2141865f370>],
[Text(0, 0, '2016'),
Text(0, 0, '2017'),
Text(0, 0, '2018'),
Text(0, 0, '2019'),
Text(0, 0, '2020')])

पायथन में एकाधिक ग्राफ़ को कैसे संयोजित करें

6.अब सैमसंग गैलेक्सी की बिक्री के बारे में जानकारी जोड़ने के लिए एक और y अक्ष बनाएं।

plt.twinx()
Galaxy = plt.plot(Position, Galaxy_Sales, 'o-', color='blue')
plt.ylabel('Galaxy Sales')
plt.xticks(Position, Years)


([<matplotlib.axis.XTick at 0x214186b4c40>,
<matplotlib.axis.XTick at 0x214186b4c10>,
<matplotlib.axis.XTick at 0x21418682ac0>,
<matplotlib.axis.XTick at 0x214186dd8e0>,
<matplotlib.axis.XTick at 0x214186dddf0>],
[Text(0, 0, '2016'),
Text(0, 0, '2017'),
Text(0, 0, '2018'),
Text(0, 0, '2019'),
Text(0, 0, '2020')])

पायथन में एकाधिक ग्राफ़ को कैसे संयोजित करें

7. अब हम अंतिम Google पिक्सेल बिक्री की योजना बनाएंगे।

plt.subplot(2, 1, 2)
plt.plot(Position, Pixel_Sales, color='yellow')
plt.gca().set_ylim(ymin=0)
plt.xticks(Position, Years)


([<matplotlib.axis.XTick at 0x2141870f9a0>,
<matplotlib.axis.XTick at 0x2141870f580>,
<matplotlib.axis.XTick at 0x2141870a730>,
<matplotlib.axis.XTick at 0x2141873c9d0>,
<matplotlib.axis.XTick at 0x2141873cee0>],
[Text(0, 0, '2016'),
Text(0, 0, '2017'),
Text(0, 0, '2018'),
Text(0, 0, '2019'),
Text(0, 0, '2020')])


पायथन में एकाधिक ग्राफ़ को कैसे संयोजित करें

plt.show()

उदाहरण

8. इसे एक साथ रखना और चार्ट को सहेजना।

import matplotlib.pyplot as plt

# data prep (I made up data no accuracy in these stats)
mobile = ['Iphone','Galaxy','Pixel']

# Data for the mobile units sold for 4 Quaters in Million
units_sold = (('2016',12,8,6),
('2017',14,10,7),
('2018',16,12,8),
('2019',18,14,10),
('2020',20,16,5),)
# data prep - splitting the data
Years, IPhone_Sales, Galaxy_Sales, Pixel_Sales = zip(*units_sold)

# set the position
Position = list(range(len(units_sold)))

# set the width
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.show()
plt.savefig('CombiningGraphs.png', dpi=72)


पायथन में एकाधिक ग्राफ़ को कैसे संयोजित करें


  1. पायथन में हिस्टोग्राम को सामान्य कैसे करें?

    पायथन में हिस्टोग्राम को सामान्य करने के लिए, हम उपयोग कर सकते हैं hist() तरीका। सामान्यीकृत बार में, प्लॉट के नीचे का क्षेत्र 1 होना चाहिए। कदम संख्याओं की सूची बनाएं। घनत्व=सत्य . के साथ एक हिस्टोग्राम प्लॉट करें । आकृति प्रदर्शित करने के लिए, दिखाएँ () . का उपयोग करें विधि। उदाहरण imp

  1. पायथन में लॉग हिस्टोग्राम कैसे बनाएं?

    लॉग हिस्टोग्राम बनाने के लिए, हम उपयोग कर सकते हैं log=True इतिहास () . के तर्क में विधि। कदम संख्याओं की सूची बनाएं। घनत्व=सत्य . के साथ एक हिस्टोग्राम प्लॉट करें । आकृति प्रदर्शित करने के लिए, शो () . का उपयोग करें विधि। उदाहरण pltplt.rcParams[figure.figsize] =[7.50, 3.50]plt.rcParams

  1. पायथन में टाइम सीरीज़ कैसे प्लॉट करें?

    मैटप्लोटलिब का उपयोग करके पायथन में एक समय श्रृंखला तैयार करने के लिए, हम निम्नलिखित कदम उठा सकते हैं - numpy का उपयोग करके x और y अंक बनाएं। प्लॉट() . का उपयोग करके बनाए गए x और y बिंदुओं को प्लॉट करें विधि। आकृति प्रदर्शित करने के लिए, शो () . का उपयोग करें विधि। उदाहरण import matplot