plotly について
Plotlyを使ったPythonのインタラクティブなデータ可視化を解説するスキルです。高水準・低水準API、チャートの調整、操作機能、出力方法を扱います。
主な機能
- 0140種類以上のチャート
- 02Plotly Expressによる簡易作図
- 03Graph Objectsによる詳細な制御
- 04サブプロットとスタイル調整
- 05操作機能とアニメーション
- 06HTML・静止画像への出力
活用シーン
- 01科学データの可視化
- 02統計分布の比較
- 03時系列・金融データの表示
- 04複数チャートのダッシュボード作成
インストール
コマンドをコピーしてターミナルで実行します。
npx skills add https:/ / github.com/ davila7/ claude-code-templates --skill plotlyインストール後、Claude Code で/plotlyを入力して読み込まれたか確認します。
同梱ファイル
SKILL.md と一緒にインストールされる参考資料やスクリプト。
reference/chart-types.mdexport-interactivity.mdgraph-objects.mdlayouts-styling.mdplotly-express.md詳細
Plotly
インタラクティブで出版品質の可視化を、40以上のグラフタイプで作成できる Python グラフィックスライブラリです。
クイックスタート
Plotly をインストールします:
uv pip install plotly
Plotly Express(高レベル API)の基本的な使い方:
import plotly.express as px
import pandas as pd
df = pd.DataFrame({
'x': [1, 2, 3, 4],
'y': [10, 11, 12, 13]
})
fig = px.scatter(df, x='x', y='y', title='My First Plot')
fig.show()
API の選び方
Plotly Express (px) を使う
妥当なデフォルト設定で、手早く標準的な可視化を行う場合に使います:
- pandas の DataFrame を扱う場合
- 一般的なグラフタイプ(散布図、折れ線、棒、ヒストグラムなど)を作成する場合
- 色のエンコーディングや凡例を自動で付けたい場合
- コードを最小限(1〜5 行)にしたい場合
詳細なガイドは reference/plotly-express.md を参照してください。
Graph Objects (go) を使う
細かな制御やカスタムの可視化を行う場合に使います:
- Plotly Express にないグラフタイプ(3D メッシュ、等値面、複雑な金融チャートなど)を作成する場合
- 複数のトレースを含む複雑な図をゼロから構築する場合
- 個々の構成要素を正確に制御する必要がある場合
- カスタムの図形や注釈を使った特殊な可視化を作成する場合
詳細なガイドは reference/graph-objects.md を参照してください。
注: Plotly Express は graph objects の Figure を返すため、両方のアプローチを組み合わせることができます:
fig = px.scatter(df, x='x', y='y')
fig.update_layout(title='Custom Title') # Use go methods on px figure
fig.add_hline(y=10) # Add shapes
主な機能
1. チャートの種類
Plotly は 40 以上のチャートの種類をカテゴリ別に提供しています:
基本チャート: 散布図、折れ線、棒、円、面、バブル
統計チャート: ヒストグラム、箱ひげ図、バイオリン図、分布図、誤差線
科学チャート: ヒートマップ、等高線、三角座標、画像表示
金融チャート: ローソク足、OHLC、ウォーターフォール、ファネル、時系列
地図: スキャッターマップ、コロプレス図、密度マップ(地理的な可視化)
3D チャート: scatter3d、サーフェス、メッシュ、コーン、ボリューム
特殊なチャート: サンバースト、ツリーマップ、サンキー、平行座標、ゲージ
すべてのチャートの種類の詳細な例と使い方は reference/chart-types.md を参照してください。
2. レイアウトとスタイル
サブプロット: 軸を共有したマルチプロットの図を作成します:
from plotly.subplots import make_subplots
import plotly.graph_objects as go
fig = make_subplots(rows=2, cols=2, subplot_titles=('A', 'B', 'C', 'D'))
fig.add_trace(go.Scatter(x=[1, 2], y=[3, 4]), row=1, col=1)
テンプレート: 一貫したスタイルを適用します:
fig = px.scatter(df, x='x', y='y', template='plotly_dark')
# Built-in: plotly_white, plotly_dark, ggplot2, seaborn, simple_white
カスタマイズ: 見た目のあらゆる側面を制御できます:
- 色(離散的なカラーシーケンス、連続的なスケール)
- フォントとテキスト
- 軸(範囲、目盛り、グリッド)
- 凡例
- 余白とサイズ
- 注釈と図形
レイアウトとスタイルのオプションの全体については、reference/layouts-styling.md を参照してください。
3. インタラクティブ性
組み込みのインタラクティブ機能:
- カスタマイズ可能なデータを含むホバーツールチップ
- パンとズーム
- 凡例の切り替え
- 箱型選択と投げ縄選択
- 時系列用のレンジスライダー
- ボタンとドロップダウン
- アニメーション
# Custom hover template
fig.update_traces(
hovertemplate='<b>%{x}</b><br>Value: %{y:.2f}<extra></extra>'
)
# Add rangeslider
fig.update_xaxes(rangeslider_visible=True)
# Animations
fig = px.scatter(df, x='x', y='y', animation_frame='year')
インタラクティブ性の全ガイドは reference/export-interactivity.md を参照してください。
4. エクスポートオプション
インタラクティブな HTML:
fig.write_html('chart.html') # Full standalone
fig.write_html('chart.html', include_plotlyjs='cdn') # Smaller file
静的画像(kaleido が必要):
uv pip install kaleido
fig.write_image('chart.png') # PNG
fig.write_image('chart.pdf') # PDF
fig.write_image('chart.svg') # SVG
エクスポートオプションの全体については、reference/export-interactivity.md を参照してください。
一般的なワークフロー
科学データの可視化
import plotly.express as px
# Scatter plot with trendline
fig = px.scatter(df, x='temperature', y='yield', trendline='ols')
# Heatmap from matrix
fig = px.imshow(correlation_matrix, text_auto=True, color_continuous_scale='RdBu')
# 3D surface plot
import plotly.graph_objects as go
fig = go.Figure(data=[go.Surface(z=z_data, x=x_data, y=y_data)])
統計分析
# Distribution comparison
fig = px.histogram(df, x='values', color='group', marginal='box', nbins=30)
# Box plot with all points
fig = px.box(df, x='category', y='value', points='all')
# Violin plot
fig = px.violin(df, x='group', y='measurement', box=True)
時系列と金融
# Time series with rangeslider
fig = px.line(df, x='date', y='price')
fig.update_xaxes(rangeslider_visible=True)
# Candlestick chart
import plotly.graph_objects as go
fig = go.Figure(data=[go.Candlestick(
x=df['date'],
open=df['open'],
high=df['high'],
low=df['low'],
close=df['close']
)])
マルチプロットのダッシュボード
from plotly.subplots import make_subplots
import plotly.graph_objects as go
fig = make_subplots(
rows=2, cols=2,
subplot_titles=('Scatter', 'Bar', 'Histogram', 'Box'),
specs=[[{'type': 'scatter'}, {'type': 'bar'}],
[{'type': 'histogram'}, {'type': 'box'}]]
)
fig.add_trace(go.Scatter(x=[1, 2, 3], y=[4, 5, 6]), row=1, col=1)
fig.add_trace(go.Bar(x=['A', 'B'], y=[1, 2]), row=1, col=2)
fig.add_trace(go.Histogram(x=data), row=2, col=1)
fig.add_trace(go.Box(y=data), row=2, col=2)
fig.update_layout(height=800, showlegend=False)
Dash との統合
インタラクティブな Web アプリケーションには、Plotly の Web アプリフレームワークである Dash を使用します:
uv pip install dash
import dash
from dash import dcc, html
import plotly.express as px
app = dash.Dash(__name__)
fig = px.scatter(df, x='x', y='y')
app.layout = html.Div([
html.H1('Dashboard'),
dcc.Graph(figure=fig)
])
app.run_server(debug=True)
リファレンスファイル
- plotly-express.md - 手早い可視化のための高レベル API
- graph-objects.md - 細かな制御のための低レベル API
- chart-types.md - 40 以上のチャートの種類の一覧と例
- layouts-styling.md - サブプロット、テンプレート、色、カスタマイズ
- export-interactivity.md - エクスポートオプションとインタラクティブ機能
追加リソース
- 公式ドキュメント: https://plotly.com/python/
- API リファレンス: https://plotly.com/python-api-reference/
- コミュニティフォーラム: https://community.plotly.com/