Fork me on GitHub

一份数据6种Plotly画法

一份数据,6种高级画法!Plotly又秀了一把

针对同一份数据,很多时候我们有不同的图形来进行数据的展示。今天Peter带领大家来体会下不同图表的可视化表现能力,同时感受下Plotly的强大~最终会从6种不同的图形来展示:

  • 柱状图
  • 水平柱状图
  • 饼图
  • 旭日图
  • 桑基图
  • 矩形树状图

数据

数据介绍

这是一份模拟的数据。假设小明同学有这样的一份月度消费数据,体现在衣食住行4个方面。

为了后面的数据进一步处理的方便和绘图,我们把上面的原始数据稍加处理,变成了下面的样子,有三个不同的字段:一级、二级、金额。后面我们也会在pandas中导入这份处理后的数据。

数据导入

在pandas中导入数据,查看前5行:

我们增加一个新的字段:根节点

图1:柱状图

直接使用plotly_express快速绘制柱状图

1
2
3
4
5
fig = px.bar(df,x="二级",y="金额",color="一级",text="金额")

fig.update_traces(textposition="outside")

fig.show()

我们还可以采用堆叠stack的形式:

1
2
3
4
5
6
7
# 我们采用堆叠的形式

fig = px.bar(df,x="一级",y="金额",color="二级",text="金额",barmode="stack")

fig.update_traces(textposition="outside")

fig.show()

图2:水平柱状图

1
2
3
4
5
6
7
8
9
10
11
12
fig = px.bar(
df,
y="一级", # xy轴的数据需要交换!!!
x="金额",
color="二级",
text="金额",
orientation='h' # 设置:改成水平柱状图
)

fig.update_traces(textposition="outside")

fig.show()

图3:饼图

1
2
3
fig = px.pie(df,names="二级",values="金额")

fig.show()

改成显示数值而不是比例的饼图:

图4:旭日图

1、使用plotly_express绘制的初级版本的旭日图,是没有数据显示的

1
2
3
fig = px.sunburst(df,path=["根节点","一级","二级"],values="金额")

fig.show()

2、使用plotly_graph_objects绘制的旭日图

我们需要先把数据进行处理下:

绘图代码如下:

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

fig =go.Figure(go.Sunburst(
labels=df1["一级"].tolist() + df2["二级"].tolist(),
parents=df1["根节点"].tolist() + df2["一级"].tolist(),
values=df1["金额"].tolist() + df2["金额"].tolist(),
branchvalues="total",
text = df1["金额"].tolist() + df2["金额"].tolist()
))
fig.update_layout(margin = dict(t=0, l=0, r=0, b=0))


fig.show()

图5:桑基图

为了绘制桑基图,数据还需要进行处理:

1、修改属性名称

2、合并数据

3、生成父类和子类的标签

1
2
labels = list((set(df3["父类"].tolist() + df3["子类"].tolist())))
numbers = list(range(0,len(labels)))

4、生成父类、子类的属性字段

5、绘图代码

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

# 标签就是index字典中的key键
label = list(index.keys())
# 父类和子类
source = df3["父类索引"].tolist()
target = df3["子类索引"].tolist()
# 流量的值
value = df3["金额"].tolist()

# 生成绘图需要的字典数据
link = dict(source = source, target = target, value = value)
node = dict(label = label, pad=200, thickness=50)

data = go.Sankey(link = link, node=node)

fig = go.Figure(data)

fig.update_layout(title=dict(text="月度消费桑基图",x=0.5,y=0.95))
fig.show()

图6:矩形树状图

1、基于Plotly_express绘制的桑基图

1
2
3
4
5
6
7
8
fig = px.treemap(
df,
path = [px.Constant("all"),"根节点","一级","二级"],
values = "金额",
color = "一级",
)

fig.show()

2、基于plotly_graph_objects绘制

一般情形下基于plotly_graph_objects绘制图形会有更多的参数选项

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

fig =go.Figure(go.Treemap(
labels= df3["子类"],
parents=df3["父类"],
values=df3["金额"],
text = df3["金额"],
textinfo = "label+value+percent parent",
branchvalues = "total"
))

fig.update_layout(margin = dict(t=0, l=0, r=0, b=0))

fig.show()

我们采用了6种不同类型的精美图形来展示这份数据,你觉得哪个更适合展示这份数据?请选择😊

本文标题:一份数据6种Plotly画法

发布时间:2021年11月15日 - 16:11

原始链接:http://www.renpeter.cn/2021/11/15/%E4%B8%80%E4%BB%BD%E6%95%B0%E6%8D%AE6%E7%A7%8DPlotly%E7%94%BB%E6%B3%95.html

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

Coffee or Tea