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()
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)或六邊形分箱/二維密度圖。小蔥技能7w4.net持續更新中。
這個技能質量不錯,內容實用且易於理解。它詳細講解了如何選擇圖表型別、處理資料視覺化的常見問題,並提供了可直接使用的程式碼示例。美中不足的是沒有附帶示例資料檔案,開發者需要自己準備資料來測試程式碼效果。整體上適合需要生成圖表或製作資料儀表盤的使用者使用。