怎么使用Plotly的Pie類創(chuàng)建餅圖

小億
88
2024-05-17 19:31:17
欄目: 編程語言

要使用Plotly的Pie類創(chuàng)建餅圖,首先需要安裝Plotly庫。然后可以按照以下步驟創(chuàng)建餅圖:

  1. 導(dǎo)入所需的庫:
import plotly.graph_objects as go
  1. 創(chuàng)建餅圖數(shù)據(jù):
labels = ['A', 'B', 'C', 'D']
values = [30, 40, 20, 10]
  1. 使用Pie類創(chuàng)建餅圖對(duì)象,并指定數(shù)據(jù):
fig = go.Figure(data=[go.Pie(labels=labels, values=values)])
  1. 可以通過設(shè)置一些屬性來自定義餅圖,例如標(biāo)題、顏色、起始角度等:
fig.update_layout(title_text='Pie Chart')
fig.update_traces(marker=dict(colors=['red', 'blue', 'green', 'orange']), 
                  rotation=90)
  1. 最后顯示餅圖:
fig.show()

這樣就可以使用Plotly的Pie類創(chuàng)建并顯示餅圖了??梢愿鶕?jù)需要調(diào)整數(shù)據(jù)和屬性來自定義餅圖的樣式。

0