Fork me on GitHub

可视化神器Plotly玩转柱状图

可视化神器Plotly玩转柱状图

本文是可视化神器Plotly绘图的第5篇:重点讲解如何利用Plotly绘制柱状图。柱状图在可视化图中是出现频率非常高的一种图表,能够很直观地展现数据的大小分布情况,在自己的工作中也使用地十分频繁。本文将详细介绍如何制作柱状图水平柱状图

前4篇关于Plotly的文章分别是:

导入库

首先还是需要导入我们绘图需要的几个常用库

1
2
3
4
5
6
import pandas as pd
import numpy as np

# 两种接口
import plotly_express as px
import plotly.graph_objects as go

绘图的时候还是会基于两种方式来实现:

  • plotly_express:px
  • plotly.graph_objects:go

基于px实现柱状图

基础柱状图

模拟生成一份简单的绘图所需数据

1
2
3
4
5
6
7
df1 = pd.DataFrame({
"name": ["小明","小红","周明","周红","张三"],
"age": [20,28,18,25,36],
"score": ["150","170","160","168","154"]
})

df1

1
2
fig = px.bar(df1,x="name",y="age")
fig.show()

加上颜色参数color:

1
2
3
4
5
6
fig = px.bar(df1,
x="name",
y="age",
color="age" # 颜色参数
)
fig.show()

基于长表的分组柱状图

1
2
3
4
5
6
7
df2 = pd.DataFrame({
"姓名": ["小明","小红","张三","小明","小红","张三","小明","小红","张三"],
"科目":["语文","语文","语文","数学","数学","数学","英语","英语","英语"],
"得分": [58,78,84,90,71,90,64,84,69]
})

df2

1
2
3
4
5
fig = px.bar(df2,
x="姓名",
y="得分",
color="科目")
fig.show()

基于宽表的分组柱状图

宽表形式主要是指字段很多

1
2
3
4
5
6
7
8
df3 = pd.DataFrame({
"姓名": ["小明","小红","张三"],
"语文":[58,78,84],
"数学":[90,71,90],
"英语":[64,84,69]
})

df3

1
2
3
4
5
6
fig = px.bar(df3,
x="姓名",
y=["语文","数学","英语"],
title="学生成绩对比" # 添加标题
)
fig.show()

图表个性设置

1、改变图形的颜色:加上颜色参数

1
2
3
4
5
6
fig = px.bar(df1,
x="name",
y="age",
color="age" # 加上颜色参数
)
fig.show()

看下自带数据集中小费的例子:

1
2
3
4
5
6
7
fig = px.bar(df4,
x="sex",
y="total_bill",
color="smoker" # 参数
)

fig.show()

2、设置图表的悬停信息:hover_data:

1
2
3
4
5
6
7
fig = px.bar(df1,
x="name",
y="age",
color="age", # 加上颜色参数
hover_data = ['score'] # 悬停参数
)
fig.show()

3、x轴标签倾斜设置

1
2
3
4
5
6
7
8
9
10
information = pd.DataFrame({
"days":["2021年3月1日 星期一",
"2021年3月2日 星期二",
"2021年3月3日 星期三",
"2021年3月4日 星期四",
ß"2021年3月5日 星期五"],
"number":[400,700,300,500,800]
})

information

默认情况下是水平显示:

1
2
fig = px.bar(information,x="days",y="number")
fig.show() # 默认水平显示

如果我们加上参数width,height向右显示:

1
2
3
4
5
6
fig = px.bar(information,
x="days",
y="number",
width=600,
height=600)
fig.show() # 参数width,height向右显示

加上xaxis_tickangle,设置倾斜角度:

1
2
3
fig = px.bar(information,x="days",y="number")
fig.update_layout(xaxis_tickangle=-45) # 倾斜角度设置
fig.show() # 结果是向左倾斜

即使我们加上width=600,height=600还是向左倾斜的:

1
2
3
4
fig = px.bar(information,x="days",y="number",width=600,height=600)
fig.update_layout(xaxis_tickangle=-45) # 倾斜角度设置

fig.show() # 结果是向左倾斜

柱状图模式设置

柱状图显示模式主要是有4种:

  • stack:堆叠
  • group:分组
  • overlay:覆盖
  • relative:相对
1
2
3
4
5
6
7
8
fig = px.bar(df4,
x="sex",
y="total_bill",
color="smoker",
barmode="stack" # ['stack', 'group', 'overlay', 'relative']
)

fig.show()

4种不同模式下的显示区别:

多面柱状图

我们以自带的小费数据为例:

1
2
3
4
5
6
7
8
9
10
11
12
13
fig = px.bar(df4,   # 带绘图数据
x="sex", # x轴
y="total_bill", # y轴
color="smoker", # 颜色设置
barmode="group", # 柱状图4种模式之一
facet_row="time", # 行
facet_col="day", # 列
category_orders={
"day": ["Thur", "Fri", "Sat", "Sun"],
"time": ["Lunch", "Dinner"] # 分类顺序设置
}
)
fig.show()

需要注意每个不同的参数表示的含义

基于go实现柱状图

基础柱状图

自建的数据集:

1
2
3
4
5
6
7
8
9
fig = go.Figure(
data = (
go.Bar(x=df1["name"].tolist(), # x轴数据
y=df1["score"].tolist() # y轴数据
)
)
)

fig.show()

分组柱状图

也是可以选择4种不同的柱状图模式:

  • stack:堆叠
  • group:分组
  • overlay:覆盖
  • relative:相对
1
2
3
4
5
6
7
8
9
fig = go.Figure(data=[
go.Bar(name='小明', x=["语文","数学","英语"], y=[120, 104, 93]),
go.Bar(name='小红', x=["语文","数学","英语"], y=[101, 88, 109])
])

# 柱状图模式需要设置:4选1
fig.update_layout(barmode='group') # ['stack', 'group', 'overlay', 'relative']

fig.show()

设置文本悬停信息hovertext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
name = ['Product A', 'Product B', 'Product C']
number = [200, 500, 300]

fig = go.Figure(data=[go.Bar(x=name,
y=number,
hovertext=['20% 市场份额 ', '50% 市场份额', '30% 市场份额'])])

fig.update_traces(marker_color='rgb(158,202,225)', # marker颜色
marker_line_color='rgb(8,48,107)', # 线条颜色
marker_line_width=1.5, # 线宽
opacity=0.6) # 透明度

fig.update_layout(title_text='3种产品市场份额')

fig.show()

显示数据信息

显示的模式有4种:

  • auto:自动
  • inside:内部
  • outside:外部
  • none:无
1
2
3
4
5
6
7
8
9
10
11
12
name = ['Product A', 'Product B', 'Product C']
number = [200, 500, 300]

fig = go.Figure(data=[go.Bar(x=name,
y=number,
text=number, # 显示number的数据信息
textposition="auto" # ['inside', 'outside', 'auto', 'none']
)])

fig.update_layout(title_text='3种产品市场数量')

fig.show()

格式化文本显示模式

1
2
3
4
5
import plotly.express as px

gap = px.data.gapminder().query("continent == 'Europe' and year == 2007 and pop > 2.e6")

gap.head()

1
2
3
4
5
6
7
8
9
10
11
12
fig = px.bar(gap,
x='country',
y='pop',
text='lifeExp')

fig.update_traces(texttemplate='%{text:.2s}', # 显示的整数位数:示例为2位
textposition='outside') # 文本显示位置:['inside', 'outside', 'auto', 'none']

fig.update_layout(uniformtext_minsize=8, # 显示文本字体大小
uniformtext_mode='show') # 文本显示4种模式:[False, 'hide', 'show']

fig.show()

x轴标签倾斜

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import plotly.graph_objects as go

months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

# 生成画布对象
fig = go.Figure()


# 添加多个图形轨迹
fig.add_trace(go.Bar(
x=months,
y=[20, 14, 25, 16, 18, 22, 19, 15, 12, 16, 14, 17],
name='产品A',
marker_color='lightblue' # 颜色的不同表示方法
))

fig.add_trace(go.Bar(
x=months,
y=[19, 14, 22, 14, 16, 19, 15, 14, 10, 12, 12, 16],
name='产品B',
marker_color='#af0010'
))

fig.update_layout(barmode='group', # 柱状图模式
xaxis_tickangle=-45 # 倾斜角度
)
fig.show()

单个柱状图设置

我们还是自建的数据集:

1
2
3
4
5
6
7
8
9
fig = go.Figure(data=[go.Bar(
x=df1["name"].tolist(), # 姓名作为x轴
y=df1["score"].tolist(), # 分数作为y轴
marker_color=colors # 颜色设置:上面的colors
)])

fig.update_layout(title_text="期末成绩分数")

fig.show()

柱状图宽度设置

1
2
3
4
5
6
7
8
9
10
# 不设置的话,每个柱状图是一样的宽度

fig = go.Figure(data=[go.Bar(
x=df1["name"].tolist(), # 姓名作为x轴
y=df1["score"].tolist(), # 分数作为y轴
)])

fig.update_layout(title_text="期末成绩分数")

fig.show()

默认情况下,每个柱子的宽度是相同的:

我们给每个柱子设置不同的宽度:

1
2
3
4
5
6
7
8
9
fig = go.Figure(data=[go.Bar(
x=df1["name"].tolist(), # 姓名作为x轴
y=df1["score"].tolist(), # 分数作为y轴
width=[1, 0.8, 0.4, 1.2, 0.4] # 宽度设置
)])

fig.update_layout(title_text="期末成绩分数")

fig.show()

改变柱状图基准位置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import plotly.graph_objects as go

years = ['2018','2019','2020']

fig = go.Figure()

fig.add_trace(go.Bar(
x=years,
y=[2000, 3000, 5000],
# base另一种写法:np.array([2000, 3000, 5000]) * (-1)
base=[-2000,-3000,-5000], # 基准设置
marker_color='crimson',
name='商家A'))

fig.add_trace(go.Bar(
x=years,
y=[3000, 4000, 2000],
base=0, # 默认基准设置
marker_color='lightslategrey',
name='商家B'
))

fig.show()

图例位置和颜色

设置图例的位置和颜色:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import plotly.graph_objects as go

subjects = ["语文","数学","英语","物理","化学","生物"]

fig = go.Figure()
fig.add_trace(go.Bar(
x=subjects, # x轴、y轴数据
y=[90,85,78,90,99,80],
name='小明', # 图例名称
marker_color='rgb(15, 83, 109)' # 该组数据颜色

))

fig.add_trace(go.Bar(
x=subjects,
y=[80,89,68,100,78,90],
name='小红',
marker_color='rgb(180, 118, 255)'
))

fig.update_layout(
title='期末成绩对比', # 整个图的标题
xaxis_tickfont_size=12, # x轴字体大小
yaxis=dict(
title='成绩', # y轴的标题
titlefont_size=16, # 标题大小
tickfont_size=12, # y轴上的数值大小
),
legend=dict(
x=0, # 图例的位置
y=1,
# bgcolor='rgba(255, 255, 255, 0)', # 图例的背景色
bordercolor='rgba(2, 255, 255, 0)' # 边缘颜色
),
barmode='group', # 柱状图模式
bargap=0.4, # 组间距离
bargroupgap=0.2 # 组内距离
)
fig.show()

相对模式relative理解

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import plotly.graph_objects as go

x = [1, 2, 3, 4]

fig = go.Figure()
fig.add_trace(go.Bar(x=x, y=[1, 4, 9, 2]))
fig.add_trace(go.Bar(x=x, y=[6, -9, -1.5,4]))
fig.add_trace(go.Bar(x=x, y=[-10, -3, 4.5, -8]))
fig.add_trace(go.Bar(x=x, y=[-1, 3, -3, -4]))

fig.update_layout(
barmode='relative', # 相对初始位置
title_text='相对模式理解')

fig.show()

堆叠模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import plotly.graph_objects as go

subjects=['语文', '数学', '英语']

fig = go.Figure(go.Bar(
x=subjects,
y=[69,71,120],
name='小明'))

fig.add_trace(go.Bar(
x=subjects,
y=[64, 89, 116],
name='小红'))

fig.add_trace(go.Bar(
x=subjects,
y=[76,104, 70],
name='张三'))

fig.update_layout(
barmode='stack', # 堆叠模式
xaxis={'categoryorder':'total descending'}) # category ascending、category descending、total ascending、total descending

fig.show()

多组共享x轴

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import plotly.graph_objects as go

x = [
["小明", "小明", "小明", "小红", "小红", "小红"],
["语文", "数学", "英语", "语文", "数学", "英语",]
]

fig = go.Figure()

fig.add_bar(x=x,
y=[91,72,83,104,85,66],
name="上学期"
)
fig.add_bar(x=x,
y=[106,85,64,93,72,81],
name="下学期"
)

fig.update_layout(barmode="relative")

fig.show()

基于px实现水平柱状图

基础柱状图

需要注意两点:

  • x、y轴的数据需要交换
  • 添加参数orientation
1
2
3
4
5
6
7
df1 = pd.DataFrame({
"name": ["小明","小红","周明","周红","张三"],
"age": [20,28,18,25,36],
"score": ["150","170","160","168","154"]
})

df1

image-20210422002520605

1
2
3
4
5
6
7
fig = px.bar(
df1,
y="name", # xy轴的数据需要交换!!!
x="age",
orientation='h' # 设置:改成水平柱状图
)
fig.show()

显示数据信息

1
2
3
4
5
6
7
8
fig = px.bar(
df1,
y="name",
x="age",
orientation='h', # 水平柱状图
text="age" # 需要显示的数据
)
fig.show()

默认文本是显示在内部的,可以进行修改:

1
2
3
4
5
6
7
8
9
10
11
12
13
# 修改文本位置

fig = px.bar(
df1,
y="name", # xy轴的数据需要交换
x="age",
orientation='h', # 水平柱状图
text="age" # 需要显示的数据
)

fig.update_traces(textposition="outside") # ['inside', 'outside', 'auto', 'none']

fig.show()

设置颜色参数

1
2
3
4
5
6
7
8
9
10
11
12
fig = px.bar(
df1,
y="name",
x="age",
color="age", # 颜色参数
orientation='h', # 水平柱状图
text="age"
)

fig.update_traces(textposition="outside") # ['inside', 'outside', 'auto', 'none']

fig.show()

自定义颜色

1
2
3
4
5
6
7
8
9
10
11
12
fig = px.bar(
df1,
y="name",
x="age",
color_discrete_sequence=px.colors.diverging.RdBu, # 自定义颜色
orientation='h', # 水平柱状图
text="age"
)

fig.update_traces(textposition="outside") # ['inside', 'outside', 'auto', 'none']

fig.show()

小费案例

多参数个性设置

1
2
3
4
5
6
7
8
9
10
fig = px.bar(
tips,
x="total_bill",
y="sex",
color='day',
orientation='h',
hover_data=["tip", "size"],
height=400,
title='小费案例')
fig.show()

基于go实现水平柱状图

基础图形

1
2
3
4
5
6
fig = go.Figure(go.Bar(
x=df1["score"].tolist(), # x轴数据
y=df1["name"].tolist(), # y轴数据
orientation='h'))

fig.show()

设置颜色和模式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import plotly.graph_objects as go

# 生成画布
fig = go.Figure()

# 添加3个图形轨迹
fig.add_trace(go.Bar(
y=['小明', '小红', '张三'],
x=[120, 74, 103],
name='语文',
orientation='h',
marker=dict(
color='rgba(246, 78, 139, 0.6)',
line=dict(color='rgba(246, 78, 139, 1.0)', width=3)
)
))

fig.add_trace(go.Bar(
y=['小明', '小红', '张三'],
x=[92, 128, 89],
name='数学',
orientation='h',
marker=dict(
color='rgba(5, 71, 80, 0.6)',
line=dict(color='rgba(58, 71, 80, 1.0)', width=3)
)
))

fig.add_trace(go.Bar(
y=['小明', '小红', '张三'],
x=[92, 78, 69],
name='英语',
orientation='h',
marker=dict(
color='rgba(18, 7, 80, 0.6)',
line=dict(color='rgba(58, 71, 80, 1.0)', width=3)
)
))


fig.update_layout(barmode='stack') # 堆叠模式:['stack', 'group', 'overlay', 'relative']


fig.show()

4种不同模式的对比:

总结

本文结合自建和Plotly中自带的数据集,详细的介绍了如何基于plotly_express和plotly.graph_objects两种方式来实现不同需求和显示方式的柱状图和水平柱状图,希望对读者朋友有所帮助。

本文标题:可视化神器Plotly玩转柱状图

发布时间:2021年04月22日 - 00:04

原始链接:http://www.renpeter.cn/2021/04/22/%E5%8F%AF%E8%A7%86%E5%8C%96%E7%A5%9E%E5%99%A8Plotly%E7%8E%A9%E8%BD%AC%E6%9F%B1%E7%8A%B6%E5%9B%BE.html

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

Coffee or Tea