SoFunction
Updated on 2024-11-16

An article that teaches you to draw dynamic visualization charts in Python

preamble

Storytelling is a crucial skill for data scientists. In order to express our ideas and convince others, we need to communicate effectively. And beautiful visualizations are a great tool for this task.

In this article, we will cover 5 non-traditional visualization techniques that can make your data stories prettier and more effective. The Plotly graphics library for Python will be used here, allowing you to generate animated charts and interactive charts effortlessly.

mounting module

If you haven't installed Plotly yet, simply run the following command in your terminal to complete the installation:

pip install plotly

Visualization of dynamic diagrams

When studying the evolution of this or that indicator, we often involve time data.The Plotly animation tool allows one to watch how the data changes over time with just one line of code, as shown below:

The code is as follows:

import  as px
from vega_datasets import data
df = ()
df = df[ > 1990]
fig = (df,
             y="Entity",
             x="Deaths",
             animation_frame="Year",
             orientation='h',
             range_x=[0, ()],
             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='')
()

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

import  as px
df = ()
fig = (
    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=
)
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 picture

The sunburst chart is a great way to visualize group by statements. If you want to break down a given quantity by one or more category variables, then use a sunburst chart.

Assuming we want to break down the average tip data based on gender and time of day, this double group by statement can be presented more effectively through a visualization as opposed to a table.

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

import plotly.graph_objects as go
import  as px
import numpy as np
import pandas as pd
df = ()
fig = ((
    labels=["Female", "Male", "Dinner", "Lunch", 'Dinner ', 'Lunch '],
    parents=["", "", "Female", "Female", 'Male', 'Male'],
    values=(
        ('sex').().values,
        (['sex', 'time']).().values),
    marker=dict(colors=)),
                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')
()

Now we add another layer to this hierarchy:

To do this, we add the value of another group by statement involving three category variables.

import plotly.graph_objects as go
import  as px
import pandas as pd
import numpy as np
df = ()
fig = ((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=(
                                (
                                    ('sex').().values,
                                    (['sex',
                                                'time']).().values,
                                ),
                                (['sex', 'time',
                                            'day']).().values),
                            marker=dict(colors=)),
                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')

()

pointer chart

Pointer charts are just for looks. Use this chart when reporting on success metrics such as KPIs and showing how close they are to your goals.

import plotly.graph_objects as go
fig = ((
    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"}],
          }))
()

sanghatu (Greek letter Świętokrzyn)

Another way to explore the relationships between category variables is with a parallel coordinate chart like the one below. You can drag and drop, highlight, and browse values at any time, making it perfect for presentations.

The code is as follows:

import  as px
from vega_datasets import data
import pandas as pd
df = ()
df = ()
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=,
)
()

parallel coordinate chart

The parallel coordinate chart is a derivative version of the chart above. Here, each string represents a single observation. This is a great tool that can be used to identify outliers (single lines away from the rest of the data), clusters, trends, and redundant variables (e.g., if two variables have similar values on each observation, they will lie on the same horizontal line, indicating redundancy).

The code is as follows:

import  as px
from vega_datasets import data
import pandas as pd
df = ()
df = ()
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=)
()

summarize

To this article on how to use Python to draw dynamic visualization of the article is introduced to this, more related Python to draw dynamic visualization of the content of the chart, please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!