欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Pyecharts

程序员文章站 2022-05-27 09:34:41
...

Pyecharts

绘制一个柱状图

from pyecharts.faker import Faker  # 数据集
from pyecharts import options as opts  # 配置
from pyecharts.charts import Bar  # 实例化

bar = Bar()
# x,y 内添加的必须是list, list类型必须是python基础类型
bar.add_xaxis(["衬衫", "毛衣", "领带", "裤子", "风衣", "高跟鞋", "袜子"])
bar.add_yaxis("商家A", [114, 55, 27, 101, 125, 27, 105])
bar.add_yaxis("商家B", [57, 134, 137, 129, 145, 60, 49])
bar.set_global_opts(title_opts=opts.TitleOpts(title="某商场销售情况"))
bar.render('this is a test html.html')

堆叠柱状图

  • 切换皮肤
  • 旋转轴标签
  • stack堆叠
  • 添加mark点, line辅助线
  • x轴y轴交换
from pyecharts.faker import Faker  # 数据集
from pyecharts import options as opts  # 配置
from pyecharts.globals import ThemeType  # 主题颜色
from pyecharts.charts import Bar  # 实例化

# 改变皮肤, 尺寸
bar = Bar(init_opts=opts.InitOpts(
                                theme=ThemeType.DARK,
                                width='800px',
                                height="600px"))
# x,y 内添加的必须是list, list内数据类型必须是python基础类型, 如果是numpy数组需要转成list
bar.add_xaxis(Faker.choose())
# 堆叠柱状图 stack='stack1'表示堆叠在stack1这一组里面
bar.add_yaxis('商家A',Faker.values(),stack='stack1')
bar.add_yaxis('商家B',Faker.values(),stack='stack1')
bar.add_yaxis('商家C',Faker.values(),stack='stack2')
bar.add_yaxis('商家D',Faker.values(),stack='stack2')
# 添加标题 副标题
bar.set_global_opts(title_opts=opts.TitleOpts(title="Bar_基本示例",subtitle="我是副标题"),
                    # 旋转x轴角度
                    xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=30)))
# 添加最大,最小点, 平均线
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="最小值"),
        ]
    ),
    markline_opts=opts.MarkLineOpts(
        data=[
            opts.MarkLineItem(type_="average",name="平均值"),
        ]
    )
)
# x,y轴交换
bar.reversal_axis()
bar.render(R'F:\1000篇技术博客\pyecharts\chart1.html')

窗口滑块

from pyecharts.faker import Faker  # 数据集
from pyecharts import options as opts  # 配置
from pyecharts.globals import ThemeType  # 主题颜色
from pyecharts.charts import Bar  # 实例化



# 改变皮肤, 尺寸
bar = Bar(init_opts=opts.InitOpts(
    theme=ThemeType.DARK,
    width='800px',
    height="600px"))
bar.add_xaxis(Faker.days_attrs)
bar.add_yaxis("商家A",Faker.days_values)
bar.set_global_opts(
    title_opts=opts.TitleOpts(
        title="Bar_基本示例",
        subtitle="我是副标题"),
    # 添加窗口滑块效果
    datazoom_opts=[opts.DataZoomOpts()]
    )
bar.render(R'F:\1000篇技术博客\pyecharts\chart1.html')

3D柱状图

import random
from pyecharts.faker import Faker  # 数据集
from pyecharts import options as opts  # 配置
from pyecharts.charts import Bar3D  # 实例化


bar3d = Bar3D()
# 需要三维的数据 ,
data = [(i,j,random.randint(0,12)) for i in range(24) for j in range(6)]
bar3d.add(
    "",
    data,
    xaxis3d_opts=opts.Axis3DOpts(Faker.clock,type_='category'),
    yaxis3d_opts=opts.Axis3DOpts(Faker.week_en,type_='category'),
    zaxis3d_opts=opts.Axis3DOpts(type_='value'),
)
bar3d.set_global_opts(
    visualmap_opts=opts.VisualMapOpts(max_=20),
    title_opts=opts.TitleOpts(title="Bar3D_基本示例")
    )
bar3d.render(R'F:\1000篇技术博客\pyecharts\chart1.html')

折线 / 面积图

from pyecharts.faker import Faker  # 数据集
from pyecharts import options as opts  # 配置
from pyecharts.charts import Line  # 实例化


line = Line()
line.add_xaxis(Faker.choose())
# is_smooth 折现变平滑
line.add_yaxis("商家A",Faker.values(),
               is_smooth=True,
               areastyle_opts=opts.AreaStyleOpts(
                   # opacity 透明度
                   opacity=0.2,
                   # color 填充区域颜色
                   color='#000'
               ))
line.add_yaxis("商家B",Faker.values())
line.set_global_opts(title_opts=opts.TitleOpts(title='Line_基本示例'))
line.render(R'F:\1000篇技术博客\pyecharts\chart1.html')

饼图

from pyecharts.faker import Faker  # 数据集
from pyecharts import options as opts  # 配置
from pyecharts.charts import Pie  # 实例化


pie = Pie()
# radius 内环和外环半径, 列表类型
# rosetype 玫瑰饼图
# resetype = 'area' , 所有扇区圆心角相同, 仅通过半径展现圆心角大小
#          = 'radius' , 圆心角展现百分比 , 
pie.add("",[list(z) for z in zip(Faker.choose(),Faker.values())],
        radius=["40%","75%"],
        rosetype='radius')
pie.set_global_opts(title_opts=opts.TitleOpts(title='Pie_基本示例'))
pie.set_series_opts(label_opts=opts.LabelOpts(formatter='{b}:{c}'))
pie.render(R'F:\1000篇技术博客\pyecharts\chart1.html')

涟漪效果散点图

from pyecharts.faker import Faker  # 数据集
from pyecharts import options as opts  # 配置
from pyecharts.charts import EffectScatter  # 实例化
from pyecharts.globals import SymbolType

effect_scatter = EffectScatter()
effect_scatter.add_xaxis(Faker.choose())
effect_scatter.add_yaxis("",
                         Faker.values(),
                         symbol=SymbolType.ARROW)
effect_scatter.set_global_opts(title_opts=opts.TitleOpts(title="EffectScatter_基本示例"))
effect_scatter.render(R'F:\1000篇技术博客\pyecharts\chart1.html')

漏斗图

from pyecharts.faker import Faker  # 数据集
from pyecharts import options as opts  # 配置
from pyecharts.charts import Funnel  # 实例化

funnel = Funnel()
funnel.add(
    "用户转化率",
    [list(z) for z in zip(Faker.choose(),Faker.values())],
    # position = 'inside' 图例名放在图形中间
    label_opts=opts.LabelOpts(position='inside')
)
funnel.set_global_opts(title_opts=opts.TitleOpts(title="Funnel_基本示例"))
funnel.render(R'F:\1000篇技术博客\pyecharts\chart1.html')

地理坐标系

from pyecharts.faker import Faker  # 数据集
from pyecharts import options as opts  # 配置
from pyecharts.charts import Geo  # 实例化

geo = Geo()
geo.add_schema(maptype="china")
geo.add('geo',[list(z) for z in zip(Faker.provinces,Faker.values())],
        type_='heatmap')
geo.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
geo.set_global_opts(
    visualmap_opts=opts.VisualMapOpts(),
    title_opts=opts.TitleOpts(title="Geo_基本示例")
)
geo.render(R'F:\1000篇技术博客\pyecharts\chart1.html')

水球图

from pyecharts import options as opts  # 配置
from pyecharts.charts import Liquid  # 实例化

liquid = Liquid()
liquid.add('Liquid',[0.7,0.6,0.5])
liquid.set_global_opts(title_opts=opts.TitleOpts(title="Liquid_基本示例"))
liquid.render(R'F:\1000篇技术博客\pyecharts\chart1.html')

雷达图

词云图