แนะนำตัว...
จุดประสงค์หลักของแผนภูมิคือการทำให้ข้อมูลเข้าใจได้ง่าย "ภาพหนึ่งภาพมีค่าหนึ่งพันคำ" หมายถึง ความคิดที่ซับซ้อนที่ไม่สามารถบรรยายออกมาเป็นคำพูดได้ สามารถถ่ายทอดด้วยภาพ/แผนภูมิเดียวได้
เมื่อวาดกราฟที่มีข้อมูลจำนวนมาก คำอธิบายอาจยินดีที่จะแสดงข้อมูลที่เกี่ยวข้องเพื่อปรับปรุงความเข้าใจในข้อมูลที่นำเสนอ
ทำอย่างไร..
ใน matplotlib สามารถนำเสนอตำนานได้หลายวิธี คำอธิบายประกอบเพื่อดึงดูดความสนใจไปยังจุดใดจุดหนึ่งก็มีประโยชน์เช่นกันในการช่วยให้ผู้อ่านเข้าใจข้อมูลที่แสดงบนกราฟ
1. ติดตั้ง matplotlib โดยเปิดพรอมต์คำสั่ง python และเริ่มทำงาน pip install matplotlib
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% จาก 10 ล้านเครื่องในปี 2019 เหลือเพียง 5 ล้านเครื่องในปี 2022 ดังนั้นเราจะตั้งค่าข้อความและคำอธิบายประกอบให้อยู่ในแถบสุดท้าย
สุดท้าย เราจะเพิ่มคำอธิบายแผนภูมิโดยใช้พารามิเตอร์คำอธิบาย โดยค่าเริ่มต้น 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 เพื่อปรับคำอธิบายที่กราฟเริ่มต้นและสิ้นสุด
เช่น. ค่า right=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')
