Plotly Express#
The plotly.express
module contains functions that can create entire figures at once. plotly.express
functions are designed to be a user-friendly point of entry to the plotly
package. A graph object is created as part of any plotly.express
function, but the point of the plotly.express
function is to significantly reduce the amount of code needed to create, customize, and render the graph object.
Scatterplot Workflows#
For example, to create a scatterplot using plotly.express
, we use the px.scatter()
function, which takes values for the X
and Y
axis.
Without any additional arguments, the default formatting and style options are applied.
import plotly.express as px # import statement
fig = px.scatter(x=[0, 1, 2, 3, 4], y=[0, 1, 4, 9, 16]) # create figure
fig # show figure
Everything from tick marks to axis labels to grid lines to hover labels has been generated by the plotly.express
defaults. To create a scatterplot from data stored in a DataFrame
, the same general syntax specifying X
and Y
values still applies.
Let’s look at a DataFrame
example.
df = px.data.iris() # load data
df # inspect data
sepal_length | sepal_width | petal_length | petal_width | species | species_id | |
---|---|---|---|---|---|---|
0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa | 1 |
1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa | 1 |
2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa | 1 |
3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa | 1 |
4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa | 1 |
... | ... | ... | ... | ... | ... | ... |
145 | 6.7 | 3.0 | 5.2 | 2.3 | virginica | 3 |
146 | 6.3 | 2.5 | 5.0 | 1.9 | virginica | 3 |
147 | 6.5 | 3.0 | 5.2 | 2.0 | virginica | 3 |
148 | 6.2 | 3.4 | 5.4 | 2.3 | virginica | 3 |
149 | 5.9 | 3.0 | 5.1 | 1.8 | virginica | 3 |
150 rows × 6 columns
fig = px.scatter(df, x="sepal_width", y="sepal_length") # create figure
fig # show output
In the dataframe
example, we passed the entire data frame to px.scatter()
, and specify columns to use for the X
and Y
axis. In the resulting figure, we can see how plotly
assigns the column names as axis labels.
Customizing#
We can modify point color and size to reflect underlying values in the data frame (using color
and size
). We can also modify the information displayed in the hover label (using hover_data
).
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species", size="petal_length", hover_data=["petal_width"]) # create figure
fig # show figure
In this modified example, color
specifies what field to use to color points, size
specifies what field to use to size points, and hover_data
adds fields not already incorporated in the figure to the hover label, which now includes 5 different fields.