কম্পিউটার

পাইথনে চার্টে কিংবদন্তি কীভাবে যুক্ত করবেন?


পরিচয়...

চার্টের মূল উদ্দেশ্য হল সহজে ডেটা বোঝা। "একটি ছবি হাজার শব্দের মূল্য" মানে এমন জটিল ধারণা যা শব্দে প্রকাশ করা যায় না তা একটি ছবি/চার্ট দ্বারা প্রকাশ করা যায়।

প্রচুর তথ্য সহ গ্রাফ আঁকার সময়, উপস্থাপিত ডেটা বোঝার উন্নতির জন্য একটি কিংবদন্তি প্রাসঙ্গিক তথ্য প্রদর্শন করতে খুশি হতে পারে।

কিভাবে করতে হবে..

ম্যাটপ্লটলিবে, কিংবদন্তিগুলি একাধিক উপায়ে উপস্থাপন করা যেতে পারে। পাঠককে গ্রাফে প্রদর্শিত তথ্য বুঝতে সাহায্য করার জন্য নির্দিষ্ট পয়েন্টের প্রতি দৃষ্টি আকর্ষণ করার জন্য টীকাও দরকারী।

1. পাইথন কমান্ড প্রম্পট খুলে ম্যাটপ্লটলিব ইনস্টল করুন এবং পিপ ইনস্টল ম্যাটপ্লটলিব ফায়ার করুন৷

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
IPhone_Sales = [Iphones for Year, Iphones, Galaxy, Pixel in units_sold]
Galaxy_Sales = [Galaxy for Year, Iphones, Galaxy, Pixel in units_sold]
Pixel_Sales = [Pixel for Year, Iphones, Galaxy, Pixel in units_sold]

# data prep - Labels
Years = [Year for Year, Iphones, Galaxy,Pixel in units_sold]

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

# set the width
Width = 0.2

4. প্রস্তুত করা ডেটা সহ একটি বার গ্রাফ তৈরি করা৷ প্রতিটি পণ্য বিক্রয় তার অবস্থান এবং বিক্রয় নির্দিষ্ট করে .bar-এ একটি কল পায়৷

xy এবং xytext অ্যাট্রিবিউট ব্যবহার করে টীকা যোগ করা হয়। ডেটার দিকে তাকালে, Google Pixel মোবাইল বিক্রি 50% কমে গেছে অর্থাৎ 2019 সালে বিক্রি হওয়া 10 মিলিয়ন ইউনিট থেকে 2022 সালে মাত্র 5 মিলিয়নে দাঁড়িয়েছে। তাই আমরা টেক্সট এবং টীকাটিকে শেষ বারে সেট করতে যাচ্ছি।

অবশেষে, আমরা কিংবদন্তি প্যারামিটার ব্যবহার করে কিংবদন্তি যোগ করব। ডিফল্টরূপে, matplotlib এমন একটি এলাকায় লেজেন্ডকে আঁকবে যেখানে ডেটার সর্বনিম্ন ওভারল্যাপ আছে।

উদাহরণ

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')

# Set X-axis as years
plt.xticks(Position, Years)

# Set the Y axis label
plt.xlabel('Yearly Sales')
plt.ylabel('Unit Sales In Millions')

# Set the annotation Use the xy and xytext to change the arrow
plt.annotate('50% Drop in Sales', xy=(4.2, 5), xytext=(5.0, 12),
horizontalalignment='center',
arrowprops=dict(facecolor='red', shrink=0.05))

# Set the legent
plt.legend(mobile, title='Manufacturers')

আউটপুট

<matplotlib.legend.Legend at 0x19826618400>

পাইথনে চার্টে কিংবদন্তি কীভাবে যুক্ত করবেন?

  • যদি আপনি মনে করেন যে চার্টের ভিতরে কিংবদন্তি যোগ করা গোলমাল, আপনি কিংবদন্তিটিকে বাইরে প্লট করতে bbox_to_anchor বিকল্পটি ব্যবহার করতে পারেন। bbox_to_anchor-এর (X, Y) অবস্থান রয়েছে, যেখানে 0 হল গ্রাফের নীচে-বাম কোণে এবং 1 হল উপরের-ডান কোণে৷

দ্রষ্টব্য: - লেজেন্ডটি যেখানে গ্রাফ শুরু এবং শেষ হয় সেখানে সামঞ্জস্য করতে .subplots_adjust ব্যবহার করুন৷

যেমন ডান =0.50 মান মানে এটি প্লটের ডানদিকে স্ক্রীনের 50% ছেড়ে যায়। বামের জন্য ডিফল্ট মান হল 0.125, মানে এটি বাম দিকে 12.5% ​​স্থান ছেড়ে দেয়।

আউটপুট

plt.legend(mobile, title='Manufacturers', bbox_to_anchor=(1, 0.8))
plt.subplots_adjust(right=1.2)

পাইথনে চার্টে কিংবদন্তি কীভাবে যুক্ত করবেন?

উদাহরণ

6.অবশেষে আসুন চিত্রটি সংরক্ষণ করি।

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
IPhone_Sales = [Iphones for Year, Iphones, Galaxy, Pixel in units_sold]
Galaxy_Sales = [Galaxy for Year, Iphones, Galaxy, Pixel in units_sold]
Pixel_Sales = [Pixel for Year, Iphones, Galaxy, Pixel in units_sold]

# data prep - Labels
Years = [Year for Year, Iphones, Galaxy,Pixel in units_sold]

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

# set the width
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')

# Set X-axis as years
plt.xticks(Position, Years)

# Set the Y axis label
plt.xlabel('Yearly Sales')
plt.ylabel('Unit Sales In Millions')

# Set the annotation Use the xy and xytext to change the arrow
plt.annotate('50% Drop in Sales', xy=(4.2, 5), xytext=(5.0, 12),
horizontalalignment='center',
arrowprops=dict(facecolor='red', shrink=0.05))

# Set the legent
plt.legend(mobile, title='Manufacturers')

plt.legend(mobile, title='Manufacturers')
plt.subplots_adjust(right=1.2)

# plt.show()
plt.savefig('MobileSales.png')

পাইথনে চার্টে কিংবদন্তি কীভাবে যুক্ত করবেন?


  1. পাইথনের একটি মডেলের সাথে নন-লিনিয়ার ডেটা কীভাবে ফিট হতে পারে?

  2. কিভাবে Openpyxl দিয়ে পাইথন ব্যবহার করে এক্সেলে চার্ট তৈরি করবেন?

  3. কিভাবে পাইথনে পাথ যোগ করবেন?

  4. রুবিতে ব্যতিক্রমগুলিতে প্রসঙ্গ ডেটা কীভাবে যুক্ত করবেন