# Plotly Cheat Sheet: Plotly Express Charts and Common Commands

> Plotly cheat sheet with a downloadable one-page PNG: px.scatter, line, bar, histogram, box, heatmap and more, plus update_layout, templates, make_subplots, add_hline, hover and write_image.

Source: https://www.pythoncharts.com/plotly/plotly-cheat-sheet/ | Published: 2026-12-06 | Author: Alex Scott | Category: Plotly | Tags: plotly, cheat-sheet, plotly-express, templates, subplots, kaleido, python

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.

```python
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](https://www.pythoncharts.com/plotly/plotly-cheat-sheet/images/plotly-cheat-sheet-quick-example.png)

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)](https://www.pythoncharts.com/plotly/plotly-cheat-sheet/images/plotly-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](https://www.pythoncharts.com/plotly/plotly-cheat-sheet/images/plotly-cheat-sheet.png)

## 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.

```python
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](https://www.pythoncharts.com/plotly/plotly-scatter-plot/) | `px.scatter(df, x="price", y="units", color="region")` |
| [Line](https://www.pythoncharts.com/plotly/plotly-express-line-charts/) | `px.line(monthly, x="month", y="sales", color="region")` |
| [Bar](https://www.pythoncharts.com/plotly/plotly-express-bar-charts/) | `px.bar(totals, x="region", y="sales")` |
| [Stacked bar](https://www.pythoncharts.com/plotly/plotly-express-bar-charts/) | `px.bar(monthly, x="month", y="sales", color="region")` |
| [Histogram](https://www.pythoncharts.com/plotly/plotly-histograms/) | `px.histogram(df, x="price", nbins=20)` |
| [Box](https://www.pythoncharts.com/plotly/plotly-box-violin-plots/) | `px.box(df, x="region", y="units")` |
| [Violin](https://www.pythoncharts.com/plotly/plotly-box-violin-plots/) | `px.violin(df, x="region", y="units")` |
| [Area](https://www.pythoncharts.com/plotly/plotly-area-charts/) | `px.area(monthly, x="month", y="sales", color="region")` |
| [Heatmap](https://www.pythoncharts.com/plotly/plotly-heatmaps/) | `px.imshow(df[["price", "units", "sales"]].corr(), text_auto=".2f")` |
| [Pie](https://www.pythoncharts.com/plotly/plotly-pie-charts-donut-charts/) | `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](https://www.pythoncharts.com/plotly/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](https://www.pythoncharts.com/plotly/plotly-figure-size-margins/), [axis ranges and log scales](https://www.pythoncharts.com/plotly/plotly-axis-range-log-scale/), [rotating tick labels](https://www.pythoncharts.com/plotly/plotly-rotate-tick-labels-plotly-express/) and [legend position](https://www.pythoncharts.com/plotly/plotly-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](https://www.pythoncharts.com/plotly/plotly-templates-themes/) shows each, and [colors](https://www.pythoncharts.com/plotly/plotly-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)`<br>`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](https://www.pythoncharts.com/plotly/plotly-facets/)). Use `make_subplots` when each panel is a different kind of chart ([make_subplots](https://www.pythoncharts.com/plotly/plotly-make-subplots/)). See also [horizontal and vertical lines](https://www.pythoncharts.com/plotly/plotly-add-horizontal-vertical-lines/) and [annotations](https://www.pythoncharts.com/plotly/plotly-add-custom-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](https://www.pythoncharts.com/plotly/plotly-export-static-images-with-kaleido/) covers the rest. HTML files are self-contained interactive charts ([saving as standalone HTML](https://www.pythoncharts.com/plotly/plotly-saving-standalone-html-files/)). Hover text is customized in [hover tooltips](https://www.pythoncharts.com/plotly/plotly-customize-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

- [Plotly Tutorial: A Guide to Interactive Charts in Python](https://www.pythoncharts.com/plotly/plotly-tutorial/)
- [Plotly Templates and Themes](https://www.pythoncharts.com/plotly/plotly-templates-themes/)
- [Matplotlib Cheat Sheet: Plot Types and Common Commands](https://www.pythoncharts.com/matplotlib/matplotlib-cheat-sheet/)
