Plotly interactive viz

From findscripter/everything-skills · View interactive version

Plotly interactive viz is an AI agent skill from the GitHub repository findscripter/everything-skills. 当需要带悬停、缩放、平移或可嵌入网页的交互式图表(仪表盘、探索性分析、演示)时使用;用 Plotly Express/Graph Objects 产出 40+ 种图表并导出 HTML 或静态图;不适用于无交互的出版级静态图(用 matplotlib);触发词:plotly、交互式图表、hover、仪表盘、HTML 图表

How to use this skill

Skill file

何时使用

需要交互式图表时用本技能:图表要支持悬停提示、缩放/平移、图例切换、框选,或要嵌入网页/仪表盘/演示。典型场景——探索性分析 notebook、仪表盘、时间序列 rangeslider、3D 曲面、金融 K 线。

不该用的边界:

步骤 / 指令

  1. 安装:uv pip install plotly;需导出 PNG/PDF/SVG 再装 uv pip install kaleido。
  2. 选 API(关键决策):
  1. 画图 → 调样式(template、update_traces、update_xaxes)→ 输出(fig.show() / write_html / write_image)。
  2. 多图仪表盘用 make_subplots,并在 specs 里声明各格 type。

示例

最小起步(Plotly Express):

import plotly.express as px
import pandas as pd

df = pd.DataFrame({'x': [1, 2, 3, 4], 'y': [10, 11, 12, 13]})
fig = px.scatter(df, x='x', y='y', title='My First Plot')
fig.show()

常用工作流:

# 带趋势线散点 / 相关性热力图
fig = px.scatter(df, x='temperature', y='yield', trendline='ols')
fig = px.imshow(corr, text_auto=True, color_continuous_scale='RdBu')

# 分布对比 / 箱线(全点)/ 小提琴
fig = px.histogram(df, x='values', color='group', marginal='box', nbins=30)
fig = px.box(df, x='category', y='value', points='all')
fig = px.violin(df, x='group', y='measurement', box=True)

# 时间序列 + rangeslider;动画
fig = px.line(df, x='date', y='price'); fig.update_xaxes(rangeslider_visible=True)
fig = px.scatter(df, x='x', y='y', animation_frame='year')

Graph Objects(px 覆盖不到的图):

import plotly.graph_objects as go
# 3D 曲面
fig = go.Figure(data=[go.Surface(z=z_data, x=x_data, y=y_data)])
# K 线
fig = go.Figure(data=[go.Candlestick(x=df['date'], open=df['open'],
    high=df['high'], low=df['low'], close=df['close'])])

多图仪表盘:

from plotly.subplots import make_subplots
fig = make_subplots(rows=2, cols=2, subplot_titles=('Scatter','Bar','Histogram','Box'),
    specs=[[{'type':'scatter'},{'type':'bar'}],[{'type':'histogram'},{'type':'box'}]])
fig.add_trace(go.Scatter(x=[1,2,3], y=[4,5,6]), row=1, col=1)
fig.update_layout(height=800, showlegend=False)

交互定制与导出:

fig.update_traces(hovertemplate='<b>%{x}</b><br>Value: %{y:.2f}<extra></extra>')
fig.write_html('chart.html')                          # 独立 HTML
fig.write_html('chart.html', include_plotlyjs='cdn')  # 体积更小
fig.write_image('chart.png')   # 需 kaleido;支持 .pdf / .svg

模板(统一样式):template='plotly_dark'(内置 plotly_white plotly_dark ggplot2 seaborn simple_white)。

嵌入 Web 应用(Dash):

import dash
from dash import dcc, html
app = dash.Dash(__name__)
app.layout = html.Div([html.H1('Dashboard'), dcc.Graph(figure=px.scatter(df, x='x', y='y'))])
app.run_server(debug=True)

注意事项

互见


采编自 sickn33/antigravity-awesome-skills(MIT),原作者 K-Dense Inc.,适配重写并精简。

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.