以下代码均在jupyter notebook 中运行。
一、全局配置
参照:https://pyecharts.org/#/zh-cn/global_options,所需功能可在该网站查找,参照下面代码格式举一反三实现所需。
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.faker import Faker
x = ["哈士奇", "泰迪", "金毛", "柯基", "吉娃娃", "牧羊犬"]
y1 = [140, 136, 144, 120, 119, 150]
y2 = [130, 146, 24, 110, 79, 110]
bar = Bar()
bar.add_xaxis(x)
bar.add_yaxis("商家A", y1)
bar.add_yaxis("商家B", y2)
#bar.reversal_axis() #横向
#全局配置:标题,坐标轴,图例
bar.set_global_opts(title_opts=opts.TitleOpts(title="宠物狗的销售价格", subtitle="两个商家的对比",
title_link = "https://www.baidu.com",
pos_left = 'center'),
xaxis_opts=opts.AxisOpts( name_rotate = 90),
datazoom_opts = opts.DataZoomOpts(is_show = True),
legend_opts = opts.LegendOpts(pos_left = "right"))
bar.render_notebook()
图:
二、系列配置项
- set_series_opts
from pyecharts import options as opts from pyecharts.charts import Bar from pyecharts.faker import Faker x = ["哈士奇", "泰迪", "金毛", "柯基", "吉娃娃", "牧羊犬"] y1 = [140, 136, 144, 120, 119, 150] y2 = [130, 146, 24, 110, 79, 110] bar = Bar() bar.add_xaxis(x) bar.add_yaxis("商家A", y1) bar.add_yaxis("商家B", y2) #全局配置:标题,坐标轴,图例 bar.set_global_opts(title_opts=opts.TitleOpts(title="宠物狗的销售价格", subtitle="两个商家的对比", title_link = "https://www.baidu.com", pos_left = 'center'), xaxis_opts=opts.AxisOpts( name_rotate = 90), datazoom_opts = opts.DataZoomOpts(is_show = True), legend_opts = opts.LegendOpts(pos_left = "right")) #系列配置项; bar.set_series_opts(label_opts = opts.LabelOpts(is_show = False), markpoint_opts = opts.MarkPointOpts( data = [ opts.MarkPointItem(type_ = "max", name = "最大值"), opts.MarkPointItem(type_ = "min", name = "最小值"), # ])) bar.render_notebook()
三、除了上面两种方式之外,还可以直接放在x 轴或者y 轴上进行修饰
from pyecharts import options as opts from pyecharts.charts import Bar from pyecharts.faker import Faker x = ["哈士奇", "泰迪", "金毛", "柯基", "吉娃娃", "牧羊犬"] y1 = [140, 136, 144, 120, 119, 150] y2 = [130, 146, 24, 110, 79, 110] bar = Bar() bar.add_xaxis(x) bar.add_yaxis("商家A", y1,itemstyle_opts = opts.ItemStyleOpts(color = "blue")) bar.add_yaxis("商家B", y2) #全局配置:标题,坐标轴,图例 bar.set_global_opts(title_opts=opts.TitleOpts(title="宠物狗的销售价格", subtitle="两个商家的对比", title_link = "https://www.baidu.com", pos_left = 'center'), xaxis_opts=opts.AxisOpts( name_rotate = 90), datazoom_opts = opts.DataZoomOpts(is_show = True), legend_opts = opts.LegendOpts(pos_left = "right")) #系列配置项; bar.set_series_opts(label_opts = opts.LabelOpts(is_show = False), markpoint_opts = opts.MarkPointOpts( data = [ opts.MarkPointItem(type_ = "max", name = "最大值"), opts.MarkPointItem(type_ = "min", name = "最小值"), # ])) bar.render_notebook()
四、我们再来看一些其他的具体示例
1.主题(全局,InitOpts)
pyecharts内置提供了10+种不同的风格,另外也提供了便捷的定制主题的方法
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.globals import ThemeType
x = ["哈士奇", "泰迪", "金毛", "柯基", "吉娃娃", "牧羊犬"]
y1 = [140, 136, 144, 120, 119, 150]
y2 = [130, 146, 24, 110, 79, 110]
bar = Bar(init_opts = opts.InitOpts(theme=ThemeType.WALDEN))
bar.add_xaxis(x)
bar.add_yaxis("商家A", y1)
bar.add_yaxis("商家B", y2)
#全局配置:标题,坐标轴,图例
bar.set_global_opts(title_opts=opts.TitleOpts(title="宠物狗的销售价格", subtitle="两个商家的对比",
title_link = "https://www.baidu.com",
pos_left = 'center'),
xaxis_opts=opts.AxisOpts( name_rotate = 90),
datazoom_opts = opts.DataZoomOpts(is_show = True),
legend_opts = opts.LegendOpts(pos_left = "right"))
#系列配置项;
bar.set_series_opts(label_opts = opts.LabelOpts(is_show = False),
markpoint_opts = opts.MarkPointOpts(
data = [
opts.MarkPointItem(type_ = "max", name = "最大值"),
opts.MarkPointItem(type_ = "min", name = "最小值"), #
]))
bar.render_notebook()