Fork me on GitHub

plotly-express-10-plotly实现饼图

本文中介绍的是如何利用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
2
3
4
5
6
7
8
9
import pandas as pd
import numpy as np

import plotly_express as px
import plotly.graph_objects as go

import dash
import dash_core_components as dcc
import dash_html_components as html

基于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
2
3
4
df = px.data.gapminder().query("year == 2007").query("continent == 'Europe'")
df.loc[df["pop"] < 2.e6, "country"] = "Other countries" # 将满足条件的全部改为Other countries
fig = px.pie(df, values="pop", names="country", title="Population of Eurpean contient")
fig.show()

基于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
2
3
4
5
labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']
values = [4500, 2500, 1053, 500]

fig = go.Figure(data=go.Pie(labels=labels,values=values))
fig.show()

update_traces

1
2
3
4
5
6
7
8
9
10
11
12
13
# 颜色选项
colors = ['gold', 'mediumturquoise', 'darkorange', 'lightgreen']

# 绘图
fig = go.Figure(data=go.Pie(labels=['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen'],
values=[4500,2500,1053,500]))

# 更新饼图配置
fig.update_traces(hoverinfo='label+percent', # 悬停信息
textinfo='value', # 饼图中显示的信息
textfont_size=20,
marker=dict(colors=colors, line=dict(color='#000000', width=2)))
fig.show()

文本隐藏

1
2
3
4
5
6
7
8
df = px.data.gapminder().query("continent == 'Asia'")
fig = px.pie(df, values='pop', names='country')

fig.update_traces(textposition='inside') # 文字信息在里面
fig.update_layout(uniformtext_minsize=15, # 文本信息的最小值
uniformtext_mode='hide' # 小于最小值则被隐藏
)
fig.show()

不进行隐藏的效果对比:

设置饼图颜色

通过序列形式

通过color_discrete_sequence=px.colors.sequential.Bluyl来实现

1
2
3
4
df = px.data.tips()
# 设置饼图的颜色:px.colors.sequential.Bluyl
fig = px.pie(df, values="tip", names="day",color_discrete_sequence=px.colors.sequential.Bluyl)
fig.show()

通过字典形式

1
2
3
4
5
6
7
# 通过字典的形式上色:这个颜色好看呀
fig = px.pie(df, values="tip",names="day",color="day",
color_discrete_map={'Thur':'lightcyan',
'Fri':'cyan',
'Sat':'royalblue',
'Sun':'darkblue'})
fig.show()

布局和属性设置

1
2
3
4
5
6
7
8
df = px.data.gapminder().query("year == 2007").query("continent == 'Americas'")   # 居然是Americas!!!!

fig = px.pie(df, values='pop', names='country',
title='Population of American continent',
hover_data=['lifeExp'], labels={'lifeExp':'life expectancy'})

fig.update_traces(textposition='inside', textinfo='percent+label') # 将文本的信息放在里面
fig.show()

修改参数之后的效果对比:

文本排列

文本信息如何在扇区中进行合理地排列,3种方式:

  • horizontal
  • radial
  • tangential

NWqdM9.png

NWquvj.png

NWqDVx.png

Donut & Pulling sectors

1
2
3
4
5
6
labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']
values = [4500, 2500, 1053, 500]

# Use `hole` to create a donut-like pie chart
fig = go.Figure(data=[go.Pie(labels=labels, values=values, hole=.3)]) # 通过hole参数实现中心的环
fig.show()

1
2
3
4
5
6
labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']
values = [4500, 2500, 1053, 500]

# pull is given as a fraction of the pie radius
fig = go.Figure(data=[go.Pie(labels=labels, values=values, pull=[0, 0, 0.2, 0])]) # 通过pull参数
fig.show()

本文标题:plotly-express-10-plotly实现饼图

发布时间:2020年06月29日 - 12:06

原始链接:http://www.renpeter.cn/2020/06/29/plotly-express-10-plotly%E5%AE%9E%E7%8E%B0%E9%A5%BC%E5%9B%BE.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

Coffee or Tea