[Visualization of ratio] Plotly and dynamic visualization [python, pie, sunburst, sanky, treemap, fannele,]

tr.gif

python==3.8 plotly==4.10.0

pie (yen, donut)

Basic circle

You don't need to explain about pie charts anymore

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

with graph_objects

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

Make donuts

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

sunburst

Specify in the order specified as the parent in path The size of the value is specified by values

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

sun.gif

Change color

It also color-codes the size of the value

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

alluvial (alluvial map)

Basic

import plotly.express as px
df = px.data.tips()
fig = px.parallel_categories(df)
fig.show()

image.png

parallel_categories

Count the categorical variables to make a parallel view Specify with color to visualize which category exists in other variables at what rate

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

Check the distribution of continuous values one by one You can visually check where there are many and how much they vary, rather than a cohesive ratio.

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 diagram (flow chart)

A graph that began when Mr. Sanky visualized the flow rate of energy

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

An arrow is drawn from the source entered as a real number to the target, and the thickness of the arrow is passed to the value. Specify only the number of uniques in source and target for label The specified order corresponds to the size of the source and target numbers.

If it is in the form of data

image.png

Make a state like Convert the passing relationship to a number

image.png

By doing such pretreatment, Which category holds how much flow Percentage distributed to other categories And so on

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

treemap

Express the ratio by area Use of expenses, confirmation of inventory ratio, etc.

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()

Branch in the order passed to path If you want to make a background, make an arbitrary one with constant

tr.gif

funnel basic

Can express the progress of the ratio Can express attenuation from website viewing to purchase

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

Some at the same time

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()

If text info is percentage total, it shows the ratio with the whole as 100

image.png

Represent the funnel as a plane

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

Recommended Posts

[Visualization of ratio] Plotly and dynamic visualization [python, pie, sunburst, sanky, treemap, fannele,]
[Talking about the drawing structure of plotly] Dynamic visualization with plotly [python]
[Density visualization] Plotly and dynamic visualization [python3, hist, kde, join, contour, heat map]
[Drawing and labeling multiple graphs] Plotly dynamic visualization [python3, make subplot, xlabel, ylabel]
[Various image analysis with plotly] Dynamic visualization with plotly [python, image]
[Write to map with plotly] Dynamic visualization with plotly [python]
[EDA super basic] Plotly and dynamic visualization [python3, table, bar, box, violin, joy]
[Scatter plot, 3D plot and regression plane] Plotly dynamic visualization [python, scatter, 3D, surface, pair, joint]
[Time series with plotly] Dynamic visualization with plotly [python, stock price]
Source installation and installation of Python
[Capacity indicator, Gantt chart, UI] Plotly dynamic visualization [python, gauge display, Gantt chart]