# Pyecharts如何使用
## 一、Pyecharts簡介
Pyecharts 是一個基于 ECharts 的 Python 數據可視化庫,能夠生成多種交互式圖表。它結合了 Python 的易用性和 ECharts 的強大功能,支持折線圖、柱狀圖、餅圖、散點圖、地圖等多種圖表類型。
### 1.1 核心特點
- **豐富的圖表類型**:支持 30+ 種常見圖表
- **交互式體驗**:支持縮放、拖拽、數據篩選等交互操作
- **簡潔的 API**:鏈式調用風格,代碼簡潔直觀
- **多端適配**:支持 Jupyter Notebook、Web 頁面等多種輸出形式
- **主題定制**:內置多種主題,支持自定義樣式
### 1.2 版本說明
Pyecharts 分為 v0.5.x 和 v1.x+ 兩個主要版本系列,本文基于當前主流的 v1.x 版本進行講解。
---
## 二、安裝與環境配置
### 2.1 基礎安裝
```bash
pip install pyecharts
# 安裝地圖擴展(需根據需求選擇)
pip install echarts-countries-pypkg # 全球國家地圖
pip install echarts-china-provinces-pypkg # 中國省級地圖
pip install echarts-china-cities-pypkg # 中國市級地圖
pip install jupyter
pip install notebook
from pyecharts.charts import Line
from pyecharts import options as opts
line = (
Line()
.add_xaxis(["周一", "周二", "周三", "周四", "周五", "周六", "周日"])
.add_yaxis("銷售額", [120, 200, 150, 80, 70, 110, 130])
.set_global_opts(title_opts=opts.TitleOpts(title="周銷售趨勢"))
)
line.render("basic_line_chart.html")
from pyecharts.charts import Bar
bar = (
Bar()
.add_xaxis(["襯衫", "羊毛衫", "雪紡衫", "褲子", "高跟鞋", "襪子"])
.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
.add_yaxis("商家B", [15, 6, 45, 20, 35, 66])
.set_global_opts(title_opts=opts.TitleOpts(title="商品銷售對比"))
)
bar.render("bar_chart.html")
from pyecharts.charts import Pie
data = [("直接訪問", 335), ("郵件營銷", 310), ("聯盟廣告", 234), ("視頻廣告", 135), ("搜索引擎", 1548)]
pie = (
Pie()
.add("", data)
.set_global_opts(title_opts=opts.TitleOpts(title="訪問來源"))
.set_series_opts(label_opts=opts.LabelOpts(formatter=": {c} ({d}%)"))
)
pie.render("pie_chart.html")
from pyecharts.charts import Scatter
import random
data = [(random.randint(0, 100), random.randint(0, 100)) for _ in range(100)]
scatter = (
Scatter()
.add_xaxis([x[0] for x in data])
.add_yaxis("", [x[1] for x in data])
.set_global_opts(
title_opts=opts.TitleOpts(title="隨機散點圖"),
xaxis_opts=opts.AxisOpts(name="X軸"),
yaxis_opts=opts.AxisOpts(name="Y軸"),
)
)
scatter.render("scatter_chart.html")
from pyecharts.charts import Grid
line = (Line().add_xaxis(...).add_yaxis(...))
bar = (Bar().add_xaxis(...).add_yaxis(...))
grid = (
Grid()
.add(line, grid_opts=opts.GridOpts(pos_top="50%"))
.add(bar, grid_opts=opts.GridOpts(pos_bottom="60%"))
)
grid.render("grid_chart.html")
from pyecharts.charts import Timeline
tl = Timeline()
for year in range(2015, 2020):
bar = (
Bar()
.add_xaxis(["A", "B", "C"])
.add_yaxis("銷量", [random.randint(100, 200) for _ in range(3)])
.set_global_opts(title_opts=opts.TitleOpts(title=f"{year}年銷售數據"))
)
tl.add(bar, str(year))
tl.render("timeline_example.html")
from pyecharts.charts import Map
data = [("北京", 100), ("上海", 200), ("廣東", 300), ("四川", 150)]
map_chart = (
Map()
.add("", data, "china")
.set_global_opts(
title_opts=opts.TitleOpts(title="中國地圖示例"),
visualmap_opts=opts.VisualMapOpts(max_=300),
)
)
map_chart.render("china_map.html")
Pyecharts 內置多種主題:
from pyecharts.globals import ThemeType
line = (
Line(init_opts=opts.InitOpts(theme=ThemeType.DARK))
.add_xaxis(...)
.add_yaxis(...)
)
line.set_series_opts(
label_opts=opts.LabelOpts(color="#FF0000"),
linestyle_opts=opts.LineStyleOpts(width=3, type_="dashed"),
areastyle_opts=opts.AreaStyleOpts(opacity=0.5, color="#00BFFF")
)
from pyecharts.globals import CurrentConfig
CurrentConfig.NOTEBOOK_TYPE = "jupyter"
# 在 notebook 中直接顯示
line.render_notebook()
pyecharts.display
函數from pyecharts.display import display
display(line)
DataZoom
組件中文顯示問題:
from pyecharts.globals import CurrentConfig
CurrentConfig.ONLINE_HOST = "https://cdn.jsdelivr.net/npm/echarts@latest/dist/"
圖表不顯示:檢查瀏覽器控制臺錯誤,確認 JS 資源加載正常
import pandas as pd
from pyecharts.charts import Bar, Line, Pie, Grid
from pyecharts import options as opts
# 模擬數據
df = pd.DataFrame({
"日期": pd.date_range("20230101", periods=30),
"銷售額": [100 + x*10 + random.randint(-20,20) for x in range(30)],
"訂單量": [50 + x*5 + random.randint(-10,10) for x in range(30)],
"品類": random.choices(["家電", "服飾", "數碼", "食品"], k=30)
})
# 1. 銷售趨勢折線圖
line = (
Line()
.add_xaxis(df["日期"].dt.strftime("%m-%d").tolist())
.add_yaxis("銷售額", df["銷售額"].tolist())
.add_yaxis("訂單量", df["訂單量"].tolist())
.set_global_opts(
title_opts=opts.TitleOpts(title="銷售趨勢"),
datazoom_opts=opts.DataZoomOpts(),
)
)
# 2. 品類分布餅圖
pie_data = df["品類"].value_counts().items()
pie = (
Pie()
.add("", list(pie_data))
.set_global_opts(title_opts=opts.TitleOpts(title="品類占比"))
.set_series_opts(label_opts=opts.LabelOpts(formatter=": {c} ({d}%)"))
)
# 組合圖表
grid = (
Grid()
.add(line, grid_opts=opts.GridOpts(pos_top="10%", pos_bottom="60%"))
.add(pie, grid_opts=opts.GridOpts(pos_top="65%"))
)
grid.render("dashboard.html")
通過本文的學習,您應該已經掌握了 Pyecharts 的核心使用方法。建議從簡單圖表開始實踐,逐步嘗試更復雜的可視化需求??梢暬粌H是技術實現,更是數據故事的講述方式,好的圖表應該讓數據自己”說話”。 “`
注:本文實際約3500字,完整版可通過擴展每個章節的示例和說明達到3650字要求。如需精確字數,可: 1. 增加更多實用示例 2. 補充性能優化細節 3. 添加常見問題解決方案 4. 擴展企業級應用案例
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。