Plotly Cheat Sheet: Plotly Express Charts and Common Commands

A one-page Plotly cheat sheet to download, with Plotly Express code for twelve chart types and the commands for layout, colors, templates, facets, hover, and export.

Most Plotly charts are one call to Plotly Express: pass a DataFrame and name its columns, as in px.scatter(df, x="price", y="units", color="region"). It returns a Figure that you adjust with fig.update_layout(), fig.update_traces() and fig.update_xaxes(), then show with fig.show() or save with fig.write_html() / fig.write_image(). This cheat sheet collects those commands on one page, with copyable tables below.

Quick Example

A scatter plot with a color per group, bubble size from a third column, hover details, and a clean template. Hover over a point to see its values.

import numpy as np
import pandas as pd
import plotly.express as px

rng = np.random.default_rng(21)
df = pd.DataFrame({
    "region": rng.choice(["North", "South", "East"], 120),
    "month": rng.integers(1, 13, 120),
    "price": rng.uniform(5, 25, 120).round(1),
})
df["units"] = (120 - 3.2 * df["price"] + rng.normal(0, 12, 120)).round()
df["sales"] = df["price"] * df["units"]

fig = px.scatter(df, x="price", y="units", color="region", size="sales",
                 hover_data=["month"], title="Units sold vs price",
                 template="plotly_white")
fig.update_layout(legend_title_text="Region")
fig.show()

Plotly scatter plot of units sold against price for three regions, with bubble size showing sales; units fall as price rises

The units fall as the price rises (a correlation of -0.87 in this data), and the bubble size is the sales (price times units). Click a legend entry to hide a region.

Download the Plotly Cheat Sheet

Download the full-size cheat sheet (PNG). Every chart on it is drawn by the code printed beside it, with Plotly 7.

One page Plotly cheat sheet with setup code, twelve Plotly Express chart types each shown with its code, and reference panels for layout and labels, colors and templates, facets and subplots, and hover, show and export

Setup Code for the Examples

The snippets in the tables use these variables: 150 made-up sales records, the sales per month and region, and the totals per region. Plotly Express takes DataFrames in "long" format, with one row per observation, which is what groupby(..., as_index=False) returns.

import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
from plotly.subplots import make_subplots

rng = np.random.default_rng(8)
df = pd.DataFrame({
    "region": rng.choice(["North", "South", "East"], 150),
    "month": rng.integers(1, 13, 150),
    "price": rng.uniform(5, 25, 150).round(1)})
df["units"] = (120 - 3.2 * df["price"] + rng.normal(0, 12, 150)).round()
df["sales"] = df["price"] * df["units"]
monthly = df.groupby(["month", "region"],
                     as_index=False).sum()
totals = df.groupby("region", as_index=False).sum()
fig = px.scatter(df, x="price", y="units", color="region")

Plotly Express Chart Types

Chart (links to guide) Code
Scatter px.scatter(df, x="price", y="units", color="region")
Line px.line(monthly, x="month", y="sales", color="region")
Bar px.bar(totals, x="region", y="sales")
Stacked bar px.bar(monthly, x="month", y="sales", color="region")
Histogram px.histogram(df, x="price", nbins=20)
Box px.box(df, x="region", y="units")
Violin px.violin(df, x="region", y="units")
Area px.area(monthly, x="month", y="sales", color="region")
Heatmap px.imshow(df[["price", "units", "sales"]].corr(), text_auto=".2f")
Pie px.pie(totals, names="region", values="sales")
Density heatmap px.density_heatmap(df, x="price", y="units")
ECDF px.ecdf(df, x="price", color="region")

Bars are stacked by default when you pass color=; add barmode="group" for side-by-side bars. px.line connects points in row order, so sort by the x column first if your data is shuffled. For maps, 3D, and other chart types, see the Plotly tutorial.

Layout, Titles, and Axes

These methods work on any figure, whether it came from px or go.

Task Code
Title fig.update_layout(title="Title")
Axis title fig.update_xaxes(title="Price")
Size fig.update_layout(width=700, height=400)
Axis range fig.update_yaxes(range=[0, 100])
Rotate tick labels fig.update_xaxes(tickangle=45)
Legend on top fig.update_layout(legend=dict(orientation="h", y=1.1))

Details are in the guides to figure size and margins, axis ranges and log scales, rotating tick labels and legend position.

Colors, Templates, and Styling Traces

Task Code
Discrete colors px.scatter(df, x="price", y="units", color="region", color_discrete_sequence=px.colors.qualitative.Set2)
Continuous scale px.scatter(df, x="price", y="units", color="sales", color_continuous_scale="Viridis")
Template for one figure fig.update_layout(template="plotly_white")
Default template pio.templates.default = "simple_white"
One color fig.update_traces(marker_color="#0f766e")
Opacity fig.update_traces(opacity=0.6)

pio is import plotly.io as pio. Built-in templates include "plotly", "plotly_white", "plotly_dark", "ggplot2", "seaborn", "simple_white" and "none"; templates and themes shows each, and colors covers sequences and scales. update_traces styles every trace; add selector=dict(name="North") to target one.

Facets, Subplots, and Guide Lines

Task Code
Facet by column px.scatter(df, x="price", y="units", facet_col="region")
Subplots fig = make_subplots(rows=1, cols=2)
fig.add_trace(go.Bar(x=totals["region"], y=totals["sales"]), row=1, col=2)
Horizontal line fig.add_hline(y=50)
Shaded band fig.add_vrect(x0=10, x1=15, opacity=0.2)
Annotation fig.add_annotation(x=10, y=60, text="note")
Grouped bars px.bar(monthly, x="month", y="sales", color="region", barmode="group")

facet_col splits one dataset into small multiples with shared axes (facets). Use make_subplots when each panel is a different kind of chart (make_subplots). See also horizontal and vertical lines and annotations.

Hover, Show, and Export

Task Code
Hover columns px.scatter(df, x="price", y="units", hover_data=["month"])
Hover format fig.update_traces(hovertemplate="%{x}: %{y:.0f}")
Compare on hover fig.update_layout(hovermode="x unified")
Show fig.show()
Save as HTML fig.write_html("plot.html")
Save as PNG fig.write_image("plot.png", scale=2)

write_image needs the kaleido package and a Chrome or Chromium install; pip install kaleido is the first step, and exporting static images with Kaleido covers the rest. HTML files are self-contained interactive charts (saving as standalone HTML). Hover text is customized in hover tooltips.

Plotly Express or Graph Objects?

plotly.express plotly.graph_objects
Looks like px.bar(df, x="region", y="sales") go.Figure(go.Bar(x=..., y=...))
Input A DataFrame and column names Lists or arrays per trace
Color by group color="region" One trace per group
Good for Most charts, quickly Mixing chart types, custom traces, subplots

Both build the same Figure, so you can start with px and add traces with fig.add_trace(go.Scatter(...)).

Practical Tips

  • Use long-format data with px. One column for the category and one for the value, rather than one column per category. df.melt() converts from wide.
  • fig.show() opens a browser tab in a script and draws inline in a notebook. fig.write_html() works everywhere.
  • Set a template once with pio.templates.default instead of repeating template= on every chart.
  • Interactive charts don't survive a copy to PDF or slides. Export with write_image for static output.

Similar Topics