from pyecharts import options as opts
from pyecharts.charts import Pie
data = [
['a', 10],
['b', 20],
['c', 30],
['d', 40],
]
pie = (
# 链式调用
Pie()
# 添加数据
.add("", data_pair=data)
# 设置颜色
.set_colors(["blue", "green", "yellow", "red"])
# 设置标题 全局设置项
.set_global_opts(title_opts=opts.TitleOpts(title="饼图"))
# 设置标签 局部设置项
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
# 网页渲染
.render("饼图.html")
)
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
bar = (
Bar()
.add_xaxis(Faker.days_attrs) # x轴的名字
.add_yaxis("商家A", Faker.days_values) # y轴的数据
# 全局配置项
.set_global_opts(
# 设置标题
title_opts=opts.TitleOpts(title="Bar-DataZoom(slider-水平)"),
# 显示区域缩放条
datazoom_opts=opts.DataZoomOpts(),
)
.render("柱状图.html")
)
折线图
import pyecharts.options as opts
from pyecharts.charts import Line
data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
line = (
Line()
.add_xaxis([i for i in range(1, 11)]) # 设置x轴名称
.add_yaxis("商家A", data) # 设置y轴折线
.set_global_opts(title_opts=opts.TitleOpts(title="Line-基本示例")) # 设置标题
.render("line_base.html")
)
选择对应要显示的多个图表,手动拖动式布局图表
from pyecharts.charts import Page, Bar, Pie, Polar, Line
from pyecharts import options as opts
from pyecharts.faker import Faker
data = [
['a', 10],
['b', 20],
['c', 30],
['d', 40],
]
pie = (
# 链式调用
Pie()
# 添加数据
.add("", data_pair=data)
# 设置颜色
.set_colors(["blue", "green", "yellow", "red"])
# 设置标题
.set_global_opts(title_opts=opts.TitleOpts(title="饼图"))
# 设置标签
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
# # 网页渲染
# .render("pie_set_color.html")
)
bar = (
Bar()
.add_xaxis(Faker.days_attrs) # x轴的名字
.add_yaxis("商家A", Faker.days_values) # y轴的数据
# 全局配置项
.set_global_opts(
# 设置标题
title_opts=opts.TitleOpts(title="Bar-DataZoom(slider-水平)"),
# 显示区域缩放条
datazoom_opts=opts.DataZoomOpts(),
)
# .render("bar_datazoom_slider.html")
)
data2 = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
polar = (
Polar()
# 设置角轴每一部分的名称,以及轴类型
.add_schema(angleaxis_opts=opts.AngleAxisOpts(data=data2, type_="category"))
# 设置数据的名称,数据值,表现形式,表示堆叠的值
.add("A", [1, 2, 3, 4, 3, 5, 1], type_="bar", stack="stack0")
.add("B", [2, 4, 6, 1, 2, 3, 1], type_="bar", stack="stack0")
.add("C", [1, 2, 3, 4, 1, 2, 5], type_="bar", stack="stack0")
# 设置标题
.set_global_opts(title_opts=opts.TitleOpts(title="极坐标系图"))
# .render("polar_angleaxis.html")
)
data3 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
line = (
Line()
.add_xaxis([i for i in range(1, 11)]) # 设置x轴名称
.add_yaxis("商家A", data3) # 设置y轴折线
.set_global_opts(title_opts=opts.TitleOpts(title="Line-基本示例")) # 设置标题
# .render("line_base.html")
)
# 开始
# 实例化Page对象,指定布局方式
page = Page(layout=Page.DraggablePageLayout)
page.add(pie, bar, polar, line)
page.render("old.html")
# 打开绘制好的网页,进行拖曳调整布局。然后保存设置
打开生成的图表页面,调整页面图表布局,点击左上角 Save Config按钮保存设置
chart_config.json
文件生成新图表页面将保存的chart_config.jsonn文件 放置项目目录
# 利用上一步渲染好的网页,和自定义保存的布局json文件来重新生成网页
from pyecharts.charts import Page
Page.save_resize_html('old.html', cfg_file="chart_config.json", dest="new.html")