परिचय
चार्ट का मुख्य उद्देश्य डेटा को आसानी से समझाना है। जब ग्राफ में बहुत सारी जानकारी हो, तो लीजेंड (दंतकथा) प्रासंगिक जानकारी प्रदर्शित करके समझ में सुधार करती है।
Matplotlib में लीजेंड जोड़ना
1. डेटा तैयार करें
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))
# डेटा विभाजित करें
iPhone_Sales = [iph for yr, iph, gal, pix in units_sold]
Galaxy_Sales = [gal for yr, iph, gal, pix in units_sold]
Pixel_Sales = [pix for yr, iph, gal, pix in units_sold]
Years = [yr for yr, iph, gal, pix in units_sold]
Position = list(range(len(units_sold)))
Width = 0.2
2. बार चार्ट और लीजेंड
plt.bar([p - Width for p in Position], iPhone_Sales, width=Width, color='green', label='iPhone')
plt.bar([p for p in Position], Galaxy_Sales, width=Width, color='blue', label='Galaxy')
plt.bar([p + Width for p in Position], Pixel_Sales, width=Width, color='yellow', label='Pixel')
plt.xticks(Position, Years)
plt.xlabel('वार्षिक बिक्री')
plt.ylabel('यूनिट बिक्री (मिलियन में)')
# एनोटेशन (विशिष्ट बिंदु हाइलाइट करें)
plt.annotate('50% बिक्री गिरावट', xy=(4.2, 5), xytext=(5.0, 12),
horizontalalignment='center',
arrowprops=dict(facecolor='red', shrink=0.05))
# लीजेंड जोड़ें
plt.legend(mobile, title='निर्माता')
# या plt.legend(title='निर्माता') अगर bar में label दिया हो
plt.show()
3. लीजेंड को चार्ट के बाहर रखना
plt.legend(mobile, title='निर्माता', bbox_to_anchor=(1, 0.8))
plt.subplots_adjust(right=0.75) # दाएं जगह छोड़ें
plt.show()
bbox_to_anchor=(x, y): (0,0) नीचे-बाएं, (1,1) ऊपर-दाएं।subplots_adjustसे मार्जिन समायोजित करें।
4. फाइल सेव करना
plt.savefig('MobileSales.png', bbox_inches='tight', dpi=300)
पूर्ण कोड
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))
iPhone_Sales = [iph for yr, iph, gal, pix in units_sold]
Galaxy_Sales = [gal for yr, iph, gal, pix in units_sold]
Pixel_Sales = [pix for yr, iph, gal, pix in units_sold]
Years = [yr for yr, iph, gal, pix in units_sold]
Position = list(range(len(units_sold)))
Width = 0.2
plt.bar([p - Width for p in Position], iPhone_Sales, width=Width, color='green', label='iPhone')
plt.bar([p for p in Position], Galaxy_Sales, width=Width, color='blue', label='Galaxy')
plt.bar([p + Width for p in Position], Pixel_Sales, width=Width, color='yellow', label='Pixel')
plt.xticks(Position, Years)
plt.xlabel('वर्ष')
plt.ylabel('यूनिट बिक्री (मिलियन)')
plt.annotate('50% बिक्री गिरावट', xy=(4.2, 5), xytext=(5.0, 12),
horizontalalignment='center', arrowprops=dict(facecolor='red', shrink=0.05))
plt.legend(title='निर्माता', bbox_to_anchor=(1.02, 1), loc='upper left')
plt.subplots_adjust(right=0.75)
plt.tight_layout()
plt.savefig('MobileSales.png', dpi=300)
plt.show()
लीजेंड अनुकूलन विकल्प
loc: स्थान ('best', 'upper right', 'lower left', आदि)ncol: स्तंभों की संख्याfontsize: फॉन्ट आकारframeon: फ्रेम दिखाएं/छिपाएंshadow: छायाfancybox: गोल कोने