Skill: Visualization
Use this skill whenever the user asks to:
- Plot, visualize, chart, or graph data.
- Create figures for EDA, reports, dashboards, or presentations.
- Modify or improve existing matplotlib / seaborn code.
- Save figures to disk for reports or notebooks.
Library
- Use matplotlib for charts.
- Use seaborn alongside matplotlib for theming, palettes, and statistical plots.
Global Setup (do this first)
At the start of any notebook or script that produces figures, set the theme once. font_scale enlarges all text elements proportionally, so individual fontsize= arguments are usually unnecessary.
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_theme(
style="whitegrid",
palette="muted",
font_scale=1.2,
)
Japanese text
If any text (titles, labels, legends, annotations) contains Japanese, configure a CJK-capable font, otherwise characters render as tofu (□□□).
Preferred approach (cross-platform):
import japanize_matplotlib # uv add japanize-matplotlib
Alternative (set an installed CJK font explicitly):
plt.rcParams["font.family"] = "Noto Sans CJK JP" # or "IPAexGothic", "Hiragino Sans", "Yu Gothic"
Figure Creation
- Do not use the stateful
plt.figure(...) / plt.plot(...) style.
- Always create figures and axes explicitly, and prefer
constrained_layout=True over tight_layout():
fig, ax = plt.subplots(figsize=(10, 6), constrained_layout=True)
- Use the object-oriented API:
ax.set_title(), ax.set_xlabel(), ax.plot(), etc.
figsize=(10, 6) is a reasonable default. Adjust to content: wide time series → (12, 4), square scatter → (6, 6), multi-panel → scale up accordingly.
Color Map / Color Palette
Choose by data type:
- Categorical:
"muted", "Set2", "colorblind" (seaborn palettes).
- Sequential (continuous):
"viridis", "cividis", "mako" — perceptually uniform.
- Diverging:
"coolwarm", "RdBu", "vlag".
- Forbidden:
"jet", "rainbow" — not perceptually uniform, poor for colorblind viewers.
Font Size
sns.set_theme(font_scale=1.2) covers most cases. Override per-element only when needed (e.g. a long title needs to be smaller, or one label needs emphasis):
ax.set_title("...", fontsize=18)
Do not repeat fontsize= on every call — it is redundant when font_scale is set.
Axis Scale Guidelines
- Bar charts: start y-axis at 0 (
ax.set_ylim(bottom=0)). Truncated bars are misleading.
- Line / scatter: do not force y-axis to 0 — it can hide meaningful variation. Let matplotlib autoscale, or set limits based on the data range.
- Log scale: use
ax.set_yscale("log") when data spans multiple orders of magnitude.
Legend
- Use
ax.legend() only when ≥2 series are plotted.
- If the auto-placement overlaps data, set explicitly:
ax.legend(loc="upper left") or place outside: ax.legend(loc="center left", bbox_to_anchor=(1.0, 0.5)).
Saving
- Save final figures under
outputs/figures/.
- Use the path utilities from
src/analysis_project/paths.py.
from analysis_project.paths import outputs_dir, ensure_parent_dir
# 出力パスを構成して保存
output_path = outputs_dir() / "figures" / "survival_by_class.png"
ensure_parent_dir(output_path)
fig.savefig(output_path, dpi=150)
- Use descriptive snake_case filenames:
monthly_sales_2024h1.png, not fig1.png.
- DPI guidance:
- Notebook / README / slides:
dpi=150
- Publication / print:
dpi=300
- Always call
plt.close(fig) after saving to free memory.
fig.savefig("outputs/figures/monthly_sales.png", dpi=150, bbox_inches="tight")
plt.close(fig)
Chart Quality Checklist
Before finalizing a chart, verify:
Example
from pathlib import Path
import matplotlib.pyplot as plt
import seaborn as sns
import japanize_matplotlib # noqa: F401 # 日本語フォント有効化
sns.set_theme(style="whitegrid", palette="muted", font_scale=1.2)
Path("outputs/figures").mkdir(parents=True, exist_ok=True)
fig, ax = plt.subplots(figsize=(10, 6), constrained_layout=True)
ax.bar(categories, values)
ax.set_title("月別売上推移 (2024年1月〜6月)")
ax.set_xlabel("月")
ax.set_ylabel("売上 (万円)")
ax.set_ylim(bottom=0) # 棒グラフは0起点
fig.savefig("outputs/figures/monthly_sales_2024h1.png", dpi=150, bbox_inches="tight")
plt.close(fig)
1---2name: visualization-23description: Use this when creating, modifying, reviewing, or saving charts, figures, plots, or visual summaries — including matplotlib/seaborn code, EDA figures, report figures, dashboards, or any task involving Japanese chart labels, color palettes, or figure styling.4metadata:5 source: https://skillmd.com/skills/gabrielmoreira/visualization-26---78# Skill: Visualization910Use this skill whenever the user asks to:1112- Plot, visualize, chart, or graph data.13- Create figures for EDA, reports, dashboards, or presentations.14- Modify or improve existing matplotlib / seaborn code.15- Save figures to disk for reports or notebooks.1617## Library1819- Use **matplotlib** for charts.20- Use **seaborn** alongside matplotlib for theming, palettes, and statistical plots.2122## Global Setup (do this first)2324At the start of any notebook or script that produces figures, set the theme once. `font_scale` enlarges all text elements proportionally, so individual `fontsize=` arguments are usually unnecessary.2526```python27import matplotlib.pyplot as plt28import seaborn as sns2930sns.set_theme(31 style="whitegrid",32 palette="muted",33 font_scale=1.2,34)35```3637### Japanese text3839If any text (titles, labels, legends, annotations) contains Japanese, configure a CJK-capable font, **otherwise characters render as tofu (□□□)**.4041Preferred approach (cross-platform):4243```python44import japanize_matplotlib # uv add japanize-matplotlib45```4647Alternative (set an installed CJK font explicitly):4849```python50plt.rcParams["font.family"] = "Noto Sans CJK JP" # or "IPAexGothic", "Hiragino Sans", "Yu Gothic"51```5253## Figure Creation5455- **Do not** use the stateful `plt.figure(...)` / `plt.plot(...)` style.56- **Always** create figures and axes explicitly, and prefer `constrained_layout=True` over `tight_layout()`:5758```python59fig, ax = plt.subplots(figsize=(10, 6), constrained_layout=True)60```6162- Use the **object-oriented** API: `ax.set_title()`, `ax.set_xlabel()`, `ax.plot()`, etc.63- `figsize=(10, 6)` is a reasonable default. Adjust to content: wide time series → `(12, 4)`, square scatter → `(6, 6)`, multi-panel → scale up accordingly.6465## Color Map / Color Palette6667Choose by data type:6869- **Categorical**: `"muted"`, `"Set2"`, `"colorblind"` (seaborn palettes).70- **Sequential (continuous)**: `"viridis"`, `"cividis"`, `"mako"` — perceptually uniform.71- **Diverging**: `"coolwarm"`, `"RdBu"`, `"vlag"`.72- **Forbidden**: `"jet"`, `"rainbow"` — not perceptually uniform, poor for colorblind viewers.7374## Font Size7576`sns.set_theme(font_scale=1.2)` covers most cases. Override per-element **only when needed** (e.g. a long title needs to be smaller, or one label needs emphasis):7778```python79ax.set_title("...", fontsize=18)80```8182Do **not** repeat `fontsize=` on every call — it is redundant when `font_scale` is set.8384## Axis Scale Guidelines8586- **Bar charts**: start y-axis at 0 (`ax.set_ylim(bottom=0)`). Truncated bars are misleading.87- **Line / scatter**: do **not** force y-axis to 0 — it can hide meaningful variation. Let matplotlib autoscale, or set limits based on the data range.88- **Log scale**: use `ax.set_yscale("log")` when data spans multiple orders of magnitude.8990## Legend9192- Use `ax.legend()` only when ≥2 series are plotted.93- If the auto-placement overlaps data, set explicitly: `ax.legend(loc="upper left")` or place outside: `ax.legend(loc="center left", bbox_to_anchor=(1.0, 0.5))`.9495## Saving9697- Save final figures under `outputs/figures/`.98- Use the path utilities from `src/analysis_project/paths.py`.99100```python101from analysis_project.paths import outputs_dir, ensure_parent_dir102103# 出力パスを構成して保存104output_path = outputs_dir() / "figures" / "survival_by_class.png"105ensure_parent_dir(output_path)106fig.savefig(output_path, dpi=150)107```108109- Use descriptive snake_case filenames: `monthly_sales_2024h1.png`, not `fig1.png`.110- DPI guidance:111 - Notebook / README / slides: `dpi=150`112 - Publication / print: `dpi=300`113- Always call `plt.close(fig)` after saving to free memory.114115```python116fig.savefig("outputs/figures/monthly_sales.png", dpi=150, bbox_inches="tight")117plt.close(fig)118```119120## Chart Quality Checklist121122Before finalizing a chart, verify:123124- [ ] **Title** clearly describes what the chart shows.125- [ ] **Axis labels** include units where applicable (e.g. "売上 (万円)", "Latency (ms)").126- [ ] **Font sizes** are readable (rely on `font_scale=1.2` as baseline).127- [ ] **Color palette** is perceptually uniform / colorblind-friendly (no jet/rainbow).128- [ ] **Date range** noted in title, subtitle, or annotation when relevant.129- [ ] **Sample / filter note** when data is subsetted (e.g. "n=1,234, 2024年1月〜6月").130- [ ] **Bar charts** start y-axis at 0; other chart types use sensible limits.131- [ ] **Legend** present when multiple series; placement does not overlap data.132- [ ] **Japanese text** renders correctly (japanize-matplotlib or CJK font configured).133- [ ] **Saved** to `outputs/figures/` with descriptive filename, followed by `plt.close(fig)`.134135## Example136137```python138from pathlib import Path139import matplotlib.pyplot as plt140import seaborn as sns141import japanize_matplotlib # noqa: F401 # 日本語フォント有効化142143sns.set_theme(style="whitegrid", palette="muted", font_scale=1.2)144145Path("outputs/figures").mkdir(parents=True, exist_ok=True)146147fig, ax = plt.subplots(figsize=(10, 6), constrained_layout=True)148149ax.bar(categories, values)150ax.set_title("月別売上推移 (2024年1月〜6月)")151ax.set_xlabel("月")152ax.set_ylabel("売上 (万円)")153ax.set_ylim(bottom=0) # 棒グラフは0起点154155fig.savefig("outputs/figures/monthly_sales_2024h1.png", dpi=150, bbox_inches="tight")156plt.close(fig)157```