官网
中文官方文档: https://pyecharts.org/#/zh-cn/intro
下载:
pip install pyecharts
导包:
from pyecharts.charts import *
简单绘图
画柱状图
# 普通调用
# 画柱状图
bar = Bar()
bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
bar.add_yaxis('服装',[5, 20, 36, 10, 75, 90])
# 保存绘制的图像 只能保存html文件
bar.render('普通柱状图.html')
# 显示
bar.render_notebook()
散点图绘制
# 散点图绘制
# 链式调用
x_data = [1,2,3,4,5,6,7]
y_data=[10,20,30,40,50,60,70]
scatter = (
Scatter()
.add_xaxis(x_data)
.add_yaxis('散点图1',y_data)
)
scatter.render_notebook()
折线图绘制
# 折线图绘制
line_x_data = ['2019','2020','2021','2022']
lint_y_data = [10,20,30,20,15]
line = (Line().add_xaxis(line_x_data).add_yaxis('折线图',lint_y_data))
line.render_notebook()
饼图绘制
# 饼图绘制
num=[110,120,130,140,150,160]
lab=['python','java','html','JavaScript','css','c']
pie = Pie()
pie.add('饼图',[[i,j] for i,j in zip(lab,num)],radius=100)# radius半径
pie.render_notebook()
绘制地图
# 绘制地图
maps = Map()
name=['邵阳市','长沙市','怀化市','益阳市']
data = [[i,random.randint(0,30)]for i in name]
maps.add('地图',data,maptype='湖南')
maps.render_notebook()
绘制词云
# 绘制词云
wordcloud = WordCloud()
words = [
("花鸟市场", 1446),
("汽车", 928),
("视频", 906),
("电视", 825),
("Lover Boy 88", 514),
("动漫", 486),
("音乐", 53),
("直播", 163),
("广播电台", 86),
("戏曲曲艺", 17),
("演出票务", 6),
("给陌生的你听", 1),
("资讯", 1437),
("商业财经", 422),
("娱乐八卦", 353),
("军事", 331),
("科技资讯", 313),
("社会时政", 307),
("时尚", 43),
("网络奇闻", 15),
("旅游出行", 438),
("景点类型", 957),
("国内游", 927),
("远途出行方式", 908),
("酒店", 693),
("关注景点", 611),
("旅游网站偏好", 512),
("出国游", 382),
("交通票务", 312),
("旅游方式", 187),
("旅游主题", 163),
("港澳台", 104),
("本地周边游", 3),
("小卖家", 1331)]
wordcloud.add('词云',words)
wordcloud.render_notebook()
绘图高级操作-配置
全局配置项
# set_global_opts
import pyecharts.options as opts
bar = (Bar()
.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
.add_yaxis('柱状图1',[5, 20, 36, 10, 75, 90])
# .set_global_opts设置全局配置项
.set_global_opts(title_opts=opts.TitleOpts(title='我是主标题',subtitle='我是副标题'))
)
bar.render_notebook()
主题选项
# 导入主题选项
from pyecharts.globals import ThemeType
bar = (Bar(init_opts=opts.InitOpts(
# 通过全局配置项修改画图大小
width='500px',
height='200px',
# 修改主题
theme=ThemeType.LIGHT
))
.add_xaxis(["Apple", "HuaWei", "XiaoMi", "oppo"])
.add_yaxis('柱状图2',[36, 10, 75, 90])
.set_global_opts(title_opts=opts.TitleOpts(title='我是主标题',subtitle='我是副标题'))
)
bar.render_notebook()
区域缩放配置项
# 区域缩放配置项
x_data = list(range(2000,2022))
y_data_1=[random.randint(0,100) for i in x_data]
y_data_2=[random.randint(0,100) for i in x_data]
bar = (Bar()
.add_xaxis(x_data)
.add_yaxis('data1',y_data_1)
.add_yaxis('data2',y_data_2)
# 区域缩放配置项
.set_global_opts(datazoom_opts=opts.DataZoomOpts(
range_start=30,
range_end=80
))
)
bar.render_notebook()
涟漪配置项
# 涟漪配置项
effect_scatter = (EffectScatter()
.add_xaxis([random.randint(0,100) for i in x_data])
.add_yaxis('涟漪散点图',[random.randint(0,100) for i in x_data])
.set_series_opts(effect_opts=opts.EffectOpts(period=6))
)
effect_scatter.render_notebook()
图例配置项
# 图例配置项
bar = (Bar()
.add_xaxis(x_data)
.add_yaxis('data1',y_data_1)
.add_yaxis('data2',y_data_2)
.set_global_opts(legend_opts=opts.LegendOpts(
# is_show=False # 不显示图例
pos_left='60px'# 设置图例离左侧的举例
))
)
bar.render_notebook()