本文中介绍的是如何利用px.pie和go.Pie来绘制饼图,以及在饼图中的相关参数设置。
A pie chart is a circular statistical chart, which is divided into sectors to illustrate numerical proportion.
- 基于px.pie
- 基于go.pie
导入库
1 | import pandas as pd |
基于px.pie实现
In px.pie
, data visualized by the sectors of the pie is set in values
. The sector labels are set in names
.
1 | df = px.data.gapminder().query("year == 2007").query("continent == 'Europe'") |
基于go.pie实现
basic
In go.Pie
, data visualized by the sectors of the pie is set in values
. The sector labels are set in labels
. The sector colors are set in marker.colors
.
- labels
- values
1 | labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen'] |
update_traces
1 | # 颜色选项 |
文本隐藏
1 | df = px.data.gapminder().query("continent == 'Asia'") |
不进行隐藏的效果对比:
设置饼图颜色
通过序列形式
通过color_discrete_sequence=px.colors.sequential.Bluyl
来实现
1 | df = px.data.tips() |
通过字典形式
1 | # 通过字典的形式上色:这个颜色好看呀 |
布局和属性设置
1 | df = px.data.gapminder().query("year == 2007").query("continent == 'Americas'") # 居然是Americas!!!! |
修改参数之后的效果对比:
文本排列
文本信息如何在扇区中进行合理地排列,3种方式:
- horizontal
- radial
- tangential
Donut & Pulling sectors
1 | labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen'] |
1 | labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen'] |