[比率的可视化]绘图和动态可视化[python,pie,sunburst,sanky,treemap,fannele]


tr.gif

python == 3.8
情节== 4.10.0

派(日元,甜甜圈)

基本圈

没有关于饼图的更多解释

1
2
3
4
5
import plotly.express as px
df = px.data.tips()
fig = px.pie(df, values='tip', names='day',
             title='pie plot with PX')
fig.show()

image.png

在graph_objects中

1
2
3
4
5
6
7
8
9
10
import plotly.graph_objects as go
df = px.data.tips()
fig = go.Figure(data=[go.Pie(labels=df['day'],
                             values=df['tip'])])

fig.update_traces(hoverinfo='label+percent',
                  textinfo='value',
                  textfont_size=20,
                  marker=dict(line=dict(width=2)))
fig.show()

image.png

做甜甜圈

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

fig = go.Figure(data=[go.Pie(labels=df['day'],
                             values=df['tip'],
                     hole=.3)
                     ])

fig.update_traces(hoverinfo='label+percent',
                  textinfo='value',
                  textfont_size=20,
                  marker=dict(line=dict(width=2)))
fig.show()

image.png

朝阳

按照在路径中指定为父级的顺序指定
值的大小由值

指定

1
2
3
4
import plotly.express as px
df = px.data.tips()
fig = px.sunburst(df, path=['day', 'time', 'sex'], values='total_bill')
fig.show()

sun.gif

变色

值的大小也用颜色编码

1
2
3
4
import plotly.express as px
df = px.data.tips()
fig = px.sunburst(df, path=['day', 'time', 'sex'], values='total_bill',color='total_bill')
fig.show()

image.png

冲积(冲积图)

基本的

1
2
3
4
import plotly.express as px
df = px.data.tips()
fig = px.parallel_categories(df)
fig.show()

image.png

parallel_categories

计算类别变量以创建并行视图
用颜色指定以可视化在其他变量中以何种比例存在哪个类别

1
2
3
4
5
import plotly.express as px
df = px.data.tips()
fig = px.parallel_categories(df, color="size",
                             color_continuous_scale=px.colors.sequential.Inferno)
fig.show()

image.png

parallel_coordinates

逐一检查连续值的分布
您可以目视检查在哪里有多少以及它们有多少变化,而不是内聚比

1
2
3
4
5
6
import plotly.express as px
df = px.data.tips()
fig = px.parallel_coordinates(df, color="size",
                              dimensions=['total_bill', 'size', 'tip'],
                              color_continuous_midpoint=2)
fig.show()

image.png

粗略图(流程图)

Sanky先生可视化能量流率时开始的图

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

fig = go.Figure()

fig.add_trace(

    go.Sankey(

    node = dict(pad = 15,thickness = 20,line = dict(color = "black", width = 0.5),
    label = ["n0","n1","n2","n3","n4"],
    color = "blue"),

    link = dict(
      source = [0,1,2,3],
      target =[1,2,3,4],
      value = [5,10,15,20])

    )
)

fig.update_layout(title_text="Sankey Diagram", font_size=10)
fig.show()

image.png

从作为实数输入的源中为目标绘制一个箭头,并将箭头的粗细传递到值
在标签的源和目标中仅指定唯一编号
指定的顺序以及源编号和目标编号的大小对应于

如果是数据形式,则

image.png

创建一个名为

的状态
将传递的关系转换为数字

image.png

通过进行这样的预处理,
哪个类别拥有多少流量
分配给其他类别的百分比
<铅>

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
d1 = df.groupby(["sex","smoker"], as_index=False).sum()[["sex","smoker",'total_bill']]
d1.columns = ['sor','tar','total_bill']
d2 = df.groupby(["smoker","day"], as_index=False).sum()[["smoker","day",'total_bill']]
d2.columns = ['sor','tar','total_bill']
d3 = df.groupby(["day","time"], as_index=False).sum()[["day","time",'total_bill']]
d3.columns = ['sor','tar','total_bill']
concat_d = pd.concat([d1, d2, d3],axis=0, ignore_index=True)
label_list = pd.concat([concat_d['sor'],concat_d['tar']],axis=0).unique().astype('str')


for i in range(0,len(label_list)):
    for j in range(0,len(concat_d['sor'])):

        if concat_d['sor'].astype('str')[j]==label_list[i]:
            concat_d['sor'][j]=i

        if concat_d['tar'].astype('str')[j]==label_list[i]:
            concat_d['tar'][j]=i

import plotly.graph_objects as go
fig = go.Figure()

fig.add_trace(

    go.Sankey(

    node = dict(pad = 15,thickness = 20,line = dict(color = "black", width = 0.5),
    label = label_list,
    color = "blue"),

    link = dict(
      source = concat_d.sor,
      target =concat_d.tar,
      value = concat_d.total_bill)

    )
)

fig.update_layout(title_text="Basic Sankey Diagram", font_size=10)
fig.show()

image.png

树状图

按面积表示比率
费用的使用,库存比率的确认等。

1
2
3
4
5
6
7
8
import plotly.express as px
df = px.data.tips()
fig = px.treemap(df,
                 path=[px.Constant('back ground'),'sex','day','time'],
                 values='total_bill',
                 color='sex'
                )
fig.show()

分支按顺序传递到路径
如果要制作背景,请使用常数

任意设置背景

tr.gif

渠道基本

可以表示进度比率

可以表示从网站浏览到购买的衰减

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

fig = go.Figure()

fig.add_trace(go.Funnel(
    name = 'Montreal',
    orientation = "h",
    y = ["2018-01-01", "2018-07-01", "2019-01-01", "2020-01-01"],
    x = [100, 60, 40, 20],
    textposition = "inside",
    texttemplate = "%{y| %a. %_d %b %Y}"))

fig.update_layout(yaxis = {'type': 'date'})

fig.show()

image.png

同时几个

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
from plotly import graph_objects as go

fig = go.Figure()

fig.add_trace(go.Funnel(
    name = 'Montreal',
    orientation = "h",
    y = ["2018-01-01", "2018-07-01", "2019-01-01", "2020-01-01"],
    x = [100, 60, 40, 20],
    textposition = "inside",
    textinfo = "value+percent previous"))

fig.add_trace(go.Funnel(
    name = 'Vancouver',
    orientation = "h",
    y = ["2018-01-01", "2018-07-01", "2019-01-01", "2020-01-01"],
    x = [90, 70, 50, 10],
    textposition = "inside",
    textinfo = "value+percent previous"))


fig.add_trace(go.Funnel(
    name = 'Toronto',
    orientation = "h",
     y = ["2018-01-01", "2018-07-01", "2019-01-01","2020-01-01","2021-01-01"],
    x = [100, 60, 40,30, 20,10],
    textposition = "inside",
    textinfo = "value+percent previous"))

fig.update_layout(yaxis = {'type': 'date'})

fig.show()

文本信息表示全部百分比,如果是总百分比

,则为100

image.png

表示飞机中的漏斗

1
2
3
4
5
import plotly.express as px
fig = px.funnel_area(names=["The 1st","The 2nd", "The 3rd", "The 4th", "The 5th"],
                    values=[5, 4, 3, 2, 1],
                    color=[5, 3, 3, 3, 1])
fig.show()

image.png