Learning Plotly Express

Get familiar with plotting datasets using Plotly Express.

Plotly Express is a higher-level plotting system, built on top of Plotly. Not only does it handle certain defaults for us, such as labeling axes and legends, it enables us to utilize our data to express many of its attributes using visual aesthetics (size, color, location, and so on). This can be done simply by declaring what attribute we want to express with which column of our data, given a few assumptions about the data structure. So, it mainly provides us with the flexibility to approach the problem from the data point of view, as mentioned at the beginning of the section.

Let’s first create a simple DataFrame:

Press + to interact
import pandas as pd
df = pd.DataFrame({
'numbers': [1, 2, 3, 4, 5, 6, 7, 8],
'colors': ['blue', 'green', 'orange', 'yellow', 'black',
'gray', 'pink', 'white'],
'floats': [1.1, 1.2, 1.3, 2.4, 2.1, 5.6, 6.2, 5.3],
'shapes': ['rectangle', 'circle', 'triangle', 'rectangle',
'circle', 'triangle', 'rectangle', 'circle'],
'letters': list('AAABBCCC')
})
print(df)

This will produce the DataFrame shown in the Jupyter Notebook set up below:

Please login to launch live app!

We typically create charts with Plotly Express by calling the type of the chart as a function, px.line, px.histogram, and so on. Each function has its own set of parameters, based on its type.

Passing aurguments to the chart functions

There are several ways of passing arguments to those functions, and we’ll focus on two main approaches.

  • A DataFrame with column names: In most cases, the first parameter is data_frame. We set the DataFrame that we want to visualize, and then we specify the columns we want to use for the parameters that we want. For our example DataFrame, if we want to create a scatterplot with “numbers” on the xx axis and “floats” on the yy
...