plotly.py 是 Python 生态里事实标准的交互式可视化库,基于 plotly.js 构建,支持 30+ 图表类型、3D、地图、金融图,配合 Dash 还能从图变完整 Web 应用。18K stars、MIT 协议、官方维护,是 BI/Dashboard 选型的底层 ” 常青树 ”。
写在前面
数据可视化的 Python 生态里,matplotlib 是静态之王、seaborn 是统计美学代表,但当你的老板 / 客户说 ” 能不能点一下就过滤 ”——交互式 就成刚需。
plotly 在这条路径上扎根最深:12 年迭代、官方公司维护、底层 plotly.js 在前端也是事实标准、配套的 Dash 框架让它从 ” 画图工具 ” 升级成 ” 建应用框架 ”。
它不是新东西,但 为什么现在还要调研它?因为 2026 年的 plotly 已经不只是 Plotly Express 那一套了——v7.0 rc 在路上、narwhals 后端支持让它能跑 Polars/DuckDB 数据、anywidget 适配 Jupyter 现代生态。本期把这个老牌库的最新玩法讲清楚。
一、它解决什么问题
一句话卖点 :Python 生态里最成熟的 交互式 可视化库,配合 Dash 可升级为完整 Web 应用。
| 维度 | 信息 |
|---|---|
| 项目名 | plotly.py |
| 作者 / 维护 | Plotly 公司官方维护(nicolaskruchten / theengineear 等核心 10 人) |
| Stars | 18,724 |
| Forks | 2,834 |
| License | MIT |
| 语言 | Python(底层 plotly.js 是 JavaScript) |
| 创建时间 | 2013-11-21(12 年常青树) |
| 最新版本 | v6.9.0(2026-07-09)/ v7.0.0rc0(2026-07-29 候选) |
| 最近更新 | 2026-08-07(3 天前) |
| Open Issues | 769 |
| 包大小 | ~10 MB(PyPI) |
| Python 要求 | >= 3.8 |
| 核心依赖 | narwhals>=1.15.1、packaging、anywidget(dev) |
二、核心能力:30+ 图表类型全覆盖
2.1 两条 API 路线
| API | 适合场景 | 特点 |
|---|---|---|
plotly.express (px) |
90% 的日常出图 | 一行函数出图、DataFrame 友好、自动配色 |
plotly.graph_objects (go) |
复杂定制 | 底层 Figure 对象、每个 trace/axis 都能调 |
# Express 极简风格
import plotly.express as px
fig = px.bar(x=["a", "b", "c"], y=[1, 3, 2])
fig.show()
2.2 图表类型清单(30+)
| 类别 | 支持 |
|---|---|
| 基础统计 | bar / line / scatter / pie / bubble / area |
| 科学图 | histogram / box / violin / density heatmap / contour |
| 3D 图 | scatter3d / surface / mesh3d / volume |
| 地图 | choropleth / scatter_geo / line_geo(需 plotly-geo 扩展包) |
| 金融图 | candlestick / OHLC / waterfall / funnel |
| 专业图 | sankey / sunburst / treemap / parallel coordinates / ternary |
| 动画 | animation_frame 参数让图随时间 / 分类切换 |
| 多视图 | subplots / facet_row / facet_col |
2.3 输出方式四件套
| 场景 | 怎么用 |
|---|---|
| Jupyter Notebook | fig.show() 内联渲染,配合 anywidget 可交互 |
| 独立 HTML 文件 | fig.write_html("out.html") 单文件可分享 |
| 静态图片 | fig.write_image("out.png") 需 kaleido 包(替代旧版 orca) |
| Dash 应用 | dcc.Graph(figure=fig) 嵌入 Web 应用 |
三、差异化能力:Dash 把 plotly 从图升级成应用
这是 plotly 生态最独特的护城河——Plotly 公司同时维护 Dash,一个基于 Flask + React 的 Python Web 框架。
3.1 一行代码嵌入交互图
import dash
from dash import dcc, html
app = dash.Dash(__name__)
app.layout = html.Div([dcc.Graph(figure=fig), # plotly Figure 直接进 Web
dcc.Dropdown(...), # 控件联动
])
app.run(debug=True)
3.2 Dash 实战能力
- 回调系统:
@callback装饰器让控件改变触发图重算 - 多页面应用:
dash.register_page()模块化 - 企业级组件:Dash Enterprise 提供认证、部署、监控
- 生态扩展:Dash Bootstrap Components / Dash Mantine Components / Dash AG Grid
3.3 现代生态适配
- narwhals 后端:v6+ 引入 narwhals,让 plotly 直接吃 Polars / DuckDB / PyArrow 数据,无需先转 pandas
- anywidget 支持:Jupyter 现代化,不再依赖老的 notebook 扩展
- narwhals 战略意义:意味着 plotly 已从 ”pandas 绑定 ” 升级为 ” 任意 DataFrame 库兼容 ”
四、编辑器 / 导出能力对比
| 能力 | plotly.py | matplotlib | seaborn | bokeh | altair |
|---|---|---|---|---|---|
| 交互式 | ✅ | ❌ | ❌ | ✅ | ✅ |
| 图表种类 | 30+ | 100+ | ~20 | 30+ | ~20 |
| 3D 支持 | ✅ | ✅ | ❌ | ❌ | ❌ |
| 地图 | ✅(扩展) | ❌ | ❌ | ✅(tile) | ❌ |
| 静态导出 | ✅ kaleido | ✅ 原生 | ✅ 靠 matplotlib | ✅ | ✅ |
| Web 应用 | ✅ Dash | ❌ | ❌ | ✅ Bokeh Server | ✅ Vega-Lite |
| 大数据性能 | ⚠️ 万级卡顿 | ✅ | ⚠️ | ✅ | ⚠️ |
| 学习曲线 | 低 | 中 | 低 | 中 | 低 |
| 文档质量 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐⭐ | ⭐⭐⭐⭐ | ⭐⭐⭐ | ⭐⭐⭐⭐ |
| 公司维护 | ✅ Plotly | ❌ 社区 | ❌ 社区 | ✅ Anaconda | ❌ 社区 |
五、对比:跟同类工具比好在哪
| 维度 | plotly.py | bokeh | altair (Vega-Lite) | matplotlib |
|---|---|---|---|---|
| 底层 | plotly.js | bokeh.js | Vega-Lite | 静态光栅化 |
| 语法风格 | 函数式 / 面向对象 | 面向对象 | 声明式 JSON | 状态机式 pyplot |
| 3D | ✅ 原生 | ❌ | ❌ | ✅ mpl_toolkits |
| 配套应用框架 | ✅ Dash | ⚠️ Bokeh Server | ❌ | ❌ |
| 大数据 | ⚠️ 万级 | ✅ Datashader | ⚠️ | ✅ |
| Vega 生态 | ❌ | ❌ | ✅(Vega-Lite 跨语言) | ❌ |
| 国内使用率 | 高 | 中 | 中 | 极高 |
| 社区规模 | ⭐⭐⭐⭐⭐ | ⭐⭐⭐ | ⭐⭐⭐ | ⭐⭐⭐⭐⭐ |
结论 :plotly 不是每个维度的冠军,但 是唯一一个同时满足 ” 交互式 + 图表全 + 配套应用框架 + 公司长期维护 + 中文社区活跃 ” 五项的库。这就是为什么它成 BI 选型的 ” 底层默认 ”。
六、实战:3 步跑通 + Dash 联动
Step 1:安装 + 基础出图
pip install plotly kaleido
import plotly.express as px
# 用内置数据集
df = px.data.gapminder()
fig = px.scatter(
df, x="gdpPercap", y="lifeExp",
size="pop", color="continent",
log_x=True, size_max=60,
animation_frame="year", # 时间动画
title="Gapminder: GDP vs Life Expectancy",
)
fig.show()
Step 2:导出多格式
fig.write_html("gapminder.html") # 交互 HTML(分享给同事)fig.write_image("gapminder.png") # 静态 PNG(kaleido)fig.write_html("gapminder.html", include_plotlyjs="cdn") # 轻量 HTML
Step 3:升级为 Dash Web 应用
import dash
from dash import dcc, html, Input, Output
app = dash.Dash(__name__)
app.layout = html.Div([
dcc.Dropdown(
id="continent",
options=[{"label": c, "value": c} for c in df["continent"].unique()],
value="Asia",
),
dcc.Graph(id="graph"),
])
@app.callback(Output("graph", "figure"), Input("continent", "value"))
def update(continent):
dff = df[df["continent"] == continent]
return px.line(dff, x="year", y="lifeExp", color="country")
app.run(debug=True)
打开 http://127.0.0.1:8050 就是一个完整的交互 Dashboard。
七、风险与坑
- 大数据性能瓶颈:万级以上点散点图会卡顿(前端 plotly.js 渲染压力)。需要先用 Datashader / 降采样 / WebGL 模式(
scattergl替代scatter)。 - Dash 是 Flask 框架的 ” 特化 ”:Dash 回调系统适合中等复杂度(几十个控件),超过 100 个回调会变成 ” 回调地狱 ”。大型项目建议配合 Dash Pages 模块化。
- v7.0 升级风险:v7.0rc0 已发布(2026-07-29),主版本升级可能 breaking changes(narwhals API 重写、deprecate 部分老 API),生产锁定建议 v6.9 LTS。
- narwhals 兼容还在演进:虽然 v6+ 引入了 narwhals,但部分高级功能(自定义 trace)仍依赖 pandas DataFrame 路径。Polars 用户可能需要 workaround。
- 依赖包体积:plotly 包 ~10 MB、Kaleido 额外 ~150 MB。Docker 镜像要预留空间。
- 中文文档稀缺:官方英文文档极好(plotly.com/python),中文翻译基本靠社区,质量参差。
- Plotly 公司战略不确定性:Plotly 公司同时维护商业产品(Dash Enterprise / Chart Studio),开源版本功能可能跟商业版拉开梯度。开源核心目前没看到 risk,但要关注。
八、总结
三个最值得装的理由:
- BI 选型的 ” 底层默认 ”——18K stars + 12 年迭代 + Plotly 公司官方维护,比任何 ” 新出的可视化库 ” 都稳。如果你做 BI/Dashboard/Dashboard 后端,plotly 几乎是绕不开的底层
- Dash 让它 ” 图→应用 ” 一步到位——其他可视化库要搭应用框架(Django/Flask + 前端),plotly + Dash 一行代码出 Web 页面,独立开发者效率最高
- narwhals + anywidget 已现代化——不再是 ” 老牌 Python 库的包袱 ”,2026 年已原生支持 Polars / DuckDB / Jupyter anywidget
一句话 :如果你还在 matplotlib 出报告, 先试一周 plotly.express,让老板在浏览器里点点过滤,比 PDF 截图震撼 10 倍。
参考
- GitHub: https://github.com/plotly/plotly.py
- PyPI: https://pypi.org/project/plotly/
- 官方文档: https://plotly.com/python/
- Dash 文档: https://dash.plotly.com/
- Kaleido 静态导出: https://github.com/plotly/Kaleido
- 社区论坛: https://community.plotly.com/
- Plotly 公司: https://plotly.com/
- narwhals(DataFrame 后端抽象): https://narwhals.readthedocs.io/