关于 data-visualization
这项数据可视化技能提供探索性分析、统计图表、交互式图表和仪表板的示例与指导,适合使用 Python 数据并通过图形呈现规律的场景。
主要功能
- 01使用 Matplotlib 创建图表
- 02使用 Seaborn 绘制统计图
- 03通过汇总检查探索数据
- 04使用 Plotly 制作交互图表
- 05使用 Plotly Dash 创建仪表板
- 06导出 PNG、PDF 和 SVG
适用场景
- 01检查数据集的分布和关系
- 02呈现时间序列或类别对比
- 03构建交互式数据仪表板
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ pluginagentmarketplace/ custom-plugin-ai-data-scientist --skill data-visualization安装后在 Claude Code 中输入/data-visualization确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
assets/chart_styles.yamlreferences/CHART_SELECTION_GUIDE.mdscripts/quick_plot.py详细介绍
数据可视化
创建引人入胜的可视化图表,探索并传达数据洞察。
快速入门
Matplotlib 基础
import matplotlib.pyplot as plt
# Line plot
plt.figure(figsize=(10, 6))
plt.plot(x, y, marker='o', linestyle='-', color='blue', label='Series 1')
plt.xlabel('X Label')
plt.ylabel('Y Label')
plt.title('Title')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
# Bar chart
plt.bar(categories, values, color='skyblue', edgecolor='black')
plt.xlabel('Categories')
plt.ylabel('Values')
plt.xticks(rotation=45)
plt.tight_layout()
plt.show()
使用 Seaborn 绘制统计图表
import seaborn as sns
# Set style
sns.set_style("whitegrid")
# Distribution
sns.histplot(data=df, x='value', kde=True, bins=30)
# Box plot
sns.boxplot(data=df, x='category', y='value')
# Violin plot
sns.violinplot(data=df, x='category', y='value')
# Heatmap
corr = df.corr()
sns.heatmap(corr, annot=True, cmap='coolwarm', center=0)
# Pairplot
sns.pairplot(df, hue='target', diag_kind='kde')
探索性数据分析
# Quick overview
df.info()
df.describe()
# Missing values
df.isnull().sum()
# Value counts
df['category'].value_counts().plot(kind='bar')
# Distribution
df.hist(figsize=(12, 10), bins=30)
plt.tight_layout()
plt.show()
# Correlation matrix
plt.figure(figsize=(10, 8))
sns.heatmap(df.corr(), annot=True, cmap='coolwarm',
center=0, square=True)
plt.title('Correlation Matrix')
plt.show()
使用 Plotly 创建交互式可视化
import plotly.express as px
import plotly.graph_objects as go
# Interactive scatter
fig = px.scatter(df, x='feature1', y='target',
color='category', size='value',
hover_data=['name', 'date'],
title='Interactive Scatter Plot')
fig.show()
# Time series
fig = px.line(df, x='date', y='value', color='category',
title='Time Series')
fig.update_xaxes(rangeslider_visible=True)
fig.show()
# 3D scatter
fig = px.scatter_3d(df, x='x', y='y', z='z',
color='category', size='value')
fig.show()
使用 Plotly Dash 创建仪表板
import dash
from dash import dcc, html
from dash.dependencies import Input, Output
app = dash.Dash(__name__)
app.layout = html.Div([
html.H1('Sales Dashboard'),
dcc.Dropdown(
id='category-dropdown',
options=[{'label': cat, 'value': cat}
for cat in df['category'].unique()],
value=df['category'].unique()[0]
),
dcc.Graph(id='sales-graph'),
dcc.RangeSlider(
id='year-slider',
min=df['year'].min(),
max=df['year'].max(),
value=[df['year'].min(), df['year'].max()],
marks={str(year): str(year)
for year in df['year'].unique()}
)
])
@app.callback(
Output('sales-graph', 'figure'),
[Input('category-dropdown', 'value'),
Input('year-slider', 'value')]
)
def update_graph(selected_category, year_range):
filtered_df = df[
(df['category'] == selected_category) &
(df['year'] >= year_range[0]) &
(df['year'] <= year_range[1])
]
fig = px.line(filtered_df, x='date', y='sales')
return fig
if __name__ == '__main__':
app.run_server(debug=True)
子图
fig, axes = plt.subplots(2, 2, figsize=(12, 10))
# Top left
axes[0, 0].hist(data1, bins=30)
axes[0, 0].set_title('Histogram')
# Top right
axes[0, 1].scatter(x, y)
axes[0, 1].set_title('Scatter')
# Bottom left
axes[1, 0].plot(x, y)
axes[1, 0].set_title('Line Plot')
# Bottom right
axes[1, 1].boxplot([data1, data2, data3])
axes[1, 1].set_title('Box Plot')
plt.tight_layout()
plt.show()
可视化最佳实践
-
选择合适的图表类型:
- 比较:柱状图
- 分布:直方图、箱线图
- 关系:散点图
- 时间序列:折线图
- 构成:饼图、堆叠柱状图
-
设计原则:
- 标签和标题清晰
- 配色方案恰当
- 去除图表中的冗余元素
- 格式保持一致
- 注重无障碍访问(适合色盲用户)
-
需要避免的常见误区:
- 误导性的坐标轴(基线不为零)
- 使用过多颜色
- 使用 3D 图表(会扭曲感知)
- 饼图类别过多
- 使用双 Y 轴(容易造成混淆)
调色板
# Seaborn palettes
sns.color_palette("viridis", as_cmap=True)
sns.color_palette("coolwarm", as_cmap=True)
sns.color_palette("Set2")
# Custom colors
colors = ['#FF6B6B', '#4ECDC4', '#45B7D1', '#FFA07A']
导出图表
# High-resolution PNG
plt.savefig('figure.png', dpi=300, bbox_inches='tight')
# Vector format (PDF, SVG)
plt.savefig('figure.pdf', bbox_inches='tight')
plt.savefig('figure.svg', bbox_inches='tight')
评论
支持 Markdown