Plotly Express Cheat Sheet
Plotly Express Cheat Sheet
Data Visualization
Import plotly When working with visualizations like scatter plots, lineplots, and more, you can customize
markers according to certain properties. These include
import plotly.express as px
size: set the marker siz line: set the width and color of a borde
color: set the marker colo symbol: set the shape of the marker
opacity: set the marker transparency
Learn Plotly online at www.DataCamp.com Scatter plots # In this example, we’re updating a scatter plot named fig_sct
fig_sct.update_traces(marker={"size": 24,
"opacity": 0.5,
> What is plotly? Set the size argument to the name of a numeric column to control
the size of the points and create a bubble plot.
fig_sct.show()
"symbol": "square"})
Plotly Express is a high-level data visualization package that allows you to create
interactive plots with very little code. It is built on top of Plotly Graph Objects, which
Line plots Customizing lines in Plotly
provides a lower-level interface for developing custom visualizations.
When working with visualizations that contain lines, you can customize them according
# Create a lineplot on a DataFramed named stock_data
> Interactive controls in Plotly color: set the line colo shape: set how values are connected
Set the line_dash argument to the name of a categorical dash: set the dash style ("solid", ("linear", "spline", "hv", "vh",
column to have dashes or dots for different lines.
"dot", "dash", "longdash", "hvh","vhv"
"dashdot", "longdashdot") width: set the line width
Plotly plots have interactive controls shown in the top-right of the plot. The controls allow Bar plots
# In this example, we’re updating a scatter plot named fig_ln
you to do the following:
# Create a barplot on a DataFramed named commodity_data
fig_ln.update_traces(patch={"line": {"dash": "dot",
"silver": "grey",
fig_ln.show()
"bronze": "brown"})
When working with barplots and histograms, you can update the bars themselves
according to the following properties
Lasso Select: Draw a region of the plot to be highlighted.
Histograms
size: set the marker siz line: set the width and color of a borde
Autoscale: Zoom to a "best" scale.
Set the nbins argument to control the number of bins shown in the # In this example, we’re updating a scatter plot named fig_bar
Toggle Spike Lines: Show or hide lines to the axes whenever you hover over data.
histogram.
fig_bar.update_traces(marker={"color": "magenta",
"opacity": 0.5,
Show closest data on hover: Show details for the nearest data point to the cursor.
Heatmaps fig_bar.show()
Compare data on hover: Show the nearest data point to the x-coordinate of the
cursor.
# Create a heatmap on a DataFramed named iris_data
px.imshow(iris_data.corr(numeric_only=True),
# In this example, we’re updating a histogram named fig_hst
fig_hst.update_traces(marker={"color": "magenta",
Set the text_auto argument to True to display text values for each cell.
"line": {"width": 2, "color": "cyan"}})
fig_hst.show()
The code pattern for creating plots is to call the plotting function, passing a data frame as
the first argument. The x argument is a string naming the column to be used on the x-axis.
The y argument can either be a string or a list of strings naming column(s) to be used on > Customizing plots in plotly
the y-axis.
The code pattern for customizing a plot is to save the figure object returned
px.plotting_fn(dataframe, # Dataframe being visualized
from the plotting function, call its .update_traces() method, then call its .show()
x=["column-for-x-axis"], # Accepts a string or a list of strings
fig = px.some_plotting_function()
www.DataCamp.com
yaxis_title="Y-axis title", # Accepts a string
fig.show()