Plotly
01 / 02

Express, Graph Objects & Styling

Express, Graph Objects & Styling

plotly.express — Fast, Concise Charts

import plotly.express as px

fig = px.scatter(
    df, x='gdp', y='life_expectancy',
    color='continent',       # categorical -> discrete colors + legend
    size='population',
    hover_name='country',     # bolded tooltip title
    hover_data=['year'],      # extra tooltip fields
    template='plotly_dark',
)
fig.show()  # interactive: hover, zoom, pan, click-legend-to-toggle — free

graph_objects — Fine-Grained Control

import plotly.graph_objects as go

fig = go.Figure(data=[
    go.Bar(x=['A', 'B', 'C'], y=[10, 20, 15], name='2024'),
    go.Bar(x=['A', 'B', 'C'], y=[12, 18, 20], name='2025'),
])
fig.update_layout(title='Comparison', barmode='group')

# Common workflow: start with px for speed, then refine with update_*
fig2 = px.bar(df, x='category', y='value')
fig2.update_traces(marker_color='darkblue')
fig2.update_layout(title='Refined Chart', xaxis_title='Category')

Subplots & Animation

from plotly.subplots import make_subplots

fig = make_subplots(rows=1, cols=2, subplot_titles=('Revenue', 'Users'))
fig.add_trace(go.Scatter(x=months, y=revenue), row=1, col=1)
fig.add_trace(go.Scatter(x=months, y=users), row=1, col=2)

# animation_frame generates a play/pause slider stepping through values
fig3 = px.scatter(
    gapminder_df, x='gdp', y='life_expectancy',
    animation_frame='year', animation_group='country',
)

Keep your own version of these notes — editable, searchable, and organised by your stack.

Start free