Day13-pyecharts的使用

Day13-pyecharts的使用

1. 柱状图

from pyecharts.charts import Bar
from pyecharts import options
# 1. 创建图表对象
bar = Bar()
# 2. 添加数据
# 1) 添加x轴上的数据
bar.add_xaxis(['1季度', '2季度', '3季度', '4季度'])

# 2) 添加y轴上的数据
bar.add_yaxis('销售额', [890, 1023, 509, 1500],
              # 设置销售额对应的柱子的颜色
              color='red',
              # is_show_background=True,
              # bar_width=40
              bar_max_width=100,
              bar_min_width=40
              )
bar.add_yaxis('销量', [208, 300, 301, 400])
# 3. 柱状图相关的配置
# 1) 全局配置
bar.set_global_opts(
    # 设置标题
    title_opts=options.TitleOpts(
        title='全年xxx手机的销售信息',
        title_link='https://www.baidu.com',
        subtitle='销售额和销量信息',
        pos_right='350',
        text_align='center'
    ),
    # 设置图例
    legend_opts=options.LegendOpts(
        is_show=False,
        pos_right='90',
        item_width=20,
        item_height=10
    ),
    # 设置工具箱
    toolbox_opts=options.ToolboxOpts(
        is_show=True
    ),
    # 设置区域缩放
    datazoom_opts=options.DataZoomOpts(
        is_show=True
    ),
    # 设置x轴配置
    xaxis_opts=options.AxisOpts(
        name='时间'
    ),
    # 设置y轴配置
    yaxis_opts=options.AxisOpts(
        name='销售额(万元)/销量'
    )
)

# 2) 系列配置
bar.set_series_opts(
    # 设置标签
    label_opts=options.LabelOpts(
        is_show=True,
        position='top',      # 'top'、'left'、'right'
        # color='rgb(255,0,0)',
        # color='red',
        # color='#ff0000'
    ),
    # 设置标记点
    markpoint_opts=options.MarkPointOpts(
        data=[options.MarkPointItem(name='最大值', type_='max'), options.MarkPointItem(name='最小值', type_='min')]
    )
)

# 3) 数据配置
# 数据配置只能在添加数据的时候设置
# 4. 渲染图表(制图)
bar.render('files/01柱状图.html')

2. 折线图

from pyecharts.charts import Line
from pyecharts import options
# 1.创建折线图对象
line = Line()
# 2. 添加数据
line.add_xaxis(['1季度', '2季度', '3季度', '4季度'])
line.add_yaxis('服装', [189, 340, 299, 412], is_smooth=True)
line.add_yaxis('电子产品', [89, 109, 77, 56])
# 3. 图表设置
# 1) 全局设置
line.set_global_opts(
    title_opts=options.TitleOpts(
        title='xxx商城各大商品的销售情况',
        pos_left='350'
    ),
    legend_opts=options.LegendOpts(
        pos_right=80
    ),
    xaxis_opts=options.AxisOpts(
        name='时间'
    ),
    yaxis_opts=options.AxisOpts(
        name='销售额(万元)'
    )
)

# 2) 系列配置
line.set_series_opts(
    label_opts=options.LabelOpts(
        is_show=True
    ),
    markline_opts=options.MarkLineOpts(
        data=[options.MarkLineItem(type_='average', name='平均销售额')]
    )
)
# 4. 渲染
line.render('files/02折线图.html')

3. 饼图

from pyecharts.charts import Pie
from pyecharts import options
# 1. 创建饼图对象
pie = Pie()
# 2. 添加数据
pie.add('手机销量',
        [('Apple', 890), ('华为', 1002), ('小米', 409), ('OPPO', 102), ('魅族', 98)],
        # 分别设置圆的内圆半径和外圆半径
        radius=(50, 100),
        # radius=('30%', '70%')
        # 让外圆环不规则
        rosetype='radius'
        )
# 3. 图表的设置
pie.set_global_opts(
    legend_opts=options.LegendOpts(
        is_show=False
    ),
    title_opts=options.TitleOpts(
        title='xxx商城6月手机销售情况',
        pos_left='350'
    )
)

pie.set_series_opts(
    label_opts=options.LabelOpts(
        # {b}   -   数据名称
        # {d}   -   百分比比值
        formatter='{b}({d}%)'
        # formatter='{b}:{d}%'
        # formatter='{b}占百分之{d}%'
    )
)
# 4. 渲染图表
pie.render('files/03饼图.html')

4. 地图

rom pyecharts.charts import Map
from pyecharts import options
# ================= 1. 中国地图 =================
# 1. 创建地图对象
map1 = Map()

# 2. 添加数据
map1.add(
    '房价',
    [('四川省', 8000), ('重庆市', 7923), ('河南省', 5672), ('北京市', 53994),
     ('上海市', 47789), ('新疆维吾尔自治区', 3982), ('广东省', 6000), ('黑龙江省', 4632)],
    zoom=1.5,
    min_scale_limit=1,
    max_scale_limit=2
)

# 3. 添加配置
map1.set_global_opts(
    visualmap_opts=options.VisualMapOpts(
        is_show=True,
        min_=3000,
        max_=65000,
        # 让颜色分段
        is_piecewise=True,
        # 设置每一段的数值范围
        pieces=[
            {'min': 3000, 'max': 4999},
            {'min': 5000, 'max': 9999},
            {'min': 10000, 'max': 29999},
            {'min': 30000, 'max': 59999},
            {'min': 60000, 'max': 65000}
        ]
    ),
    legend_opts=options.LegendOpts(
        is_show=False
    ),
    title_opts=options.TitleOpts(
        title='中国各省平均房价',
        pos_right='400'
    )
)

# 4. 渲染图表
map1.render('files/04中国地图.html')
# ================= 2. 四川地图 =================
map2 = Map()

map2.add('房价',
         [('成都市', 12000), ('乐山市', 7000)],
         maptype='四川')

map2.render('files/05四川地图.html')
# ================= 3. 世界地图 =================
map3 = Map()

map3.add('房价', [('China', 2000)], maptype='world')

map3.set_series_opts(
    label_opts=options.LabelOpts(
        is_show=False
    )
)

map3.render('files/06世界地图.html')
# ================= 4. 成都地图 =================
map4 = Map()
map4.add('房价', [('青羊区', 17000)], maptype='成都')
map4.render('files/07成都地图.html')

5. 词云

from pyecharts.charts import WordCloud
from pyecharts import options

# 1. 创建图表对象
wc = WordCloud()
# 2. 添加数据
wc.add(
    '游戏',
    [('王者荣耀', 82933), ('英雄联盟', 109283), ('阴阳师', 7282),
     ('原神', 1928), ('开心消消乐', 8192), ('和平精英', 76788),
     ('飞机大战', 109), ('蛋仔排队', 678), ('烈梦者', 302),
     ('红色警戒', 19), ('魔兽', 68922), ('刀塔', 3192), ('QQ农场', 87),
     ('植物大战僵尸', 67), ('地平线', 33), ('神庙逃亡',10), ('节奏大师', 2033),
     ('找你妹', 9), ('大富翁', 300), ('保卫萝卜', 9012)],
    # cardioid(心脏线)、diamond(钻石)、triangle(三角形)、triangle-forward、pentagon(五边形)、star(星形)
    shape='star'
    # word_size_range=(5, 200)
)

wc.render('files/08词云.html')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值