Loading...
Loading...
A high-level interactive graphing library for Python. Ideal for web-based visualizations, 3D plots, and complex interactive dashboards. Built on plotly.js, it allows users to zoom, pan, and hover over data points in a browser-based environment. Use for interactive charts, web applications, Jupyter notebooks, 3D data visualization, geographic maps, financial charts, animations, time-series analysis, and building production-ready dashboards with Dash.
npx skill4agent add tondevrel/scientific-agent-skills plotlypx.scattergo.Figurefig.update_layoutfig.write_htmlpx.choropleth| Feature | Plotly Express (px) | Graph Objects (go) |
|---|---|---|
| Complexity | High-level, concise. | Low-level, verbose. |
| Data Format | Tidy (long-form) DataFrames. | Lists, Arrays, Dicts, or DataFrames. |
| Customization | Good (using update_*). | Maximum / Full control. |
| Speed of Dev | Very fast. | Slower. |
pip install plotly pandasimport plotly.express as px
import plotly.graph_objects as go
import pandas as pd
import numpy as npimport plotly.express as px
# Load data
df = px.data.iris()
# Create interactive scatter plot
fig = px.scatter(df, x="sepal_width", y="sepal_length",
color="species", size="petal_length",
hover_data=['petal_width'])
# Display
fig.show()fig.write_html("plot.html")template="plotly_dark""ggplot2"# ❌ BAD: Over-complicating a simple plot with Graph Objects
fig = go.Figure()
for species in df['species'].unique():
sub = df[df['species'] == species]
fig.add_trace(go.Scatter(x=sub['sepal_w'], y=sub['sepal_l'], name=species))
# ✅ GOOD: Use Plotly Express (One line, automatic legend/colors)
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species")
# ❌ BAD: Mixing list-style data with DataFrame-style data in px
px.scatter(x=[1,2,3], y=df['column']) # Can lead to alignment issues
# ✅ GOOD: Stick to the DataFrame
px.scatter(df, x="column_a", y="column_b")# Boxplot with points
fig = px.box(df, x="day", y="total_bill", color="smoker", points="all")
# Violin plot with box inside
fig = px.violin(df, x="day", y="total_bill", color="sex", box=True, points="all")
# Heatmap (Density Contour)
fig = px.density_heatmap(df, x="total_bill", y="tip", marginal_x="histogram", marginal_y="histogram")df = px.data.stocks()
# Multiple lines from wide data
fig = px.line(df, x='date', y=["GOOG", "AAPL", "AMZN"], title="Tech Stocks")
# Faceting (Subplots by category)
df = px.data.tips()
fig = px.scatter(df, x="total_bill", y="tip", color="smoker",
facet_col="day", facet_row="time")# 3D Scatter
fig = px.scatter_3d(df, x='sepal_length', y='sepal_width', z='petal_width', color='species')
# 3D Surface (Using Graph Objects)
z_data = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/api_docs/mt_bruno_elevation.csv')
fig = go.Figure(data=[go.Surface(z=z_data.values)])
fig.update_layout(title='Mt Bruno Elevation', autosize=False,
width=500, height=500, margin=dict(l=65, r=50, b=65, t=90))# Scatter on a map
df = px.data.gapminder().query("year == 2007")
fig = px.scatter_geo(df, locations="iso_alpha", color="continent",
hover_name="country", size="pop",
projection="natural earth")
# Detailed Mapbox Choropleth (Needs token or use open-street-map)
fig = px.choropleth_mapbox(df, geojson=counties, locations='fips', color='unemp',
color_continuous_scale="Viridis",
mapbox_style="carto-positron",
zoom=3, center = {"lat": 37.0902, "lon": -95.7129})fig = px.scatter(df, x="x", y="y")
# Global layout updates
fig.update_layout(
title="Custom Styled Plot",
xaxis_title="Dimension X",
yaxis_title="Dimension Y",
font=dict(family="Courier New, monospace", size=18, color="RebeccaPurple"),
legend=dict(yanchor="top", y=0.99, xanchor="left", x=0.01),
plot_bgcolor="white"
)
# Axis specific updates
fig.update_xaxes(showgrid=True, gridwidth=1, gridcolor='LightPink')
fig.update_yaxes(zeroline=True, zerolinewidth=2, zerolinecolor='Black')df = px.data.gapminder()
fig = px.scatter(df, x="gdpPercap", y="lifeExp", animation_frame="year",
animation_group="country",
size="pop", color="continent", hover_name="country",
log_x=True, size_max=55, range_x=[100, 100000], range_y=[25, 90])def create_interactive_report(df, filename="report.html"):
"""Generates a multi-chart HTML report."""
fig1 = px.scatter(df, x="A", y="B", color="C")
fig2 = px.histogram(df, x="A", color="C")
with open(filename, 'a') as f:
f.write(fig1.to_html(full_html=False, include_plotlyjs='cdn'))
f.write(fig2.to_html(full_html=False, include_plotlyjs='cdn'))
# Useful for sharing findings with non-technical stakeholdersimport pandas as pd
from datetime import datetime
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/finance-charts-apple.csv')
fig = go.Figure(data=[go.Candlestick(x=df['Date'],
open=df['AAPL.Open'],
high=df['AAPL.High'],
low=df['AAPL.Low'],
close=df['AAPL.Close'])])
# Remove rangeslider for cleaner look
fig.update_layout(xaxis_rangeslider_visible=False)from plotly.subplots import make_subplots
fig = make_subplots(rows=1, cols=2, subplot_titles=("Plot A", "Plot B"))
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=1, col=1)
fig.add_trace(go.Bar(x=[1, 2, 3], y=[2, 3, 5]), row=1, col=2)
fig.update_layout(height=600, width=800, title_text="Side-by-Side Comparison")# For scatter plots with >10,000 points, use Scattergl (Graph Objects)
# or tell px to use webgl (available in newer versions)
fig = px.scatter(df, x="large_x", y="large_y", render_mode="webgl")
# WebGL drastically improves performance by using the GPU for rendering.# ❌ Problem: Notebook file size explodes to 50MB
# ✅ Solution: Display as static image (requires kaleido) or use a different renderer
# fig.show(renderer="png") # Static
# OR: Clear output after viewing# ❌ Problem: Axes jump around during animation
# ✅ Solution: Manually fix the ranges
fig = px.scatter(df, x="x", y="y", animation_frame="time",
range_x=[0, 100], range_y=[0, 100])# ❌ Problem: Colors change when filtering data because categories disappear
# ✅ Solution: Pass a category_orders dictionary
fig = px.scatter(df, x="x", y="y", color="category",
category_orders={"category": ["A", "B", "C", "D"]})pxgoupdate_layoutfig.write_html("plot.html")template="plotly_dark""ggplot2"px.scattermarginal_xmarginal_yrange_xrange_ycategory_orders