Plotly Express - Bars and donuts

1 minute read

For quick data exploration and visualization, Plotly Express, provides an easy-to-use, high-level interface to the Plotly graphing libraries. It is typically imported as px in python and every function returns a graph_objects.Figure

import plotly.express as px
import plotly.graph_objects as go
fig = go.Figure()
fig = px.pie(df_prepared, 
              values='AnzahlFall', names='Geschlecht')

Exemplary application on Corona data

As an example px bar and pie charts are used to explore aspects of corona data and visualize insights.

Corona cases by age group

The visualization of corona data by age group shows that the virus is more dangerous for older people than for younger ones.

While the majority of infections obviously occur in the 35+ age group, the picture does not differ significantly from the demographic distribution (assuming that the mobility of young children is relatively limited).

However, the risk of a fatal outcome is significantly higher with increasing age

Corona cases by Gender

Visualization by gender shows that more women have been infected than men, but the infection is more likely to be fatal for men than for women.

In order to support the intuitive perception of the message, the colours in the pie chart should be defined, as plotly would otherwise distribute them in descending order by the proportion in the diagram. The legend is also automatically sorted in descending order.

fig = px.pie(df_prepared, 
              values='AnzahlFall',
              names='Geschlecht',
              hole=.3, 
              color='Geschlecht',
              color_discrete_map = {"W": "#ef553b", "M": "#636efa"})

The hole parameter transforms the pie into a donut chart.

Chart Colors

Plotly express has an integrated chart, which helps selecting colors.

fig = px.colors.qualitative.swatches()
fig.show()

References

Jupyter Notebook for this article

Introduction to Plotly Express