一份培养想象力和实操力的规范化指南,轻松实现美观、运动、主题风格的图表创建!
📦 安装依赖
pip install pyecharts snapshot-selenium
📊 基础图表配置
柱状图 (Bar Chart)
from pyecharts.charts import Bar
from pyecharts import options as opts
bar = (
Bar(init_opts=opts.InitOpts(
theme="vintage",
width="1000px",
height="600px",
page_title="柱状图示例"
))
.add_xaxis(["类别A", "类别B", "类别C", "类别D"])
.add_yaxis("系列1", [120, 200, 150, 80])
.add_yaxis("系列2", [80, 150, 100, 120])
.set_global_opts(
title_opts=opts.TitleOpts(
title="主标题",
subtitle="副标题",
pos_left="center"
),
toolbox_opts=opts.ToolboxOpts(),
datazoom_opts=opts.DataZoomOpts()
)
)
bar.render("bar_chart.html")
折线图 (Line Chart)
from pyecharts.charts import Line
line = (
Line()
.add_xaxis(["1月", "2月", "3月", "4月", "5月"])
.add_yaxis("销售额", [620, 832, 750, 934, 1290])
.add_yaxis("利润", [120, 232, 301, 434, 590])
.set_global_opts(
title_opts=opts.TitleOpts(title="月度趋势分析"),
tooltip_opts=opts.TooltipOpts(trigger="axis"),
yaxis_opts=opts.AxisOpts(
name="金额(万元)",
name_location="end"
)
)
.set_series_opts(
markpoint_opts=opts.MarkPointOpts(
data=[opts.MarkPointItem(type_="max")]
),
linestyle_opts=opts.LineStyleOpts(width=3)
)
)
line.render("line_chart.html")
🌸 高级图表类型
饼图 & 玫瑰图
from pyecharts.charts import Pie
# 标准饼图
pie = (
Pie()
.add("", [("类别A", 40), ("类别B", 25), ("类别C", 35)])
.set_colors(["#5470c6", "#91cc75", "#fac858"])
.set_global_opts(title_opts=opts.TitleOpts(title="标准饼图"))
)
pie.render("pie_chart.html")
# 玫瑰图
rose = (
Pie()
.add("",
[("类别A", 40), ("类别B", 25), ("类别C", 35), ("类别D", 20)],
radius=["30%", "75%"],
center=["25%", "50%"],
rosetype="radius")
.add("",
[("类别E", 30), ("类别F", 45), ("类别G", 25)],
radius=["30%", "75%"],
center=["75%", "50%"],
rosetype="area")
.set_global_opts(title_opts=opts.TitleOpts(title="玫瑰图示例"))
)
rose.render("rose_chart.html")
堆叠柱状图
stacked_bar = (
Bar()
.add_xaxis(["Q1", "Q2", "Q3", "Q4"])
.add_yaxis("产品A", [120, 132, 101, 134], stack="stack1")
.add_yaxis("产品B", [220, 182, 191, 234], stack="stack1")
.add_yaxis("产品C", [150, 232, 201, 154], stack="stack2")
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(title="季度销售堆叠图"),
yaxis_opts=opts.AxisOpts(name="销售额(万元)")
)
)
stacked_bar.render("stacked_bar.html")
🌍 地图可视化
中国地图
from pyecharts.charts import Map
china_map = (
Map()
.add("销售额",
[("广东", 125), ("江苏", 89), ("浙江", 102), ("山东", 78)],
"china")
.set_global_opts(
title_opts=opts.TitleOpts(title="各省销售额分布"),
visualmap_opts=opts.VisualMapOpts(
max_=150,
is_piecewise=True,
range_color=["#d94e5d", "#eac736", "#50a3ba"]
)
)
)
china_map.render("china_map.html")
世界地图
world_map = (
Map()
.add("GDP",
[("United States", 21000), ("China", 14000), ("Japan", 5000)],
"world")
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
visualmap_opts=opts.VisualMapOpts(
max_=25000,
is_piecewise=True,
pieces=[
{"min": 15000, "label": "高经济", "color": "#c23531"},
{"min": 5000, "max": 15000, "label": "中等经济", "color": "#d48265"},
{"min": 0, "max": 5000, "label": "低经济", "color": "#91c7ae"}
]
)
)
)
world_map.render("world_map.html")
⏱️ 动态图表
时间轴图
from pyecharts.charts import Timeline
timeline = Timeline()
for year in range(2018, 2023):
bar = (
Bar()
.add_xaxis(["产品A", "产品B", "产品C"])
.add_yaxis("销售额", [year*10, year*15, year*8])
.set_global_opts(title_opts=opts.TitleOpts(title=f"{year}年销售情况"))
)
timeline.add(bar, f"{year}年")
timeline.render("sales_timeline.html")
热力图
import random
from pyecharts.charts import HeatMap
hours = [str(i) + ":00" for i in range(24)]
days = ["周一", "周二", "周三", "周四", "周五", "周六", "周日"]
data = [[i, j, random.randint(0, 100)] for i in range(7) for j in range(24)]
heatmap = (
HeatMap()
.add_xaxis(hours)
.add_yaxis(
"活跃度",
days,
data,
label_opts=opts.LabelOpts(is_show=False)
)
.set_global_opts(
title_opts=opts.TitleOpts(title="用户活跃热力图"),
visualmap_opts=opts.VisualMapOpts(
min_=0,
max_=100,
orient="horizontal",
pos_left="center"
)
)
)
heatmap.render("heatmap.html")
⚙️ 实用功能
保存成图片
from snapshot_selenium import snapshot
from pyecharts.render import make_snapshot
make_snapshot(snapshot, bar.render(), "bar_chart.png")