pyecharts数据可视化(python柱状图饼图)

柱状图饼图

  • 简单柱状图:
    • 叠加柱状图
    • 柱状图反转
    • slider-水平
    • 柱状图标签过长解决
    • markPoint效果
  • 饼图
    • 环形饼图
    • 多饼图显示
    • 南丁格尔玫瑰图

 pip3 install --index-url https://pypi.douban.com/simple pyecharts

简单柱状图:

pyecharts数据可视化(python柱状图饼图)_第1张图片
代码

from pyecharts.charts import Bar 
bar = Bar() 
bar.add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]) 
bar.add_yaxis("商家A", [5, 20, 36, 10, 75, 90]) 
bar.render()# render 会生成本地 HTML 文件,默认会在当前目录生成 render.html 文件 
# 也可以传入路径参数,如 bar.render("mycharts.html") 

from pyecharts.charts import Bar
from pyecharts import options as opts
bar = (
    Bar()
    .add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
    .add_yaxis("商家A", [5, 20, 36, 10, 75, 90])
    .add_yaxis("商家B", [6, 20, 38, 4, 70, 95])
    .set_global_opts(title_opts=opts.TitleOpts(title="我的第一个图表", subtitle="这里是副标题"))
)
bar.render_notebook()

pyecharts数据可视化(python柱状图饼图)_第2张图片

叠加柱状图

from pyecharts.charts import Bar
from pyecharts import options as opts
bar = (
    Bar()
    .add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
    .add_yaxis("商家A", [5, 20, 36, 10, 75, 90],stack="stack1")
    .add_yaxis("商家B", [6, 20, 38, 4, 70, 95],stack="stack1")
    .set_global_opts(title_opts=opts.TitleOpts(title="我的第一个图表", subtitle="这里是副标题"))
)
bar.render_notebook()

pyecharts数据可视化(python柱状图饼图)_第3张图片

柱状图反转

from pyecharts.charts import Bar
from pyecharts import options as opts
bar = (
    Bar()
    .add_xaxis(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"])
    .add_yaxis("商家A", [5, 20, 36, 10, 75, 90],stack="stack1")
    .add_yaxis("商家B", [6, 20, 38, 4, 70, 95],stack="stack1")
    .set_global_opts(title_opts=opts.TitleOpts(title="我的第一个图表", subtitle="这里是副标题"))
    .reversal_axis()
    .set_series_opts(label_opts=opts.LabelOpts(position="right"))
    .set_global_opts(title_opts=opts.TitleOpts(title="Bar-翻转 XY 轴"))
)
bar.render_notebook()

pyecharts数据可视化(python柱状图饼图)_第4张图片

slider-水平

import random
from pyecharts.charts import Bar
from pyecharts import options as opts
attr = ["{}天".format(i) for i in range(30)]
v1 = [random.randint(1, 30) for _ in range(30)]
bar = (
    Bar()
    .add_xaxis(attr)
    .add_yaxis("",v1)
    .set_global_opts(title_opts=opts.TitleOpts(title="Bar-DataZoom(slider-水平)"),
            datazoom_opts=opts.DataZoomOpts(),)
)
bar.render_notebook()

pyecharts数据可视化(python柱状图饼图)_第5张图片

柱状图标签过长解决

from pyecharts.charts import Bar
from pyecharts import options as opts

bar = (
    Bar()
       .add_xaxis(
            [
                "名字很长的X轴标签1",
                "名字很长的X轴标签2",
                "名字很长的X轴标签3",
                "名字很长的X轴标签4",
                "名字很长的X轴标签5",
                "名字很长的X轴标签6",
            ]
        )
        .add_yaxis("商家A", [10, 20, 30, 40, 50, 40])
        .add_yaxis("商家B", [20, 10, 40, 30, 40, 50])
        .set_global_opts(
            xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)),title_opts=opts.TitleOpts(title="Bar-旋转X轴标签", subtitle="解决标签名字过长的问题"),
        )
    )
bar.render_notebook()

pyecharts数据可视化(python柱状图饼图)_第6张图片

markPoint效果

from pyecharts.faker import Faker
from pyecharts.charts import Bar
from pyecharts import options as opts

bar = (
    Bar()
        .add_xaxis(Faker.choose())
        .add_yaxis("商家A", Faker.values())
        .add_yaxis("商家B", Faker.values())
        .set_global_opts(title_opts=opts.TitleOpts(title="Bar-MarkPoint(指定类型)"))
        .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="最小值"),
                    opts.MarkPointItem(type_="average", name="平均值"),
                ]
            ),
        )
)
bar.render_notebook()

pyecharts数据可视化(python柱状图饼图)_第7张图片

饼图

pyecharts数据可视化(python柱状图饼图)_第8张图片

from pyecharts.charts import Pie   #从pyecharts中导入Pie类
from pyecharts import options as opts  #使用 options 配置项
pie = (
    Pie()
    .add("",[list(z) for z in zip(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
    [5, 20, 36, 10, 75, 90])])
    .set_global_opts(title_opts=opts.TitleOpts(title="饼图基本示例", subtitle="这里是副标题"))
   .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))#设置标签格式
)
pie.render_notebook()

环形饼图

pyecharts数据可视化(python柱状图饼图)_第9张图片

from pyecharts.charts import Pie#从pyecharts中导入line类
from pyecharts import options as opts#使用 options 配置项
pie = (
    Pie()
    .add("",[list(z) for z in zip(["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"],
    [5, 20, 36, 10, 75, 90])],radius=["40%", "75%"])#饼图内半径为40,外半径为75
.set_global_opts(title_opts=opts.TitleOpts(title="Pie-Radius"),
legend_opts=opts.LegendOpts(orient="vertical", pos_top="2%", pos_left="15%"
            ),)#图例采用vertical模式,放在距离顶部2%,左边15%处
   .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))#设置标签格式
)
pie.render( )

多饼图显示

pyecharts数据可视化(python柱状图饼图)_第10张图片

pie = (
    Pie()
        .add(
        "",
        [list(z) for z in zip(["剧情", "其他"], [25, 75])],
        center=["20%", "30%"],
        radius=[60, 80],
        label_opts=opts.LabelOpts(formatter="{b}: {d}%")
    )  # center控制圆心位置;除了使用set_series_opts也可单饼用label_opts;formatter用{d}表示百分比 

        .add(
        "",
        [list(z) for z in zip(["奇幻", "其他"], [24, 76])],
        center=["55%", "30%"],
        radius=[60, 80],
        label_opts=opts.LabelOpts(formatter="{b}: {d}%")
    )
        .set_global_opts(
        title_opts=opts.TitleOpts(title="Pie-多饼图基本示例"),
        legend_opts=opts.LegendOpts(
            type_="scroll", pos_top="20%", pos_left="80%", orient="vertical"
        ),
    )
)

pie.render()  #

南丁格尔玫瑰图

pyecharts数据可视化(python柱状图饼图)_第11张图片

def pie_rosetype() -> Pie:
    v = ["aa","bb","dd","qq","rrr"]
    vv = [10,40,20,15,25]
    c = (
        Pie()
        .add(
            "",
            [list(z) for z in zip(v, vv)],
            radius=[0, 100],
            center=["20%", "40%"],
            label_opts=opts.LabelOpts(is_show=False),
        ) 
        .add(
            "",
            [list(z) for z in zip(v, vv)],
            radius=[0, 100],
            center=["50%", "40%"],
            rosetype="area",
        )
        .add(
            "",
            [list(z) for z in zip(v, vv)],
            radius=[0, 100],
            center=["80%", "40%"],
            rosetype="radius",
        )
         .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
        .set_global_opts(title_opts=opts.TitleOpts(title="Pie-玫瑰图示例"))
    )
    return c
pie_rosetype().render_notebook()

你可能感兴趣的:(数据可视化,python,echarts,开发语言)