name: data-visualization description: 使用 matplotlib、seaborn 和 plotly,从结构化数据创建清晰、有效的图表与仪表盘。 license: MIT metadata: author: awesome-ai-agent-skills version: 1.0.0
本技能让 AI Agent 将结构化数据转化为有意义的可视化呈现。Agent 根据数据和所提问题选择合适的图表类型,使用 matplotlib 和 seaborn 构建出版质量的静态图表,并使用 plotly 创建交互式可视化。它遵循成熟的数据可视化原则,以确保清晰、准确与美观。
理解数据与问题。 检查数据集的结构——有多少变量、什么类型(数值、类别、时间),以及用户想突出什么关系或对比。问题比数据本身更能驱动图表选择。
选择合适的图表类型。 将分析目标匹配到正确的视觉形式。类别对比用条形图,随时间变化的趋势用折线图,两个连续变量间的关系用散点图,分布用直方图,离散程度与离群值用箱线图,相关矩阵或密集类别网格用热力图。
为绘图准备数据。 按需聚合、透视或重塑数据。条形图按数值对类别轴排序。将时间序列重采样到合适的粒度。确保没有 NaN 值泄漏到图中而产生缺口或错误。
用恰当的样式构建可视化。 应用一致的调色板、可读的坐标轴标签、描述性标题和正确的图例。去除图表垃圾——不必要的网格线、边框和装饰。使用与目标输出媒介(报告、幻灯片、仪表盘)匹配的图幅尺寸。
添加上下文与注释。 用注释、参考线或阴影区域突出关键数据点。在有帮助的地方直接在图上添加汇总统计(例如箱线图上的中位数线、散点图上的趋势线)。上下文能让图表从装饰变为分析。
导出或展示。 将静态图表保存为 PNG 或 SVG 用于报告,或渲染为交互式 HTML 用于仪表盘与探索。为印刷质量输出将 DPI 设为 150+。
| 目标 | 图表类型 | 库 |
|---|---|---|
| 比较类别 | 条形图(竖向或横向) | matplotlib、seaborn |
| 展示随时间变化的趋势 | 折线图 | matplotlib、plotly |
| 探索两个变量的关系 | 散点图 | seaborn、plotly |
| 展示变量的分布 | 直方图或 KDE | seaborn |
| 跨组比较分布 | 箱线图或小提琴图 | seaborn |
| 展示相关矩阵 | 热力图 | seaborn |
| 展示构成 / 比例 | 堆叠条形图或饼图 | matplotlib |
| 支持用户探索 | 交互式图表 | plotly |
为 Agent 提供数据集和你想可视化的内容说明。可选择指定图表类型、颜色偏好、输出格式和图幅尺寸。若未指定图表类型,Agent 将选择最佳方案。
import pandas as pd import matplotlib.pyplot as plt import seaborn as sns df = pd.read_csv("quarterly_sales.csv", parse_dates=["date"]) sns.set_theme(style="whitegrid", palette="viridis") fig, axes = plt.subplots(2, 2, figsize=(14, 10)) fig.suptitle("Q4 2024 Sales Dashboard", fontsize=16, fontweight="bold") # 1. Monthly revenue trend monthly = df.resample("M", on="date")["revenue"].sum() axes[0, 0].plot(monthly.index, monthly.values, marker="o", linewidth=2) axes[0, 0].set_title("Monthly Revenue Trend") axes[0, 0].set_ylabel("Revenue ($)") axes[0, 0].tick_params(axis="x", rotation=45) # 2. Revenue by region (horizontal bar) region = df.groupby("region")["revenue"].sum().sort_values() axes[0, 1].barh(region.index, region.values, color=sns.color_palette("viridis", len(region))) axes[0, 1].set_title("Revenue by Region") axes[0, 1].set_xlabel("Total Revenue ($)") # 3. Units sold distribution (histogram) axes[1, 0].hist(df["units_sold"], bins=30, edgecolor="white", alpha=0.8) axes[1, 0].axvline(df["units_sold"].median(), color="red", linestyle="--", label="Median") axes[1, 0].set_title("Units Sold Distribution") axes[1, 0].legend() # 4. Revenue vs. discount scatter with regression sns.regplot(data=df, x="discount", y="revenue", ax=axes[1, 1], scatter_kws={"alpha": 0.4, "s": 15}, line_kws={"color": "red"}) axes[1, 1].set_title("Revenue vs. Discount") plt.tight_layout() plt.savefig("sales_dashboard.png", dpi=150, bbox_inches="tight") plt.show()想要更强大的技能插件,就来小葱技能站7w4.net看看吧。
import pandas as pd
import plotly.express as px
df = pd.read_csv("global_sales.csv")
# Interactive scatter with size, color, and hover data
fig = px.scatter(
df,
x="marketing_spend",
y="revenue",
size="units_sold",
color="region",
hover_data=["product_name", "quarter"],
title="Marketing Spend vs Revenue by Region",
labels={
"marketing_spend": "Marketing Spend ($)",
"revenue": "Revenue ($)",
"units_sold": "Units Sold"
},
template="plotly_white"
)
fig.update_traces(marker=dict(opacity=0.7, line=dict(width=1, color="DarkSlateGrey")))
# Add a trend line annotation
fig.add_annotation(
x=45000, y=320000,
text="Strong ROI cluster:<br>low spend, high revenue",
showarrow=True, arrowhead=2,
font=dict(size=12, color="darkblue")
)
fig.write_html("interactive_scatter.html")
fig.show()
# Users can hover over points to see product_name and quarter,
# zoom into clusters, and toggle regions on/off via the legend.
alpha=0.3)、抖动(jitter)或六边形分箱/二维密度图。这个技能质量不错,内容实用且易于理解。它详细讲解了如何选择图表类型、处理数据可视化的常见问题,并提供了可直接使用的代码示例。美中不足的是没有附带示例数据文件,开发者需要自己准备数据来测试代码效果。整体上适合需要生成图表或制作数据仪表盘的用户使用。