It's so beautiful, I didn't expect these cool and dynamic pictures to be drawn in Python!

Click on the blue " Python Space " to follow me

Add a " star " and learn happily together every day

Selected from TowardsDataScience     Author: Liana Mehrabyan

Almost Human compiler      parameters with: Panda

Data can help us describe the world, explain our ideas and show our achievements, but if there are only tedious texts and numbers, we often can't catch the audience's attention. In many cases, a beautiful visual chart is more than a thousand words. This article will introduce five visualization methods based on Plotly. You will find that the original visualization can be used not only for histograms and box graphs, but also for being so dynamic, beautiful and even interactive.

For data scientists, storytelling is a vital skill. In order to express our thoughts and convince others, we need effective communication. The beautiful visualization is an excellent tool for this task. This article will introduce  5 non-traditional visualization techniques that can make your data story more beautiful and effective. This will use Python's Plotly graphics library (also available through R), allowing you to generate animated charts and interactive charts effortlessly.

So, what are the benefits of Plotly? Plotly has strong integration capabilities: it can be used with Jupyter Notebook, it can be embedded in a website, and it fully integrates Dash-an excellent tool for building dashboards and analytical applications.

start up

If you have not installed Plotly, just run the following command in your terminal to complete the installation:

pip install plotly

After the installation is complete, start using it!

Animation

When studying the evolution of this or that indicator, we often involve time data. The Plotly animation tool requires only one line of code to allow people to watch the data change over time , as shown in the following figure:

code show as below:

import plotly.express as px
from vega_datasets import data
df = data.disasters()
df = df[df.Year > 1990]
fig = px.bar(df,
             y="Entity",
             x="Deaths",
             animation_frame="Year",
             orientation='h',
             range_x=[0, df.Deaths.max()],
             color="Entity")
# improve aesthetics (size, grids etc.)
fig.update_layout(width=1000,
                  height=800,
                  xaxis_showgrid=False,
                  yaxis_showgrid=False,
                  paper_bgcolor='rgba(0,0,0,0)',
                  plot_bgcolor='rgba(0,0,0,0)',
                  title_text='Evolution of Natural Disasters',
                  showlegend=False)
fig.update_xaxes(title_text='Number of Deaths')
fig.update_yaxes(title_text='')
fig.show()

As long as you have a time variable to filter, then almost any chart can be animated. Here is an example of making a scatterplot animation:

import plotly.express as px
df = px.data.gapminder()
fig = px.scatter(
    df,
    x="gdpPercap",
    y="lifeExp",
    animation_frame="year",
    size="pop",
    color="continent",
    hover_name="country",
    log_x=True,
    size_max=55,
    range_x=[100, 100000],
    range_y=[25, 90],

    #   color_continuous_scale=px.colors.sequential.Emrld
)
fig.update_layout(width=1000,
                  height=800,
                  xaxis_showgrid=False,
                  yaxis_showgrid=False,
                  paper_bgcolor='rgba(0,0,0,0)',
                  plot_bgcolor='rgba(0,0,0,0)')

Sun figure

The sunburst chart is a good way to visualize group by statements. If you want to decompose a given quantity by one or more categorical variables , then use the sun chart.

Suppose we want to break down the average tip data based on gender and time of day, then this double group by statement can be visualized more effectively than a table.

This chart is interactive, allowing you to click and explore various categories yourself. You only need to define all your categories, and declare the hierarchy between them (see the parents parameter in the following code) and assign the corresponding value, which is the output of the group by statement in our case.

import plotly.graph_objects as go
import plotly.express as px
import numpy as np
import pandas as pd
df = px.data.tips()
fig = go.Figure(go.Sunburst(
    labels=["Female", "Male", "Dinner", "Lunch", 'Dinner ', 'Lunch '],
    parents=["", "", "Female", "Female", 'Male', 'Male'],
    values=np.append(
        df.groupby('sex').tip.mean().values,
        df.groupby(['sex', 'time']).tip.mean().values),
    marker=dict(colors=px.colors.sequential.Emrld)),
                layout=go.Layout(paper_bgcolor='rgba(0,0,0,0)',
                                 plot_bgcolor='rgba(0,0,0,0)'))

fig.update_layout(margin=dict(t=0, l=0, r=0, b=0),
                  title_text='Tipping Habbits Per Gender, Time and Day')
fig.show()

Now we add another layer to this hierarchy:

To do this, we add another group by statement that involves three categorical variables.

import plotly.graph_objects as go
import plotly.express as px
import pandas as pd
import numpy as np
df = px.data.tips()
fig = go.Figure(go.Sunburst(labels=[
    "Female", "Male", "Dinner", "Lunch", 'Dinner ', 'Lunch ', 'Fri', 'Sat',
    'Sun', 'Thu', 'Fri ', 'Thu ', 'Fri  ', 'Sat  ', 'Sun  ', 'Fri   ', 'Thu   '
],
                            parents=[
                                "", "", "Female", "Female", 'Male', 'Male',
                                'Dinner', 'Dinner', 'Dinner', 'Dinner',
                                'Lunch', 'Lunch', 'Dinner ', 'Dinner ',
                                'Dinner ', 'Lunch ', 'Lunch '
                            ],
                            values=np.append(
                                np.append(
                                    df.groupby('sex').tip.mean().values,
                                    df.groupby(['sex',
                                                'time']).tip.mean().values,
                                ),
                                df.groupby(['sex', 'time',
                                            'day']).tip.mean().values),
                            marker=dict(colors=px.colors.sequential.Emrld)),
                layout=go.Layout(paper_bgcolor='rgba(0,0,0,0)',
                                 plot_bgcolor='rgba(0,0,0,0)'))
fig.update_layout(margin=dict(t=0, l=0, r=0, b=0),
                  title_text='Tipping Habbits Per Gender, Time and Day')

fig.show()

Parallel category

Another way to explore the relationship between categorical variables is the following flow chart. You can drag and drop, highlight, and browse values ​​at any time, which is ideal for presentations.

code show as below:

import plotly.express as px
from vega_datasets import data
import pandas as pd
df = data.movies()
df = df.dropna()
df['Genre_id'] = df.Major_Genre.factorize()[0]
fig = px.parallel_categories(
    df,
    dimensions=['MPAA_Rating', 'Creative_Type', 'Major_Genre'],
    color="Genre_id",
    color_continuous_scale=px.colors.sequential.Emrld,
)
fig.show()

Parallel coordinate graph

The parallel coordinate chart is a continuous version of the above chart. Here, each string represents a single observation . This is a type that can be used to identify outliers (a single line away from other data), clustering, trends, and redundant variables (for example, if two variables have similar values ​​on each observation, then they will be on the same horizontal line , Indicating that there is redundancy).

code show as below:

 import plotly.express as px
from vega_datasets import data
import pandas as pd
df = data.movies()
df = df.dropna()
df['Genre_id'] = df.Major_Genre.factorize()[0]
fig = px.parallel_coordinates(
    df,
    dimensions=[
        'IMDB_Rating', 'IMDB_Votes', 'Production_Budget', 'Running_Time_min',
        'US_Gross', 'Worldwide_Gross', 'US_DVD_Sales'
    ],
    color='IMDB_Rating',
    color_continuous_scale=px.colors.sequential.Emrld)
fig.show()

Scale graphs and indicators

The scale chart is just for good looking. This chart can be used when reporting success indicators such as KPIs and showing their distance from your goal.

Indicators are very useful in business and consulting. They can supplement visual effects with text marks, attract the attention of the audience and show your growth indicators.

 import plotly.graph_objects as go
fig = go.Figure(go.Indicator(
    domain = {'x': [0, 1], 'y': [0, 1]},
    value = 4.3,
    mode = "gauge+number+delta",
    title = {'text': "Success Metric"},
    delta = {'reference': 3.9},
    gauge = {'bar': {'color': "lightgreen"},
        'axis': {'range': [None, 5]},
             'steps' : [
                 {'range': [0, 2.5], 'color': "lightgray"},
                 {'range': [2.5, 4], 'color': "gray"}],
          }))
fig.show()

Original link: https://towardsdatascience.com/5-visualisations-to-level-up-your-data-story-e131759c2f41

最后,当当正在搞活动,通过我的优惠码,保证你四折买书!优惠码有限,先到先得,要屯书的千万不要错过!点击链接直达详情:谁也拦不住我,我一定要把这个送给你们!本号读者福利我将自己的原创文章整理成了一本电子书,取名《Python修炼之道》,一共 400 页,17w+ 字,目录如下:      免费送给大家,在我的公众号回复 修炼之道 即可获取-END-推荐阅读:爆肝整理 400 页 《Python 修炼之道》,高清电子书送给一直支持我的读者!
很多程序员被英语搞的死去活来,热心的我连夜找到这份专为程序员编写的英语指南!
算法题从入门到放弃?刷了几千道算法题,关于如何刷题有些话我想对你说
刷了几千道算法题,这些我私藏的刷题网站都在这里了!B站收藏 6.1w+!GitHub 标星 3.9k+!这门神课拯救了我薄弱的计算机基础????扫描上方二维码即可关注
Published 616 original articles · praised 8314 · 1.59 million views

Guess you like

Origin blog.csdn.net/u013486414/article/details/105548604