Introduction
Plotly creates interactive, publication-quality web-based visualizations.
Basic Charts
import plotly.express as px
# Scatter plot
fig = px.scatter(df, x="feature", y="target", color="category")
fig.show()
# Line chart
fig = px.line(df, x="date", y="value", color="category")
# Bar chart
fig = px.bar(df, x="category", y="value", color="type")
Layout and Styling
fig.update_layout(
title="My Chart",
xaxis_title="X Axis",
yaxis_title="Y Axis",
font=dict(size=12),
template="plotly_white"
)
fig.update_traces(marker=dict(size=10, color="red"))
Subplots
from plotly.subplots import make_subplots
import plotly.graph_objects as go
fig = make_subplots(rows=1, cols=2)
fig.add_trace(go.Scatter(x=[1, 2], y=[1, 2]), row=1, col=1)
fig.add_trace(go.Bar(x=[1, 2], y=[1, 2]), row=1, col=2)
Practice Problems
- Create interactive scatter matrix
- Build choropleth map
- Create animated charts
- Add dropdown menu to chart
- Export to HTML