Làm thế nào để bạn tạo một biểu đồ hình tròn trong python?

Mô-đun matplotlib có thể được sử dụng để tạo tất cả các loại sơ đồ và biểu đồ bằng Python. Biểu đồ hình tròn là một trong những biểu đồ nó có thể tạo, nhưng nó là một trong nhiều biểu đồ

khóa học liên quan. Trực quan hóa dữ liệu với Matplotlib và Python

Biểu đồ hình tròn Matplotlib

Đầu tiên nhập plt từ mô-đun matplotlib với dòng nhập matplotlib. pyplot dưới dạng plt
Sau đó, bạn có thể sử dụng phương pháp plt. pie[] để tạo một cốt truyện.

Đoạn mã dưới đây tạo biểu đồ hình tròn

import matplotlib.pyplot as plt

# Data to plot
labels = 'Python', 'C++', 'Ruby', 'Java'
sizes = [215, 130, 245, 210]
colors = ['gold', 'yellowgreen', 'lightcoral', 'lightskyblue']
explode = [0.1, 0, 0, 0] # explode 1st slice

# Plot
plt.pie[sizes, explode=explode, labels=labels, colors=colors,
autopct='%1.1f%%', shadow=True, startangle=140]

plt.axis['equal']
plt.show[]

Đoạn mã trên có đầu ra sau

trăn biểu đồ hình tròn

Bạn có thể xác định kích thước của nó, bộ phận nào sẽ phát nổ [khoảng cách từ trung tâm], nhãn nào nên có và màu nào nên có

Chúng tôi đã tạo một từ điển với nhiều tính năng khác nhau và chúng tôi đã chuyển nó qua pd. DataFrame để tạo tập dữ liệu

Bước 3 - Vẽ đồ thị Pie

Chúng tôi đã tạo một tính năng mới sẽ lưu trữ tổng của tất cả dữ liệu mà chúng tôi muốn tạo Pie Plot. df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df] Chúng tôi đã tạo một mảng mã màu và sử dụng nó để vẽ biểu đồ hình tròn. Chúng tôi đã vẽ biểu đồ Pie bằng Pli. pie bằng cách chuyển dữ liệu mà chúng tôi muốn vẽ đồ thị. colors = ["#E13F29", "#D69A80", "#D63B59", "#AE5552", "#CB5C3B", "#EB8076", "#96624E"] plt.pie[df["total_arrests"], labels=df["officer_name"], shadow=False, colors=colors, explode=[0, 0, 0, 0, 0.15], startangle=90, autopct="%1.1f%%"] Cuối cùng, chúng tôi in Biểu đồ hình tròn plt.axis["equal"] plt.tight_layout[]; plt.show[]

nhập khẩu cốt truyện. graph_objects khi đi từ cốt truyện. subplots import make_subplots tags = ['1st', '2nd', '3rd', '4th', '5th'] # Xác định bộ màu của tranh night_colors = ['rgb[56, 75, 126]', 'rgb[18 . 'miền'}, {'loại'. 'miền'}], [{'loại'. 'miền'}, {'loại'. 'miền'}]] fig = make_subplots[rows=2, cols=2, specs=specs] # Xác định biểu đồ tròn fig. add_trace[đi. Pie[nhãn=nhãn, giá trị=[38, 27, 18, 10, 7], name='Đêm đầy sao', marker_colors=night_colors], 1, 1] hình. add_trace[đi. Hình tròn[nhãn=nhãn, giá trị=[28, 26, 21, 15, 10], tên='Hoa hướng dương', marker_colors=màu hoa hướng dương], 1, 2] hình. add_trace[đi. Pie[nhãn=nhãn, giá trị=[38, 19, 16, 14, 13], name='Irises', marker_colors=irises_colors], 2, 1] hình. add_trace[đi. Pie[nhãn=nhãn, giá trị=[31, 24, 19, 18, 8], name='The Night Café', marker_colors=cafe_colors], 2, 2] # Điều chỉnh bố cục và thông tin di chuột hình. update_traces[hoverinfo='nhãn+phần trăm+tên', textinfo='none'] hình. update_layout title_text='Van Gogh. 5 màu nổi bật nhất được hiển thị theo tỷ lệ', layout_showlegend=False] fig = go. hình[fig] vả. buổi bieu diễn[]

startangle mặc định là 0, sẽ bắt đầu lát cắt "Ếch" trên trục x dương. Ví dụ này đặt startangle = 90 sao cho mọi thứ được xoay ngược chiều kim đồng hồ 90 độ và lát ếch bắt đầu trên trục y dương

matplotlib4= from0matplotlib7from2matplotlib9from2df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]31from2df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]33from2df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]35from2df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]37matplotlib3

 

df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]39

colors = ["#E13F29", "#D69A80", "#D63B59", "#AE5552", "#CB5C3B", "#EB8076", "#96624E"] plt.pie[df["total_arrests"], labels=df["officer_name"], shadow=False, colors=colors, explode=[0, 0, 0, 0, 0.15], startangle=90, autopct="%1.1f%%"]20= colors = ["#E13F29", "#D69A80", "#D63B59", "#AE5552", "#CB5C3B", "#EB8076", "#96624E"] plt.pie[df["total_arrests"], labels=df["officer_name"], shadow=False, colors=colors, explode=[0, 0, 0, 0, 0.15], startangle=90, autopct="%1.1f%%"]22=colors = ["#E13F29", "#D69A80", "#D63B59", "#AE5552", "#CB5C3B", "#EB8076", "#96624E"] plt.pie[df["total_arrests"], labels=df["officer_name"], shadow=False, colors=colors, explode=[0, 0, 0, 0, 0.15], startangle=90, autopct="%1.1f%%"]24colors = ["#E13F29", "#D69A80", "#D63B59", "#AE5552", "#CB5C3B", "#EB8076", "#96624E"] plt.pie[df["total_arrests"], labels=df["officer_name"], shadow=False, colors=colors, explode=[0, 0, 0, 0, 0.15], startangle=90, autopct="%1.1f%%"]25from2colors = ["#E13F29", "#D69A80", "#D63B59", "#AE5552", "#CB5C3B", "#EB8076", "#96624E"] plt.pie[df["total_arrests"], labels=df["officer_name"], shadow=False, colors=colors, explode=[0, 0, 0, 0, 0.15], startangle=90, autopct="%1.1f%%"]27colors = ["#E13F29", "#D69A80", "#D63B59", "#AE5552", "#CB5C3B", "#EB8076", "#96624E"] plt.pie[df["total_arrests"], labels=df["officer_name"], shadow=False, colors=colors, explode=[0, 0, 0, 0, 0.15], startangle=90, autopct="%1.1f%%"]28

colors = ["#E13F29", "#D69A80", "#D63B59", "#AE5552", "#CB5C3B", "#EB8076", "#96624E"] plt.pie[df["total_arrests"], labels=df["officer_name"], shadow=False, colors=colors, explode=[0, 0, 0, 0, 0.15], startangle=90, autopct="%1.1f%%"]29= df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]31

 

df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]32

df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]33

Đầu ra.
 

 

Tùy chỉnh biểu đồ hình tròn

Có thể tùy chỉnh biểu đồ hình tròn trên cơ sở một số khía cạnh. Thuộc tính startangle xoay biểu đồ theo các độ đã chỉ định theo hướng ngược chiều kim đồng hồ được thực hiện trên trục x của biểu đồ hình tròn. thuộc tính bóng chấp nhận giá trị boolean, nếu nó đúng thì bóng sẽ xuất hiện bên dưới vành bánh. Các nêm của chiếc bánh có thể được tùy chỉnh bằng cách sử dụng wedgeprop, lấy từ điển Python làm tham số với các cặp giá trị tên biểu thị các thuộc tính nêm như băng thông, màu sắc cạnh, v.v. Bằng cách đặt frame=True axis, khung được vẽ xung quanh biểu đồ hình tròn. autopct kiểm soát cách hiển thị tỷ lệ phần trăm trên các nêm. Hãy để chúng tôi cố gắng sửa đổi cốt truyện trên.
Ví dụ 1.
 

Python3




plt.axis["equal"] plt.tight_layout[]; plt.show[]6

df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]3 plt.axis["equal"] plt.tight_layout[]; plt.show[]8

df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]3 df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]38

 

 

startangle1

startangle2= from0from1from2from3from2from5from2

from7from8______82matplotlib0from2matplotlib2matplotlib3

 

matplotlib4= from0matplotlib7from2matplotlib9from2df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]31from2df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]33from2df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]35from2df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]37matplotlib3

 

 

=1

=2__= colors = ["#E13F29", "#D69A80", "#D63B59", "#AE5552", "#CB5C3B", "#EB8076", "#96624E"] plt.pie[df["total_arrests"], labels=df["officer_name"], shadow=False, colors=colors, explode=[0, 0, 0, 0, 0.15], startangle=90, autopct="%1.1f%%"]24=5from2=7from2=9from2from01from2=7from2=7from06

 

from07

from08= colors = ["#E13F29", "#D69A80", "#D63B59", "#AE5552", "#CB5C3B", "#EB8076", "#96624E"] plt.pie[df["total_arrests"], labels=df["officer_name"], shadow=False, colors=colors, explode=[0, 0, 0, 0, 0.15], startangle=90, autopct="%1.1f%%"]24from11from2from13from2from15from2

from17from18from2from20from2from22from06

 

from24

from25= from27from28 from29from30from2from32 from29from34 from35

 

from36

from37 from38

from39from40= from42from43from44 from45from46from47from48from49

from39from51 from52____853from54from55

 

df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]39

from57= from59=colors = ["#E13F29", "#D69A80", "#D63B59", "#AE5552", "#CB5C3B", "#EB8076", "#96624E"] plt.pie[df["total_arrests"], labels=df["officer_name"], shadow=False, colors=colors, explode=[0, 0, 0, 0, 0.15], startangle=90, autopct="%1.1f%%"]24colors = ["#E13F29", "#D69A80", "#D63B59", "#AE5552", "#CB5C3B", "#EB8076", "#96624E"] plt.pie[df["total_arrests"], labels=df["officer_name"], shadow=False, colors=colors, explode=[0, 0, 0, 0, 0.15], startangle=90, autopct="%1.1f%%"]25from2colors = ["#E13F29", "#D69A80", "#D63B59", "#AE5552", "#CB5C3B", "#EB8076", "#96624E"] plt.pie[df["total_arrests"], labels=df["officer_name"], shadow=False, colors=colors, explode=[0, 0, 0, 0, 0.15], startangle=90, autopct="%1.1f%%"]27colors = ["#E13F29", "#D69A80", "#D63B59", "#AE5552", "#CB5C3B", "#EB8076", "#96624E"] plt.pie[df["total_arrests"], labels=df["officer_name"], shadow=False, colors=colors, explode=[0, 0, 0, 0, 0.15], startangle=90, autopct="%1.1f%%"]28

from66= from68

from69from70= from72 from73

from69=2= from77

from69from79______7 from81

from69from83= from85from2

from69from08= from90

from69from92= from94from2

from69from97= from99

from69matplotlib01= matplotlib03matplotlib04=matplotlib06colors = ["#E13F29", "#D69A80", "#D63B59", "#AE5552", "#CB5C3B", "#EB8076", "#96624E"] plt.pie[df["total_arrests"], labels=df["officer_name"], shadow=False, colors=colors, explode=[0, 0, 0, 0, 0.15], startangle=90, autopct="%1.1f%%"]28

 

matplotlib08

matplotlib09

from17matplotlib11=matplotlib13from2

from17matplotlib16=matplotlib18from2

from17matplotlib21=colors = ["#E13F29", "#D69A80", "#D63B59", "#AE5552", "#CB5C3B", "#EB8076", "#96624E"] plt.pie[df["total_arrests"], labels=df["officer_name"], shadow=False, colors=colors, explode=[0, 0, 0, 0, 0.15], startangle=90, autopct="%1.1f%%"]24from30from2matplotlib26from2matplotlib28from2from30colors = ["#E13F29", "#D69A80", "#D63B59", "#AE5552", "#CB5C3B", "#EB8076", "#96624E"] plt.pie[df["total_arrests"], labels=df["officer_name"], shadow=False, colors=colors, explode=[0, 0, 0, 0, 0.15], startangle=90, autopct="%1.1f%%"]28

 

matplotlib32= matplotlib34matplotlib35=matplotlib37from06

matplotlib39matplotlib40from06

 

df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]32

df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]33

Đầu ra.
 

Ví dụ 2. Tạo biểu đồ hình tròn lồng nhau
 

Python3




plt.axis["equal"] plt.tight_layout[]; plt.show[]6

from matplotlibdf["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]3 colors = ["#E13F29", "#D69A80", "#D63B59", "#AE5552", "#CB5C3B", "#EB8076", "#96624E"] plt.pie[df["total_arrests"], labels=df["officer_name"], shadow=False, colors=colors, explode=[0, 0, 0, 0, 0.15], startangle=90, autopct="%1.1f%%"]2

df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]3 plt.axis["equal"] plt.tight_layout[]; plt.show[]8

 

 

startangle1

matplotlib52= matplotlib54

startangle2= from0from1from2from3from2from5from2

from7from8______82matplotlib0from2matplotlib2matplotlib3

 

matplotlib4= matplotlib73matplotlib7from2matplotlib76matplotlib77matplotlib9from2matplotlib7matplotlib81

matplotlib82______80df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]31from2matplotlib86matplotlib77df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]33from2matplotlib90matplotlib81

matplotlib82______80df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]35from2matplotlib96matplotlib77df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]37from2df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]300df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]301

 

df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]302

df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]303= matplotlib4from44 from47from48df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]309from46df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]311 from46 df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]313

 

df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]314

df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]315= df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]317matplotlib26from2

df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]320df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]321____1322from30df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]324

 

df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]325

df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]326= df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]328df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]329from06

df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]331= df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]333____654from06from46df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]337from06

df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]339= df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]341from30from2df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]311from2df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]346from2matplotlib54from2df["total_arrests"] = df["jan_arrests"] + df["feb_arrests"] + df["march_arrests"] print[df]350from2

Chủ Đề