Plotly Tutorial: A Guide to Interactive Charts in Python

A map of the Plotly tutorials: chart types, titles and legends, axes, interactivity, subplots, themes, export, and Dash.

To make an interactive chart with Plotly, call a Plotly Express function such as px.line() or px.bar() on a DataFrame, then fig.show(). The result has hover tooltips and zoom without extra code. These tutorials are grouped by task, and newer ones are written for Plotly 6.

Grid of twelve Plotly charts: line, bar, box, heatmap, subplots, 3D, Sankey, treemap, sunburst, candlestick, map and funnel charts

Quick Example

import pandas as pd
import plotly.express as px

df = pd.DataFrame({
    "quarter": ["Q1", "Q2", "Q3", "Q4"] * 2,
    "region": ["North"] * 4 + ["South"] * 4,
    "revenue": [210, 245, 238, 290, 180, 198, 231, 262],
})

fig = px.bar(df, x="quarter", y="revenue", color="region",
             barmode="group", title="Revenue by Region",
             labels={"revenue": "Revenue ($k)", "quarter": ""})
fig.show()

Grouped bar chart in which the North region's quarterly revenue is above the South region's in all four quarters

Where Should I Start With Plotly?

Step Question Tutorial
1. First chart How do I draw lines from a DataFrame? Line charts
2. Compare How do I group or stack bars? Bar charts
3. Label it How do I set titles and font sizes? Font size and titles
4. Inspect How do I control what hover shows? Hover tooltips
5. Share How do I send someone the chart? Standalone HTML

If fig.show() displays nothing, start with renderers.

Which Plotly Chart Should I Use?

To choose by appearance, browse the Python Chart Gallery.

To show Function Tutorial
A trend over time px.line Line charts
Values by category px.bar Bar charts
One distribution px.histogram Histograms
Distributions by group px.box, px.violin Box and violin plots
A grid of values px.imshow Heatmaps
Parts of a whole px.pie Pie and donut charts
A hierarchy as rectangles px.treemap Treemaps
A hierarchy as rings px.sunburst Sunburst charts
Flows between stages go.Sankey Sankey diagrams
Drop-off between steps px.funnel Funnel charts
A schedule px.timeline Gantt charts
Stock prices go.Candlestick Candlestick charts
Places px.scatter_geo, px.choropleth Maps
Three dimensions px.scatter_3d, go.Surface 3D plots
Several measures per item go.Scatterpolar Radar charts
Many numeric columns px.parallel_coordinates Parallel coordinates
Change over time as motion animation_frame= Animated bubble charts
A single headline number go.Indicator Gauges and KPI cards

Layout, Legends, and Axes

You want to Tutorial
Set width, height and margins Figure size and margins
Move the legend Legend position
Change background colors Plot and paper background
Apply a theme or build your own Templates and themes
Format dollars or percentages Axis number formats
Rotate tick labels Rotate tick labels
Add reference lines Horizontal and vertical lines
Add notes and arrows Annotations

Subplots

You want to Tutorial
Combine charts or add a second y-axis make_subplots
Keep panels in proportion Fixed aspect ratios

Interactivity and Performance

You want to Tutorial
Switch data from a menu or button Dropdowns and buttons
Scrub through a long time series Range sliders and selectors
Hide the toolbar Remove the modebar
Lock zoom on one axis Disable zooming or panning
Draw many points smoothly WebGL performance
Connect charts to Python callbacks Dash tutorial

Export Plotly Charts

You need Method Tutorial
An interactive file fig.write_html() Standalone HTML
A chart inside your own web page fig.to_html(full_html=False) Embed charts in a website
A PNG, SVG or PDF fig.write_image() with Kaleido Static image export

Should I Use Plotly Express or Graph Objects?

Choose When
plotly.express (px) You have a DataFrame and want one function per chart
plotly.graph_objects (go) You are assembling traces yourself or need a chart px lacks, such as go.Sankey
make_subplots Several chart types have to share one figure
Dash Inputs need to rerun Python code

A Plotly Express call returns a graph objects Figure, so you can start with px and refine it with fig.update_layout() and fig.update_traces().

Common Plotly Problems

Problem Fix Details
The chart doesn't appear Check the renderer and that nbformat is installed Renderers
A line zigzags back and forth Sort the data by the x column Line charts
write_image() fails Install Kaleido and the browser it needs Kaleido
Long labels are cut off Increase the margin or turn on automargin Size and margins
Large scatter plots lag Use render_mode="webgl" or go.Scattergl WebGL

Similar Topics