Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

การสร้าง Gauge Chart ด้วย Pygal ใน Python


บทนำ

Visualization ช่วยเข้าใจข้อมูลง่ายขึ้น Pygal: แพ็กเกจ Python โค้ดเปิด สร้างกราฟโต้ตอบและ SVG สามารถปรับแต่ง ดาวน์โหลดคุณภาพสูง ฝังในเว็บได้ สร้าง Bar, Histogram, Line ฯลฯ ได้ง่าย

Gauge Chart

ผสมระหว่าง Pie chart และ Donut chart ใช้แสดงข้อมูลที่ค่าเฉพาะจุด

การติดตั้ง

pip install Pygal

ตัวอย่าง: สร้าง Gauge Chart

import pygal
from pygal.style import Style

custom_style = Style(colors=('#E80080', '#404040', '#9BC850', '#E81190'))

gauge_chart = pygal.Gauge(height=400, width=300, style=custom_style, human_readable=True)
gauge_chart.title = "Gauge Plot"
gauge_chart.add("Label 1", [0.4])
gauge_chart.add("Label 2", [1.2])
gauge_chart.add("Label 3", [1.5])
gauge_chart.add("Label 3", [1.8])
gauge_chart.render_in_browser()

ผลลัพธ์

การสร้าง Gauge Chart ด้วย Pygal ใน Python

คำอธิบาย

  • นำเข้า pygal และ Style
  • สร้าง Gauge กำหนด height, width, style, human_readable
  • กำหนดสี custom style
  • ตั้งค่า title และเพิ่มข้อมูลแต่ละ label (ค่าเดียวต่อ label)
  • เรียก render_in_browser() แสดงผล