ভূমিকা
চার্টের মূল উদ্দেশ্য হলো ডেটা সহজে বোঝানো। "একটি ছবি হাজার কথা বলে" - জটিল ধারণা যা শব্দে ব্যক্ত করা যায় না, সেটি একটি ছবি/চার্ট দিয়ে বোঝানো যায়।
অনেক তথ্য সম্বলিত গ্রাফ আঁকলে লেজেন্ড যোগ করলে ডেটা বোঝা সহজ হয়। এনোটেশনও নির্দিষ্ট পয়েন্টে দৃষ্টি আকর্ষণ করতে সাহায্য করে।
কীভাবে করবেন
- Matplotlib ইনস্টল করুন:
pip install matplotlib - ডেটা প্রস্তুত করুন:
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') plt.bar([p for p in Position], Galaxy_Sales, width=Width, color='blue') plt.bar([p + Width for p in Position], Pixel_Sales, width=Width, color='yellow') plt.xticks(Position, Years) plt.xlabel('Yearly Sales') plt.ylabel('Unit Sales In Millions') # এনোটেশন (গুগল পিক্সেলের সেলস ৫০% কমে যাওয়া) plt.annotate('50% Drop in Sales', xy=(4.2, 5), xytext=(5.0, 12), horizontalalignment='center', arrowprops=dict(facecolor='red', shrink=0.05)) # লেজেন্ড plt.legend(mobile, title='Manufacturers') - লেজেন্ড চার্টের বাইরে রাখতে
bbox_to_anchorএবংsubplots_adjustব্যবহার করুন:plt.legend(mobile, title='Manufacturers', bbox_to_anchor=(1, 0.8)) plt.subplots_adjust(right=1.2) - ফিগার সেভ করুন:
plt.savefig('MobileSales.png')
সম্পূর্ণ কোড
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')
plt.bar([p for p in Position], Galaxy_Sales, width=Width, color='blue')
plt.bar([p + Width for p in Position], Pixel_Sales, width=Width, color='yellow')
plt.xticks(Position, Years)
plt.xlabel('Yearly Sales')
plt.ylabel('Unit Sales In Millions')
plt.annotate('50% Drop in Sales', xy=(4.2, 5), xytext=(5.0, 12),
horizontalalignment='center', arrowprops=dict(facecolor='red', shrink=0.05))
plt.legend(mobile, title='Manufacturers')
plt.subplots_adjust(right=1.2)
plt.savefig('MobileSales.png')