📊data-visualization
- プラグイン
- Data
- ソース
- GitHub で見る ↗
説明
Pythonを使用して効果的なデータビジュアライゼーションを作成します(matplotlib、seaborn、plotly対応)。 次のような場合に使用: グラフや図の作成、データセットに適したチャートタイプの選定、出版品質の図表の生成、またはアクセシビリティやカラーセオリーといったデザイン原則の適用が必要なとき。
原文を表示
Create effective data visualizations with Python (matplotlib, seaborn, plotly). Use when building charts, choosing the right chart type for a dataset, creating publication-quality figures, or applying design principles like accessibility and color theory.
ユースケース
- ✓グラフや図を作成するとき
- ✓データセットに適したチャートタイプを選定するとき
- ✓出版品質の図表を生成するとき
- ✓デザイン原則を適用した可視化が必要なとき
本文(日本語訳)
データビジュアライゼーション スキル
チャートの選択ガイダンス、Pythonによるビジュアライゼーションのコードパターン、デザイン原則、および効果的なデータビジュアライゼーション作成のためのアクセシビリティに関する考慮事項。
チャート選択ガイド
データの関係性に基づいて選ぶ
| 表現したい内容 | 最適なチャート | 代替候補 |
|---|---|---|
| 時系列トレンド | 折れ線グラフ | エリアチャート(累積や構成比を示す場合) |
| カテゴリ間の比較 | 縦棒グラフ | 横棒グラフ(カテゴリが多い場合)、ロリポップチャート |
| ランキング | 横棒グラフ | ドットプロット、スロープチャート(2時点の比較) |
| 全体に占める構成比 | 積み上げ棒グラフ | ツリーマップ(階層構造)、ワッフルチャート |
| 時系列での構成変化 | 積み上げエリアチャート | 100%積み上げ棒グラフ(割合に注目する場合) |
| 分布 | ヒストグラム | 箱ひげ図(グループ比較)、バイオリンプロット、ストリッププロット |
| 相関(2変数) | 散布図 | バブルチャート(サイズで第3の変数を追加) |
| 相関(多変数) | ヒートマップ(相関行列) | ペアプロット |
| **地理的パターン | コロプレス地図 | バブルマップ、ヘックスマップ |
| フロー/プロセス | サンキーダイアグラム | ファネルチャート(連続するステージ) |
| 関係性ネットワーク | ネットワークグラフ | コードダイアグラム |
| 目標値との対比 | ブレットチャート | ゲージ(単一KPIのみ) |
| 複数KPIの同時表示 | スモールマルチプル | 個別チャートを並べたダッシュボード |
使うべきでないチャート
- 円グラフ: カテゴリが6未満で、厳密な割合よりも大まかな比較で十分な場合を除き使用を避ける。人間は角度の比較が苦手。代わりに棒グラフを使用すること。
- 3Dチャート: 絶対に使用しない。視覚的な歪みが生じ、情報量も増えない。
- 2軸チャート: 慎重に使用すること。相関があるかのような誤解を招く可能性がある。使用する場合は両軸に明確なラベルを付けること。
- 積み上げ棒グラフ(カテゴリが多い場合): 中間セグメントの比較が困難になる。スモールマルチプルやグループ棒グラフを代わりに使用すること。
- ドーナツチャート: 円グラフよりやや見やすいが、根本的な問題は同じ。せいぜい単一KPIの表示に留めること。
Python ビジュアライゼーション コードパターン
セットアップとスタイル
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import seaborn as sns
import pandas as pd
import numpy as np
# プロフェッショナルなスタイル設定
plt.style.use('seaborn-v0_8-whitegrid')
plt.rcParams.update({
'figure.figsize': (10, 6),
'figure.dpi': 150,
'font.size': 11,
'axes.titlesize': 14,
'axes.titleweight': 'bold',
'axes.labelsize': 11,
'xtick.labelsize': 10,
'ytick.labelsize': 10,
'legend.fontsize': 10,
'figure.titlesize': 16,
})
# 色覚異常対応のカラーパレット
PALETTE_CATEGORICAL = ['#4C72B0', '#DD8452', '#55A868', '#C44E52', '#8172B3', '#937860']
PALETTE_SEQUENTIAL = 'YlOrRd'
PALETTE_DIVERGING = 'RdBu_r'
折れ線グラフ(時系列)
fig, ax = plt.subplots(figsize=(10, 6))
for label, group in df.groupby('category'):
ax.plot(group['date'], group['value'], label=label, linewidth=2)
ax.set_title('カテゴリ別 指標トレンド', fontweight='bold')
ax.set_xlabel('日付')
ax.set_ylabel('値')
ax.legend(loc='upper left', frameon=True)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
# X軸の日付フォーマット
fig.autofmt_xdate()
plt.tight_layout()
plt.savefig('trend_chart.png', dpi=150, bbox_inches='tight')
棒グラフ(比較)
fig, ax = plt.subplots(figsize=(10, 6))
# 読みやすいよう値でソート
df_sorted = df.sort_values('metric', ascending=True)
bars = ax.barh(df_sorted['category'], df_sorted['metric'], color=PALETTE_CATEGORICAL[0])
# 値ラベルを追加
for bar in bars:
width = bar.get_width()
ax.text(width + 0.5, bar.get_y() + bar.get_height()/2,
f'{width:,.0f}', ha='left', va='center', fontsize=10)
ax.set_title('カテゴリ別 指標(ランキング順)', fontweight='bold')
ax.set_xlabel('指標値')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.tight_layout()
plt.savefig('bar_chart.png', dpi=150, bbox_inches='tight')
ヒストグラム(分布)
fig, ax = plt.subplots(figsize=(10, 6))
ax.hist(df['value'], bins=30, color=PALETTE_CATEGORICAL[0], edgecolor='white', alpha=0.8)
# 平均・中央値のラインを追加
mean_val = df['value'].mean()
median_val = df['value'].median()
ax.axvline(mean_val, color='red', linestyle='--', linewidth=1.5, label=f'平均: {mean_val:,.1f}')
ax.axvline(median_val, color='green', linestyle='--', linewidth=1.5, label=f'中央値: {median_val:,.1f}')
ax.set_title('値の分布', fontweight='bold')
ax.set_xlabel('値')
ax.set_ylabel('頻度')
ax.legend()
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.tight_layout()
plt.savefig('histogram.png', dpi=150, bbox_inches='tight')
ヒートマップ
fig, ax = plt.subplots(figsize=(10, 8))
# ヒートマップ形式にデータをピボット
pivot = df.pivot_table(index='row_dim', columns='col_dim', values='metric', aggfunc='sum')
sns.heatmap(pivot, annot=True, fmt=',.0f', cmap='YlOrRd',
linewidths=0.5, ax=ax, cbar_kws={'label': '指標値'})
ax.set_title('行ディメンション × 列ディメンション 別 指標', fontweight='bold')
ax.set_xlabel('列ディメンション')
ax.set_ylabel('行ディメンション')
plt.tight_layout()
plt.savefig('heatmap.png', dpi=150, bbox_inches='tight')
スモールマルチプル
categories = df['category'].unique()
n_cats = len(categories)
n_cols = min(3, n_cats)
n_rows = (n_cats + n_cols - 1) // n_cols
fig, axes = plt.subplots(n_rows, n_cols, figsize=(5*n_cols, 4*n_rows), sharex=True, sharey=True)
axes = axes.flatten() if n_cats > 1 else [axes]
for i, cat in enumerate(categories):
ax = axes[i]
subset = df[df['category'] == cat]
ax.plot(subset['date'], subset['value'], color=PALETTE_CATEGORICAL[i % len(PALETTE_CATEGORICAL)])
ax.set_title(cat, fontsize=12)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
# 空のサブプロットを非表示
for j in range(i+1, len(axes)):
axes[j].set_visible(False)
fig.suptitle('カテゴリ別トレンド', fontsize=14, fontweight='bold', y=1.02)
plt.tight_layout()
plt.savefig('small_multiples.png', dpi=150, bbox_inches='tight')
数値フォーマットのヘルパー関数
def format_number(val, format_type='number'):
"""チャートラベル用に数値をフォーマットする。"""
if format_type == 'currency':
if abs(val) >= 1e9:
return f'${val/1e9:.1f}B'
elif abs(val) >= 1e6:
return f'${val/1e6:.1f}M'
elif abs(val) >= 1e3:
return f'${val/1e3:.1f}K'
else:
return f'${val:,.0f}'
elif format_type == 'percent':
return f'{val:.1f}%'
elif format_type == 'number':
if abs(val) >= 1e9:
return f'{val/1e9:.1f}B'
elif abs(val) >= 1e6:
return f'{val/1e6:.1f}M'
elif abs(val) >= 1e3:
return f'{val/1e3:.1f}K'
else:
return f'{val:,.0f}'
return str(val)
# 軸フォーマッターでの使用例
ax.yaxis.set_major_formatter(mticker.FuncFormatter(lambda x, p: format_number(x, 'currency')))
Plotly によるインタラクティブチャート
import plotly.express as px
import plotly.graph_objects as go
# シンプルなインタラクティブ折れ線グラフ
fig = px.line(df, x='date', y='value', color='category',
title='インタラクティブ 指標トレンド',
labels={'value': '指標値', 'date': '日付'})
fig.update_layout(hovermode='x unified')
fig.write_html('interactive_chart.html')
fig.show()
# ホバーデータ付きインタラクティブ散布図
fig = px.scatter(df, x='metric_a', y='metric_b', color='category',
size='size_metric', hover_data=['name', 'detail_field'],
title='相関分析')
fig.show()
デザイン原則
カラー
- 意図を持って色を使う: 色はデータをエンコードするためのものであり、装飾のためではない
- ストーリーを強調する: キーとなるインサイトには明るいアクセントカラーを使い、それ以外はグレーにする
- 順序データ: 順序のある値には単色のグラデーション(明→暗)を使用する
- 発散データ: 意味のある中心点を持つデータには、ニュートラルな中間点を持つ2色のグラデーションを使用する
- カテゴリデータ: 明確に異なる色相を使用し、6〜8色を超えると混乱を招く
- 赤と緑だけは避ける: 男性の8%は赤緑色盲。青とオレンジをメインの組み合わせとして使用する
タイポグラフィ
- タイトルにインサイトを書く: 「月次売上」より「売上が前年比23%増加」の方が優れている
- サブタイトルで文脈を補足する: 期間、適用フィルター、データソースを記載する
- 軸ラベルは読みやすく: 可能な限り90度回転させない。短縮するか折り返すこと
- データラベルで精度を上げる: すべての棒ではなく、重要なポイントにのみ使用する
- アノテーションで強調する: 特定のポイントをテキストで注釈として呼び出す
レイアウト
- チャートジャンクを減らす: 情報を持たないグリッドライン・枠線・背景は取り除く
- 意味のある順序でソートする: カテゴリは値でソートする(自然な順序がある場合—月・ステージなど—を除き、アルファベット順にしない)
- 適切なアスペクト比: 時系列は横長(3:1〜2:1)に、比較チャートはより正方形に近くてもよい
- 余白は有効活用する: チャートを詰め込みすぎない。各ビジュアライゼーションに十分な空間を与えること
正確性
- 棒グラフはゼロから始める: 常に。95〜100の棒グラフは5%の差を誇張して見せてしまう
- 折れ線グラフはゼロ以外のベースラインも可: 変動の範囲が意味を持つ場合
- 複数パネル間のスケールを統一する: 複数の
原文(English)を表示
Data Visualization Skill
Chart selection guidance, Python visualization code patterns, design principles, and accessibility considerations for creating effective data visualizations.
Chart Selection Guide
Choose by Data Relationship
| What You're Showing | Best Chart | Alternatives |
|---|---|---|
| Trend over time | Line chart | Area chart (if showing cumulative or composition) |
| Comparison across categories | Vertical bar chart | Horizontal bar (many categories), lollipop chart |
| Ranking | Horizontal bar chart | Dot plot, slope chart (comparing two periods) |
| Part-to-whole composition | Stacked bar chart | Treemap (hierarchical), waffle chart |
| Composition over time | Stacked area chart | 100% stacked bar (for proportion focus) |
| Distribution | Histogram | Box plot (comparing groups), violin plot, strip plot |
| Correlation (2 variables) | Scatter plot | Bubble chart (add 3rd variable as size) |
| Correlation (many variables) | Heatmap (correlation matrix) | Pair plot |
| Geographic patterns | Choropleth map | Bubble map, hex map |
| Flow / process | Sankey diagram | Funnel chart (sequential stages) |
| Relationship network | Network graph | Chord diagram |
| Performance vs. target | Bullet chart | Gauge (single KPI only) |
| Multiple KPIs at once | Small multiples | Dashboard with separate charts |
When NOT to Use Certain Charts
- Pie charts: Avoid unless <6 categories and exact proportions matter less than rough comparison. Humans are bad at comparing angles. Use bar charts instead.
- 3D charts: Never. They distort perception and add no information.
- Dual-axis charts: Use cautiously. They can mislead by implying correlation. Clearly label both axes if used.
- Stacked bar (many categories): Hard to compare middle segments. Use small multiples or grouped bars instead.
- Donut charts: Slightly better than pie charts but same fundamental issues. Use for single KPI display at most.
Python Visualization Code Patterns
Setup and Style
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import seaborn as sns
import pandas as pd
import numpy as np
# Professional style setup
plt.style.use('seaborn-v0_8-whitegrid')
plt.rcParams.update({
'figure.figsize': (10, 6),
'figure.dpi': 150,
'font.size': 11,
'axes.titlesize': 14,
'axes.titleweight': 'bold',
'axes.labelsize': 11,
'xtick.labelsize': 10,
'ytick.labelsize': 10,
'legend.fontsize': 10,
'figure.titlesize': 16,
})
# Colorblind-friendly palettes
PALETTE_CATEGORICAL = ['#4C72B0', '#DD8452', '#55A868', '#C44E52', '#8172B3', '#937860']
PALETTE_SEQUENTIAL = 'YlOrRd'
PALETTE_DIVERGING = 'RdBu_r'
Line Chart (Time Series)
fig, ax = plt.subplots(figsize=(10, 6))
for label, group in df.groupby('category'):
ax.plot(group['date'], group['value'], label=label, linewidth=2)
ax.set_title('Metric Trend by Category', fontweight='bold')
ax.set_xlabel('Date')
ax.set_ylabel('Value')
ax.legend(loc='upper left', frameon=True)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
# Format dates on x-axis
fig.autofmt_xdate()
plt.tight_layout()
plt.savefig('trend_chart.png', dpi=150, bbox_inches='tight')
Bar Chart (Comparison)
fig, ax = plt.subplots(figsize=(10, 6))
# Sort by value for easy reading
df_sorted = df.sort_values('metric', ascending=True)
bars = ax.barh(df_sorted['category'], df_sorted['metric'], color=PALETTE_CATEGORICAL[0])
# Add value labels
for bar in bars:
width = bar.get_width()
ax.text(width + 0.5, bar.get_y() + bar.get_height()/2,
f'{width:,.0f}', ha='left', va='center', fontsize=10)
ax.set_title('Metric by Category (Ranked)', fontweight='bold')
ax.set_xlabel('Metric Value')
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.tight_layout()
plt.savefig('bar_chart.png', dpi=150, bbox_inches='tight')
Histogram (Distribution)
fig, ax = plt.subplots(figsize=(10, 6))
ax.hist(df['value'], bins=30, color=PALETTE_CATEGORICAL[0], edgecolor='white', alpha=0.8)
# Add mean and median lines
mean_val = df['value'].mean()
median_val = df['value'].median()
ax.axvline(mean_val, color='red', linestyle='--', linewidth=1.5, label=f'Mean: {mean_val:,.1f}')
ax.axvline(median_val, color='green', linestyle='--', linewidth=1.5, label=f'Median: {median_val:,.1f}')
ax.set_title('Distribution of Values', fontweight='bold')
ax.set_xlabel('Value')
ax.set_ylabel('Frequency')
ax.legend()
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.tight_layout()
plt.savefig('histogram.png', dpi=150, bbox_inches='tight')
Heatmap
fig, ax = plt.subplots(figsize=(10, 8))
# Pivot data for heatmap format
pivot = df.pivot_table(index='row_dim', columns='col_dim', values='metric', aggfunc='sum')
sns.heatmap(pivot, annot=True, fmt=',.0f', cmap='YlOrRd',
linewidths=0.5, ax=ax, cbar_kws={'label': 'Metric Value'})
ax.set_title('Metric by Row Dimension and Column Dimension', fontweight='bold')
ax.set_xlabel('Column Dimension')
ax.set_ylabel('Row Dimension')
plt.tight_layout()
plt.savefig('heatmap.png', dpi=150, bbox_inches='tight')
Small Multiples
categories = df['category'].unique()
n_cats = len(categories)
n_cols = min(3, n_cats)
n_rows = (n_cats + n_cols - 1) // n_cols
fig, axes = plt.subplots(n_rows, n_cols, figsize=(5*n_cols, 4*n_rows), sharex=True, sharey=True)
axes = axes.flatten() if n_cats > 1 else [axes]
for i, cat in enumerate(categories):
ax = axes[i]
subset = df[df['category'] == cat]
ax.plot(subset['date'], subset['value'], color=PALETTE_CATEGORICAL[i % len(PALETTE_CATEGORICAL)])
ax.set_title(cat, fontsize=12)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
# Hide empty subplots
for j in range(i+1, len(axes)):
axes[j].set_visible(False)
fig.suptitle('Trends by Category', fontsize=14, fontweight='bold', y=1.02)
plt.tight_layout()
plt.savefig('small_multiples.png', dpi=150, bbox_inches='tight')
Number Formatting Helpers
def format_number(val, format_type='number'):
"""Format numbers for chart labels."""
if format_type == 'currency':
if abs(val) >= 1e9:
return f'${val/1e9:.1f}B'
elif abs(val) >= 1e6:
return f'${val/1e6:.1f}M'
elif abs(val) >= 1e3:
return f'${val/1e3:.1f}K'
else:
return f'${val:,.0f}'
elif format_type == 'percent':
return f'{val:.1f}%'
elif format_type == 'number':
if abs(val) >= 1e9:
return f'{val/1e9:.1f}B'
elif abs(val) >= 1e6:
return f'{val/1e6:.1f}M'
elif abs(val) >= 1e3:
return f'{val/1e3:.1f}K'
else:
return f'{val:,.0f}'
return str(val)
# Usage with axis formatter
ax.yaxis.set_major_formatter(mticker.FuncFormatter(lambda x, p: format_number(x, 'currency')))
Interactive Charts with Plotly
import plotly.express as px
import plotly.graph_objects as go
# Simple interactive line chart
fig = px.line(df, x='date', y='value', color='category',
title='Interactive Metric Trend',
labels={'value': 'Metric Value', 'date': 'Date'})
fig.update_layout(hovermode='x unified')
fig.write_html('interactive_chart.html')
fig.show()
# Interactive scatter with hover data
fig = px.scatter(df, x='metric_a', y='metric_b', color='category',
size='size_metric', hover_data=['name', 'detail_field'],
title='Correlation Analysis')
fig.show()
Design Principles
Color
- Use color purposefully: Color should encode data, not decorate
- Highlight the story: Use a bright accent color for the key insight; grey everything else
- Sequential data: Use a single-hue gradient (light to dark) for ordered values
- Diverging data: Use a two-hue gradient with neutral midpoint for data with a meaningful center
- Categorical data: Use distinct hues, maximum 6-8 before it gets confusing
- Avoid red/green only: 8% of men are red-green colorblind. Use blue/orange as primary pair
Typography
- Title states the insight: "Revenue grew 23% YoY" beats "Revenue by Month"
- Subtitle adds context: Date range, filters applied, data source
- Axis labels are readable: Never rotated 90 degrees if avoidable. Shorten or wrap instead
- Data labels add precision: Use on key points, not every single bar
- Annotation highlights: Call out specific points with text annotations
Layout
- Reduce chart junk: Remove gridlines, borders, backgrounds that don't carry information
- Sort meaningfully: Categories sorted by value (not alphabetically) unless there's a natural order (months, stages)
- Appropriate aspect ratio: Time series wider than tall (3:1 to 2:1); comparisons can be squarer
- White space is good: Don't cram charts together. Give each visualization room to breathe
Accuracy
- Bar charts start at zero: Always. A bar from 95 to 100 exaggerates a 5% difference
- Line charts can have non-zero baselines: When the range of variation is meaningful
- Consistent scales across panels: When comparing multiple charts, use the same axis range
- Show uncertainty: Error bars, confidence intervals, or ranges when data is uncertain
- Label your axes: Never make the reader guess what the numbers mean
Accessibility Considerations
Color Blindness
- Never rely on color alone to distinguish data series
- Add pattern fills, different line styles (solid, dashed, dotted), or direct labels
- Test with a colorblind simulator (e.g., Coblis, Sim Daltonism)
- Use the colorblind-friendly palette:
sns.color_palette("colorblind")
Screen Readers
- Include alt text describing the chart's key finding
- Provide a data table alternative alongside the visualization
- Use semantic titles and labels
General Accessibility
- Sufficient contrast between data elements and background
- Text size minimum 10pt for labels, 12pt for titles
- Avoid conveying information only through spatial position (add labels)
- Consider printing: does the chart work in black and white?
Accessibility Checklist
Before sharing a visualization:
- [ ] Chart works without color (patterns, labels, or line styles differentiate series)
- [ ] Text is readable at standard zoom level
- [ ] Title describes the insight, not just the data
- [ ] Axes are labeled with units
- [ ] Legend is clear and positioned without obscuring data
- [ ] Data source and date range are noted
原文・著作権は Anthropic および各プラグイン作者に帰属します。日本語訳は Claude API による自動翻訳です。