| name | python-dataviz |
| description | Professional data visualization using Python (matplotlib, seaborn, plotly). Create publication-quality static charts, statistical visualizations, and interactive plots. Use when generating charts/graphs/plots from data, creating infographics with data components, or producing scientific/statistical visualizations. Supports PNG/SVG (static) and HTML (interactive) export. |
Python Data Visualization
Create professional charts, graphs, and statistical visualizations using Python's leading libraries.
Libraries & Use Cases
matplotlib - Static plots, publication-quality, full control
- Bar, line, scatter, pie, histogram, heatmap
- Multi-panel figures, subplots
- Custom styling, annotations
- Export: PNG, SVG, PDF
seaborn - Statistical visualizations, beautiful defaults
- Distribution plots (violin, box, kde, histogram)
- Categorical plots (bar, count, swarm, box)
- Relationship plots (scatter, line, regression)
- Matrix plots (heatmap, clustermap)
- Built on matplotlib, integrates seamlessly
plotly - Interactive charts, web-friendly
- Hover tooltips, zoom, pan
- 3D plots, animations
- Dashboards via Dash framework
- Export: HTML, PNG (requires kaleido)
Quick Start
Setup Environment
cd .sensenova-claw/skills/python-dataviz
python3 -m venv .venv
source .venv/bin/activate
pip install .
Create a Chart
⚠️ 图中包含任何中文(标题 / 坐标轴 / 图例 / 标签 / 注释)时,必须在 import matplotlib.pyplot 之后立刻设置中文字体,否则会渲染成 □□□ 豆腐块。
import matplotlib
import matplotlib.pyplot as plt
matplotlib.rcParams['font.sans-serif'] = [
'PingFang SC', 'Heiti SC', 'Arial Unicode MS',
'Microsoft YaHei', 'SimHei',
'Noto Sans CJK SC', 'WenQuanYi Micro Hei',
'DejaVu Sans',
]
matplotlib.rcParams['axes.unicode_minus'] = False
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.figure(figsize=(10, 6))
plt.plot(x, y, linewidth=2, color='#667eea')
plt.title('Sine Wave', fontsize=16, fontweight='bold')
plt.xlabel('X Axis')
plt.ylabel('Y Axis')
plt.grid(alpha=0.3)
plt.tight_layout()
plt.savefig('output.png', dpi=300, bbox_inches='tight')
plt.savefig('output.svg', bbox_inches='tight')
若使用 seaborn,同样在 sns.set_theme() / sns.set_style() 之前或之后执行上述 rcParams 设置都生效(seaborn 只接管样式,不覆盖字体族)。
Chart Selection Guide
Distribution/Statistical:
- Histogram →
plt.hist() or sns.histplot()
- Box plot →
sns.boxplot()
- Violin plot →
sns.violinplot()
- KDE →
sns.kdeplot()
Comparison:
- Bar chart →
plt.bar() or sns.barplot()
- Grouped bar →
sns.barplot(hue=...)
- Horizontal bar →
plt.barh() or sns.barplot(orient='h')
Relationship:
- Scatter →
plt.scatter() or sns.scatterplot()
- Line →
plt.plot() or sns.lineplot()
- Regression →
sns.regplot() or sns.lmplot()
Heatmaps:
- Correlation matrix →
sns.heatmap(df.corr())
- 2D data →
plt.imshow() or sns.heatmap()
Interactive:
- Any plotly chart →
plotly.express or plotly.graph_objects
- See
scripts/interactive.py for examples
Best Practices
1. Figure Size & DPI
plt.figure(figsize=(10, 6))
plt.savefig('output.png', dpi=300)
2. Color Palettes
import seaborn as sns
sns.set_palette("husl")
sns.set_palette("muted")
sns.set_palette("deep")
colors = ['#667eea', '#764ba2', '#f6ad55', '#4299e1']
3. Styling
import seaborn as sns
sns.set_theme()
sns.set_style("whitegrid")
plt.style.use('ggplot')
4. Multiple Subplots
fig, axes = plt.subplots(2, 2, figsize=(12, 10))
axes[0, 0].plot(x, y1)
axes[0, 1].plot(x, y2)
plt.tight_layout()
5. Export Formats
plt.savefig('chart.png', dpi=300, bbox_inches='tight', transparent=False)
plt.savefig('chart.svg', bbox_inches='tight')
import plotly.express as px
fig = px.scatter(df, x='col1', y='col2')
fig.write_html('chart.html')
Advanced Topics
See references/ for detailed guides:
- Color theory & palettes: references/colors.md
- Statistical plots: references/statistical.md
Example Scripts
See scripts/ for ready-to-use examples:
scripts/bar_chart.py - Bar and grouped bar charts
scripts/line_chart.py - Line plots with multiple series
scripts/scatter_plot.py - Scatter plots with regression
scripts/heatmap.py - Correlation heatmaps
scripts/distribution.py - Histograms, KDE, violin plots
scripts/interactive.py - Plotly interactive charts
Common Patterns
Data from CSV
import pandas as pd
df = pd.read_csv('data.csv')
df.plot(x='date', y='value', kind='line', figsize=(10, 6))
plt.savefig('output.png', dpi=300)
sns.lineplot(data=df, x='date', y='value')
plt.savefig('output.png', dpi=300)
Dictionary Data
data = {'Category A': 25, 'Category B': 40, 'Category C': 15}
plt.bar(data.keys(), data.values())
plt.savefig('output.png', dpi=300)
import pandas as pd
df = pd.DataFrame(list(data.items()), columns=['Category', 'Value'])
sns.barplot(data=df, x='Category', y='Value')
plt.savefig('output.png', dpi=300)
NumPy Arrays
import numpy as np
x = np.linspace(0, 10, 100)
y = np.sin(x)
plt.plot(x, y)
plt.savefig('output.png', dpi=300)
Troubleshooting
"No module named matplotlib"
cd .sensenova-claw/skills/python-dataviz
source .venv/bin/activate
pip install .
Blank output / "Figure is empty"
- Check that
plt.savefig() comes AFTER plotting commands
- Use
plt.show() for interactive viewing during development
Labels cut off
plt.tight_layout()
plt.savefig('output.png', bbox_inches='tight')
Low resolution output
plt.savefig('output.png', dpi=300)
中文显示成 □□□ / tofu 方块
matplotlib 默认字体 DejaVu Sans 不包含 CJK 字形,必须显式配置中文字体。见上文 "Create a Chart" 段落开头的 rcParams['font.sans-serif'] 设置。
排查:python -c "import matplotlib.font_manager as fm; print([f.name for f in fm.fontManager.ttflist if 'Hei' in f.name or 'Ping' in f.name or 'CJK' in f.name])" 看本机实际有哪些中文字体,把存在的那几个放在回退列表最前。
Environment
The skill includes a venv with all dependencies. Always activate before use:
cd .sensenova-claw/skills/python-dataviz
source .venv/bin/activate
Dependencies: matplotlib, seaborn, plotly, pandas, numpy, kaleido (for plotly static export)