**Python free course registration, click on the “Read the text” to quickly grab!

From TowardsDataScience by Liana Mehrabyan

Heart of machine compilation participation: Panda

Data can help us describe the world, explain our ideas and show what we’ve done, but it’s often hard to hold an audience’s attention when it’s just tedious text and numbers. And many times, a beautiful visualization is worth a thousand words. In this article, we’ll introduce you to 5 Plotly based visualizations that can be done dynamically and even interactively, not just with histograms and boxes.

Storytelling is a vital skill for data scientists. In order to express our ideas and convince others, we need to communicate effectively. And nice visualizations are the perfect tool for that. This article introduces five unconventional visualization techniques to make your data stories prettier and more effective. Python’s Plotly graphics library (also available through R) will be used here, allowing you to effortlessly generate animated charts and interactive charts.

So what are the benefits of Plotly? Plotly is highly integrated: it works with Jupyter Notebook, can be embedded in websites, and is fully integrated with Dash, a great tool for building dashboards and analytics applications.

Start the

If you haven’t already installed Plotly, simply run the following command on your terminal to complete the installation:

pip install plotly

Once installed, get started!

animation

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

The code is as follows:

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,

,0,0,0 paper_bgcolor = ‘rgba (0)’,

,0,0,0 plot_bgcolor = ‘rgba (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, almost any chart can be animated. Here is an example of making a scatter plot 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,

,0,0,0 paper_bgcolor = ‘rgba (0)’,

,0,0,0 plot_bgcolor = ‘rgba (0)’)

The sun

Sunburst Chart is a great way to visualize group by statements. If you want to factor a given quantity by one or more category variables, use a sun map.

Given that we want to break down the average tip data by gender and time of day, this dual group by statement can be visualized more effectively than a table.

The chart is interactive, allowing you to click and explore the categories yourself. All you need to do is define all your categories, declare a 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 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 the value of another group BY statement involving three category 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 relationships between category variables is with flowcharts like the one below. You can drag and drop, highlight, and browse values at any time, perfect for demos.

The code is as follows:

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 diagram

The parallel coordinate chart is a continuous version of the chart above. Here, each string represents a single observation. This is a useful tool for identifying outliers (a single line away from other data), clusters, trends, and redundant variables (for example, if two variables have similar values on each observation, they will be on the same level, indicating redundancy).

The code is as follows:

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 diagram and indicator

The scale diagram is just for good looks. Use this chart when reporting success indicators such as KPIs and showing how close they are to your goals.

Indicators are useful in business and consulting. They can complement visuals with text markers, grab your audience’s attention and show your growth metrics.

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”},

The 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 () If you do find a good coworker, you’re lucky. Pythonx, crawler, artificial intelligence, big data software, and PythonWeb, automation, artificial intelligence and other Python programming learning methods. Build from zero to the project development hands-on combat all-round analysis! Click here to join our Python resource sharing collection