Python Tutorial
Plotly Charts
Plotly Express has a function per chart type. Pass a DataFrame plus column names.
Line, Bar, Scatter, Pie
import plotly.express as px
import pandas as pd
df = pd.DataFrame({
"month": ["Jan", "Feb", "Mar"],
"sales": [12, 18, 15],
"cost": [8, 11, 9],
})
px.line(df, x="month", y="sales").show()
px.bar(df, x="month", y="sales").show()
px.scatter(df, x="cost", y="sales", size="sales").show()
px.pie(df, names="month", values="sales").show()📘 Real-World Deep Dive
Knowing <strong>Plotly Charts (Plotly)</strong> well is what turns Plotly from a curiosity into a daily tool — you'll reach for it in nearly every real project.
Real-Life Scenario
An end-to-end usage of Plotly Charts that you'd actually see in a data pipeline or analytics notebook.
Real-Life Example
import plotly.graph_objects as go
fig = go.Figure(data=[go.Bar(x=["a","b","c"], y=[3, 1, 5])])
fig.update_layout(title="Quick bar chart")
fig.show()Expected Output
(no output)Common mistakes
- Plotly Express (
px) is concise but auto-layouts aggressively — switch to Graph Objects (go) for full control. - Passing a list with mixed types to
px.linetriggers silent upcasting; convert first to typed arrays. - Plotly expects millisecond timestamps for time axes (
df["date"].astype("datetime64[ms]")). - Treating Plotly Charts as a black box without reading the docs — the API has subtle defaults that bite when you scale.
🚀 Performance & Best Practices
- For large plots use
Scattergl(WebGL) instead ofScatter. - In Dash apps, send data with
dcc.Store+json.dumpsrather than re-passing Python objects. - Disable
uirevisionwhen you want Plotly to fully redraw instead of preserving state. - When working with Plotly, prefer vectorised / batched operations over Python loops.
🧪 Try It Yourself
- Reproduce the snippet on a representative slice of your own data.
- Profile the snippet with
cProfileortimeitand find the single biggest improvement. - Generalise the snippet into a small, reusable function you can drop into future projects.