pyecharts 分为 v0.5.X 和 v1 两个大版本,v0.5.X 和 v1 间不兼容,v1 是一个全新的版本。
v0.5.X版本地址
v1 版本地址
具体如何安装都在这官方文档中,大家可点击查看详情。
from pyecharts.charts import Bar
bar = Bar()
bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
# render 会生成本地 HTML 文件,默认会在当前目录生成 render.html 文件
# 也可以传入路径参数,如 bar.render("mycharts.html")
bar.render_notebook() #在notebook上直接展示可用这个
# bar.render() # 保存到本地文件
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Scatter
scatter = Scatter()
scatter.add_xaxis(Faker.choose())
scatter.add_yaxis("商家A", Faker.values())
# scatter.set_global_opts(title_opts=opts.TitleOpts(title="Scatter-基本示例"))
scatter.set_global_opts(
title_opts=opts.TitleOpts(title="Scatter-显示分割线"),
xaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True)),
yaxis_opts=opts.AxisOpts(splitline_opts=opts.SplitLineOpts(is_show=True)),
)
scatter.render_notebook()
def scatter_visualmap_color():
c = (
Scatter()
.add_xaxis(Faker.choose())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(
title_opts=opts.TitleOpts(title="Scatter-VisualMap(Size)"),
visualmap_opts=opts.VisualMapOpts(type_="size", max_=150, min_=20),
)
)
return c
scatter_visualmap_color().render_notebook()
def overlap_line_scatter():
x = Faker.choose()
bar = (
Bar()
.add_xaxis(x)
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(title_opts=opts.TitleOpts(title="Overlap-line+scatter"))
)
line = (
Line()
.add_xaxis(x)
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
)
bar.overlap(line)
return bar
overlap_line_scatter().render()
import pyecharts.options as opts
from pyecharts.faker import Faker
from pyecharts.charts import Line
def line_xaxis_type():
c = (
Line()
.add_xaxis(Faker.values())
.add_yaxis("商家A", Faker.values())
.add_yaxis("商家B", Faker.values())
.set_global_opts(
title_opts=opts.TitleOpts(title="Line-数值 X 轴"),
xaxis_opts=opts.AxisOpts(type_="value"),
)
)
return c
line_xaxis_type().render_notebook()
from pyecharts import options as opts
from pyecharts.charts import Boxplot
def boxpolt_base():
v1 = [
[850, 740, 900, 1070, 930, 850, 950, 980, 980, 880]
+ [1000, 980, 930, 650, 760, 810, 1000, 1000, 960, 960],
[960, 940, 960, 940, 880, 800, 850, 880, 900]
+ [840, 830, 790, 810, 880, 880, 830, 800, 790, 760, 800],
]
v2 = [
[890, 810, 810, 820, 800, 770, 760, 740, 750, 760]
+ [910, 920, 890, 860, 880, 720, 840, 850, 850, 780],
[890, 840, 780, 810, 760, 810, 790, 810, 820, 850, 870]
+ [870, 810, 740, 810, 940, 950, 800, 810, 870],
]
c = Boxplot()
c.add_xaxis(["expr1", "expr2"]).add_yaxis("A", c.prepare_data(v1)).add_yaxis(
"B", c.prepare_data(v2)
).set_global_opts(title_opts=opts.TitleOpts(title="BoxPlot-基本示例"))
return c
boxpolt_base().render_notebook()
import random
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import HeatMap
def heatmap_base():
value = [[i, j, random.randint(0, 50)] for i in range(24) for j in range(7)]
c = (
HeatMap()
.add_xaxis(Faker.clock)
.add_yaxis("series0", Faker.week, value)
.set_global_opts(
title_opts=opts.TitleOpts(title="HeatMap-基本示例"),
visualmap_opts=opts.VisualMapOpts(),
)
)
return c
heatmap_base().render_notebook()
import random
from pyecharts.faker import Faker
from pyecharts import options as opts
from pyecharts.charts import Scatter3D
def scatter3d_base():
data = [
[random.randint(0, 100), random.randint(0, 100), random.randint(0, 100)]
for _ in range(80)
]
c = (
Scatter3D()
.add("", data)
.set_global_opts(
title_opts=opts.TitleOpts("Scatter3D-基本示例"),
visualmap_opts=opts.VisualMapOpts(range_color=Faker.visual_color),
)
)
return c
scatter3d_base().render_notebook()
注意: 在pyecharts中的数据只能是int格式 不能是numpy.int等其他数据类型,不然就识别不了
# 获取符合pyecharts的数据格式
data_num = [list(z) for z in zip(df_pro.province.values,[int(i) for i in df_pro['number'].values])]
# 获取数据中的最大值
max_num = int(df_pro['number'].max())
# 数据连续型
def map_visualmap():
c = (
Map()
.add("商家A", data_num, "china")
.set_global_opts(
title_opts=opts.TitleOpts(title="Map-VisualMap(连续型)"),
visualmap_opts=opts.VisualMapOpts(max_=max_num),
)
)
return c
map_visualmap().render_notebook()
# 数据分段型
def map_visualmap():
c = (
Map()
.add("商家A", data_num, "china")
.set_global_opts(
title_opts=opts.TitleOpts(title="Map-VisualMap(分段型)"),
visualmap_opts=opts.VisualMapOpts(max_=max_num, is_piecewise=True,split_number=6),
)
)
return c
map_visualmap().render_notebook()
from pyecharts import options as opts
from pyecharts.charts import Funnel, Page
from random import randint
def funnel_base():
c = (
Funnel()
.add("豪车", [list(z) for z in zip(['宝马', '法拉利', '奔驰', '奥迪', '大众', '丰田', '特斯拉'],
[randint(1, 20) for _ in range(7)])])
.set_global_opts(title_opts=opts.TitleOpts(title="豪车漏斗图"))
)
return c
funnel_base().render_notebook()
from pyecharts import options as opts
from pyecharts.charts import Pie
from random import randint
def pie_base() -> Pie:
c = (
Pie()
.add("", [list(z) for z in zip(['宝马', '法拉利', '奔驰', '奥迪', '大众', '丰田', '特斯拉'],
[randint(1, 20) for _ in range(7)])])
.set_global_opts(title_opts=opts.TitleOpts(title="Pie-基本示例"))
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
)
return c
pie_base().render_notebook()
def pie_base() -> Pie:
c = (
Pie()
.add("", [list(z) for z in zip(list(lb['label'].values[1:]),
[int(i) for i in lb['messagebody'].values[1:]])],
center=["40%", "50%"]) #图标以中心为原点
.set_global_opts(title_opts=opts.TitleOpts(title="用户语句类别分布"),
legend_opts=opts.LegendOpts( pos_left="80%", orient="vertical") #将标注放于右侧
# legend_opts=opts.LegendOpts(pos_top="5%"),
)
.set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
)
return c
pie_base().render_notebook()