DAAF-Contribution-Community/daaf / .claude/skills/plotly
plotly
VerifiedPlotly interactive visualization. Express and Graph Objects: scatter, line, bar, heatmap, 3D, geographic charts; subplots; styling; export. Use when interactivity (hover/zoom) is needed. For static figures use plotnine; for GIS use geopandas. R equivalent: plotly-r (use when execution language is R).
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 8.1 KB |
| references/charts.md | markdown | 6.9 KB |
| references/export.md | markdown | 5.8 KB |
| references/gotchas.md | markdown | 6.9 KB |
| references/quickstart.md | markdown | 5.3 KB |
| references/styling.md | markdown | 7.5 KB |
| references/subplots-facets.md | markdown | 6.1 KB |
references/quickstart.md
View on GitHub# Quickstart
## Installation
No installation needed in DAAF: `plotly` (6.5.2) is pre-installed in the
container. Image export via `kaleido` is NOT installed — use plotnine for static
figures, or export interactive HTML. Runtime installs are blocked in DAAF
(`pip install`/`uv add` are refused both at the command line and inside executed
scripts — see CLAUDE.md § Runtime Package Installation). If an extra is genuinely
required, escalate to the user to add it to the Dockerfile (user additions block
near the end) and rebuild.
## Core Imports
```python
import plotly.express as px # High-level API (recommended start)
import plotly.graph_objects as go # Low-level API (full control)
from plotly.subplots import make_subplots # Multi-panel figures
import plotly.io as pio # I/O and configuration
```
## Plotly Express vs Graph Objects
Plotly has two main APIs:
### Plotly Express (px)
**Best for**: Quick exploration, standard charts, clean code.
```python
import plotly.express as px
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species")
fig.show()
```
- One function call creates complete figure
- Automatic legends, axes, hover info
- Returns a `go.Figure` (can use all go methods)
- Supports 30+ chart types
### Graph Objects (go)
**Best for**: Custom layouts, fine-grained control, complex figures.
```python
import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(
x=[1, 2, 3],
y=[4, 5, 6],
mode="markers",
name="Series A"
))
fig.update_layout(title="My Plot")
fig.show()
```
- Build figures trace by trace
- Full control over every property
- More verbose but more flexible
### When to Use Each
| Use Case | Recommendation |
|----------|----------------|
| Quick data exploration | px |
| Standard chart types | px |
| Complex customization | go |
| Multiple trace types | go |
| Subplots with mixed types | go + make_subplots |
| Starting point, then customize | px, then update_* methods |
## Figure Anatomy
Every Plotly figure has two main parts:
```python
fig = go.Figure(
data=[...], # List of traces (the actual data/charts)
layout={...} # Layout settings (title, axes, legend, etc.)
)
```
Access them:
```python
fig.data # Tuple of traces
fig.layout # Layout object
```
## Built-in Datasets
Plotly Express includes sample datasets:
```python
# Load built-in datasets
df = px.data.iris() # Iris flower measurements
df = px.data.tips() # Restaurant tips
df = px.data.gapminder() # Country statistics over time
df = px.data.stocks() # Stock prices
df = px.data.medals_long() # Olympic medals
df = px.data.wind() # Wind patterns
df = px.data.carshare() # Car sharing locations
df = px.data.election() # Election results
```
Quick example:
```python
import plotly.express as px
df = px.data.iris()
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species")
fig.show()
```
## Displaying Figures
### In Jupyter Notebooks
Figures display automatically as the last expression:
```python
px.scatter(df, x="x", y="y") # Auto-displays
```
Or explicitly:
```python
fig = px.scatter(df, x="x", y="y")
fig.show()
```
### In Python Scripts
Always call `.show()`:
```python
fig = px.scatter(df, x="x", y="y")
fig.show() # Opens in browser
```
Or save to file:
```python
fig.write_html("plot.html") # Interactive (primary export in DAAF)
# fig.write_image("plot.png") # NOT available — kaleido not installed; use plotnine for static
```
### Renderer Configuration
```python
import plotly.io as pio
# See available renderers
print(pio.renderers)
# Set default renderer
pio.renderers.default = "browser" # Open in browser
pio.renderers.default = "notebook" # Jupyter notebook
pio.renderers.default = "svg" # Static SVG
```
## Basic Patterns
### Creating a Figure with px
```python
# Most px functions follow this pattern
fig = px.chart_type(
data_frame, # DataFrame or dict
x="column_name", # X-axis column
y="column_name", # Y-axis column
color="column_name", # Color by category (optional)
title="My Title" # Chart title (optional)
)
```
### Creating a Figure with go
```python
# Create empty figure
fig = go.Figure()
# Add traces
fig.add_trace(go.Scatter(x=x, y=y, name="Series 1"))
fig.add_trace(go.Bar(x=x, y=y2, name="Series 2"))
# Update layout
fig.update_layout(title="My Plot", xaxis_title="X", yaxis_title="Y")
# Show
fig.show()
```
### Updating Figures
Both px and go figures support update methods:
```python
# Update layout
fig.update_layout(title="New Title", height=500)
# Update all traces
fig.update_traces(marker_size=10)
# Update axes
fig.update_xaxes(title_text="X Axis")
fig.update_yaxes(title_text="Y Axis")
```
## Method Chaining
All update methods return the figure, enabling chaining:
```python
fig = (
px.scatter(df, x="x", y="y", color="category")
.update_layout(title="My Plot")
.update_traces(marker_size=12)
.update_xaxes(showgrid=False)
)
```
## Quick Comparison
| Task | Plotly Express | Graph Objects |
|------|----------------|---------------|
| Scatter plot | `px.scatter(df, x="a", y="b")` | `go.Figure(go.Scatter(x=x, y=y))` |
| Add color | `color="col"` parameter | `marker_color=` in trace |
| Add title | `title="My Title"` | `.update_layout(title="My Title")` |
| Multiple series | `color="series_col"` | Multiple `add_trace()` calls |
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 193k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 193k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 193k | Updated | Claude CodeCodex | Verified | Install |