pyecharts 使用直方图
from pyecharts import *
import random
class MyBar(Bar):
"""直方图"""
def __init__(self, title, subhead=""):
"""
:param title: 主标题
:param subhead: 副标题
"""
self.bar = Bar(title, subhead)
def simple_broken_bar_diagram(self, x_name, x, y_data, filename, is_create, is_stacking=False):
"""
根据已知数据进行直接输出图像
:param x_name: x轴名称list()
:param x: x轴数据list()
:param y_data: y轴数据list()
:param filename: 文件名
:param is_create: 是否输出文件
:param is_stacking: 是否堆叠显示
:return:
"""
style = dict(
xaxis_max=len(x),
yaxis_max=max(max(y_data)) + max(max(y_data)) * 0.2,
is_datazoom_show=False,
datazoom_type='both',
xaxis_interval=0,
xaxis_rotate=30,
yaxis_rotate=30,
mark_line=["min", "max"],
mark_point=["average"],
)
if is_stacking:
style['is_stack'] = True
for i in range(len(y_data)):
self.bar.add(x_name[i], x, y_data[i], **style)
if is_create:
self.bar.render("{}.html".format(filename))
def broadcast_map(self, bar_dict, filename):
"""
根据 bar 键值对
:param bar_dict:
:param filename:
:return:
"""
time_line = Timeline(is_auto_play=True, timeline_bottom=0)
for k, v in bar_dict.items():
time_line.add(v, k)
time_line.render("{}.html".format(filename))
if __name__ == '__main__':
bar1 = MyBar("主标题", "副标题")
x = ['Java', 'C', 'C++', 'Python']
data_2018 = [1, 2, 3, 4]
data_2013 = [2, 1, 4, 7]
asd = [data_2013, data_2018]
bar1.simple_broken_bar_diagram(x=x, x_name=['2013', '2018'], y_data=asd, filename="bar1", is_create=True)
bar2 = MyBar("主标题", "副标题")
attr = ["{}月".format(i) for i in range(1, 13)]
v1 = [random.randint(1, 40) for _ in range(12)]
v2 = [random.randint(1, 40) for _ in range(12)]
v = [v1, v2]
bar2.simple_broken_bar_diagram(x_name=['增加', '减少'], x=attr, y_data=v, filename='bar2', is_create=True,
is_stacking=False)
bar3 = MyBar("主标题", "副标题")
attr = ["{}月".format(i) for i in range(1, 13)]
v1 = [random.randint(1, 40) for _ in range(12)]
v2 = [random.randint(1, 20) for _ in range(12)]
v = [v1, v2]
bar3.simple_broken_bar_diagram(x_name=['增加', '减少'], x=attr, y_data=v, filename='bar2', is_create=True,
is_stacking=False)
bar4 = MyBar("主标题", "副标题")
bar_all = {
"a1": bar2.bar,
"a2": bar3.bar
}
bar4.broadcast_map(bar_dict=bar_all, filename="轮播图")