用pyecharts画柱状图和饼状图

用pyecharts画柱状图

from pyecharts import options as opts

from pyecharts.charts import Bar

hamburger = {
    '热量' : 250,
    '脂肪' : 10,
    '碳水化合物' : 20,
    '蛋白质' : 10
}

x = ['汉堡','饭团','粥','鸡肉']
y1 = [250,200,50,255]
y2 = [10,11,1.5,15]
y3 = [20,21,8,10]
y4 = [10,6,2.5,21]

c = Bar()
# x轴
c.add_xaxis(x)
# y轴
# stack值相同柱状图就会堆叠
c.add_yaxis("热量", y1, stack="stack1")
c.add_yaxis("脂肪", y2, stack="stack1")
c.add_yaxis("碳水化合物", y3, stack="stack1")
c.add_yaxis("蛋白质", y4, stack="stack1")

# reversal_axis方法是反转xy轴
# c.reversal_axis()
# False则不显示y轴的值,默认为显示
c.set_series_opts(label_opts=opts.LabelOpts(is_show=False),
                  #markpoint_opts指定类型
             #      markpoint_opts=opts.MarkPointOpts(
             #            data = [
             #                opts.MarkPointItem(type_="max", name="最大值"),
             #                opts.MarkPointItem(type_="min", name="最小值"),
             #                 ]
             # )
)
c.set_global_opts(title_opts=opts.TitleOpts(title="主食营养分布"),
                  # datazoom_opts水平显示,vertical垂直显示
                  # datazoom_opts=opts.DataZoomOpts(orient="vertical")
                  )
#c.render_notebook()
c.render('主食营养分布.html')

用pyechats画饼状图
from pyecharts import options as opts

from pyecharts.charts import Bar,Pie
c = Pie()
c.add(
        "",
        [list(z) for z in zip(["热量", "脂肪",'碳水化合物','蛋白质'],
                              [31,2,1.5, 3])],
        # center为圆心坐标
        center=["20%", "50%"],
        # 60为内半径,80为外半径
        radius=[60, 80],
    )
c.add(
        "",
        [list(z) for z in zip(["热量", "脂肪",'碳水化合物','蛋白质'],
                              [100,4,12, 4])],
        center=["55%", "50%"],
        radius=[60, 80],
    )
c.set_global_opts(
        title_opts=opts.TitleOpts(title="豆浆(左)咖啡(右)"),
        legend_opts=opts.LegendOpts(
            type_="scroll", pos_top="200%", pos_left="80%", orient="vertical"
        ),
    )
# 设置显示百分比
c.set_series_opts(
        tooltip_opts=opts.TooltipOpts(
            trigger="item", formatter="{a} 
{b}: {c} ({d}%)"
)) c.render('豆浆咖啡.html')

你可能感兴趣的:(案例)