day10-pyecharts的使用

day10-pyecharts的使用

01柱状图

# 创建柱状图对象的类
from pyecharts.charts import Bar

# 1. 创建柱状图对象 
bar = Bar()

# 2. 添加数据
# 1) 添加x轴上的数据
bar.add_xaxis(['1季度', '2季度', '3季度', '4季度'])

# 2) 添加y轴上每一个选项对应的数据
bar.add_yaxis('销售额', [280, 219, 199, 320])
bar.add_yaxis('成本', [120, 110, 200, 150])

# 3. 制图
bar.render('files/柱状图1.html')

02柱状图的配置

from pyecharts.charts import Bar
from pyecharts import options

# 1. 创建图表对象
bar = Bar()

# 2. 添加数据
bar.add_xaxis(['1季度', '2季度', '3季度', '4季度'])

# 添加数据的时候可以单独针对某种数据进行相关配置
bar.add_yaxis(
    '销售额',
    [280, 219, 199, 320],
    # 设置销售额对应的柱子的颜色
    color='#ff01c6',
    # 设置柱子的宽度
    bar_width=40
)

bar.add_yaxis(
    '成本',
    [120, 110, 200, 150],
    # 设置销售额对应的柱子的颜色
    color='#3cc487',
    # 设置柱子的宽度
    bar_width=40
)

# 3. 添加全局配置
bar.set_global_opts(
    title_opts=options.TitleOpts(
        title='xxx公司2022年全年销售信息',
        # 标题链接
        title_link='https://www.baidu.com',
        subtitle='销售额数据和成本数据',
        # 副标题链接
        subtitle_link='https://www.jd.com',
        # 设置标题的为位置
        pos_right='-150',
        # 标题对齐方式
        text_align='right'  # 'left'、 'center' 、 'right'
    ),
    
    # 设置图例
    legend_opts=options.LegendOpts(
        # 显示或隐藏图例
        is_show=True,
        pos_left='0',
        item_width=20,
        item_height=10
    ),
    # 设置x轴
    xaxis_opts=options.AxisOpts(
        name='时间'
    ),
    # 设置y轴
    yaxis_opts=options.AxisOpts(
        name='销售额(万元)/成本(万元)'
    )
)

# 系列配置
bar.set_series_opts(
    # 设置标签
    label_opts=options.LabelOpts(
        is_show=True,
        # position='top',
        color='#151515',
        rotate=15
    ),
    # 添加最大值和最小值的标记点
    markpoint_opts=options.MarkPointOpts(
        data=[
            options.MarkPointItem('最大值', 'max'),
            options.MarkPointItem('最小值', 'min'),
        ]
    )
)

# 4. 渲染图表
bar.render('./files/柱状图2.html')

03折线图

from pyecharts.charts import Line
from pyecharts import options

# 1. 创建折线图对象
line = Line()

# 2. 添加数据
line.add_xaxis(['1季度', '2季度', '3季度', '4季度'])

line.add_yaxis(
    '销售额',
    [280, 219, 199, 320],
    # 设置线的颜色
    color='#ff01c6',
    # 是否平滑
    is_smooth=True
)

line.add_yaxis(
    '成本',
    [120, 110, 200, 150],
    color='#499c54',
    is_smooth=True
)

# 3. 全局配置
line.set_global_opts(
    title_opts=options.TitleOpts(
        title='xxx公司2023年全年销售信息',
        subtitle='销售额数据和成本数据',
        pos_right='200',
        text_align='center'
    ),
    legend_opts=options.LegendOpts(
        is_show=True,
        pos_right=90
    ),
    # 设置x轴
    xaxis_opts=options.AxisOpts(
        name='时间'
    ),
    yaxis_opts=options.AxisOpts(
        name='销售额(万元)/成本(万元)'
    )
)

# 4. 系列配置
line.set_series_opts(
    # 标记平均线
    markline_opts=options.MarkLineOpts(
        data=[options.MarkPointItem('平均值', 'average')]
    ),
    markpoint_opts=options.MarkPointOpts(
        data=[
            options.MarkPointItem('最大值', 'max')
        ]
    ),
    label_opts=options.LabelOpts(
        position='middle'
    )
)

# 5. 渲染
line.render('./files/折线图.html')

04饼图

from pyecharts.charts import Pie
from pyecharts import options

# 1. 创建饼图对象
pie = Pie()

# 2. 添加数据
data = [('Apple', 512), ('HuaWei', 600), ('XiaoMi', 320), ('SanXing', 80), ('Oppo', 120), ('ViVo', 210)]

pie.add(
    '销量',
    data,
    # 设置内半径和外半径的大小
    radius=[50, 150],
    # 让外半径大小根据数据的占比自动调整
    rosetype='radius'
)

# 3. 添加全局配置
pie.set_global_opts(
    legend_opts=options.LegendOpts(
        is_show=False
    ),
    title_opts=options.TitleOpts(
        title='2023年各手机销量汇总',
        pos_right='250',
        text_align='center'
    )
)

# 4. 添加系列配置
pie.set_series_opts(
    # 设置数据的显示格式
    label_opts=options.LabelOpts(
        # {b} - 数据名称  {d} - 一个数据在整个数据中百分比比值
        formatter='{b}: {d}%'
    )
)

# 5. 渲染
pie.render('./files/饼图.html')

05地图

from pyecharts.charts import Map
from pyecharts import options

# 1. 创建地图对象
map1 = Map()

# 2. 添加数据
data = [('四川省', 67895467), ('重庆市', 56743543), ('吉林省', 23987564), ('贵州省', 31928542), ('青海省', 23451234), ('黑龙江省', 12765498)]

map1.add(
    '人口数量',
    data,
    maptype='china',
    # 限制缩放的范围
    min_scale_limit=0.7,
    max_scale_limit=2.5,
    zoom=1.6,
    pos_top=145,
    pos_left=400
)

# 4. 配置地图
map1.set_global_opts(
    # 在地图上根据数据的大小涂色
    visualmap_opts=options.VisualMapOpts(
        is_show=True,
        min_=10000000,
        max_=70000000,
        is_piecewise=True
    ),
    title_opts=options.TitleOpts(
        title='中国地图',
        pos_right=250,
        pos_top=30,
        text_align='center',
    ),
    legend_opts=options.LegendOpts(
        is_show=False
    )
)

# 5. 渲染地图
map1.render('./files/地图.html')

06世界地图

from pyecharts.charts import Map
from pyecharts import options

# 1. 创建地图对象
map1 = Map()

# 2. 添加数据
map1.add(
    '人均GDP',
    [('China', 19820), ('Russia', 45654),('Brazil', 34212), ('Sudan', 23143), ('Canada', 66587), ('Australia', 89076)],
    maptype='world',
    min_scale_limit=1,
    max_scale_limit=1.7,
    zoom=1,
    pos_left=140,
    pos_top=100
)

# 3. 添加全局配置
map1.set_series_opts(
    label_opts=options.LabelOpts(
        is_show=False
    )
)

# 4. 添加系列配置
map1.set_global_opts(
    visualmap_opts=options.VisualMapOpts(
        is_show=True,
        min_=10000,
        max_=90000,
        is_piecewise=True
    ),
    legend_opts=options.LegendOpts(
        is_show=False
    ),
    title_opts=options.TitleOpts(
        title='世界各国人均GDP汇总',
        pos_right=175,
        pos_top=30,
        text_align='center'
    )
)

# 5. 渲染
map1.render('./files/世界地图.html')

07省份地图

from pyecharts.charts import Map
from pyecharts import options

# 1. 创建地图对象
map1 = Map()

# 2. 添加数据
map1.add(
    '平均薪资',
    [('成都市', 9000), ('绵阳市', 8500),('宜宾市', 8000), ('乐山市', 7000), ('广元市', 6500),('甘孜藏族自治州', 5000)],
    maptype='四川',
    # 限制缩放的范围
    min_scale_limit=0.7,
    max_scale_limit=2.5,
    zoom=1.1,
    pos_top=70,
    pos_left=320
)

# 3. 添加全局配置
map1.set_global_opts(
    visualmap_opts=options.VisualMapOpts(
        is_show=True,
        max_=10000,
        min_=3000,
        is_piecewise=True,
        pieces=[
            {"min": 5000, "max": 6000, "label": "5k-6k", "color": "#9d9ff2"},
            {"min": 6000, "max": 7000, "label": "6k-7k", "color": "#ffd04e"},
            {"min": 7000, "max": 8000, "label": "7k-8k", "color": "#4feb7a"},
            {"min": 8000, "max": 9000, "label": "8k-9k", "color": "#d16662"},
            {"min": 10000, "max": 12000, "label": "10k-12k", "color": "#e63f32"}
        ]
    ),
    title_opts=options.TitleOpts(
        title='20xx年四川省平均薪资',
        text_align='center',
        pos_right=176,
        pos_top=10
    ),
    legend_opts=options.LegendOpts(
        is_show=False
    )
)

# 4. 渲染
map1.render('./files/四川地图.html')

08词云

from pyecharts.charts import WordCloud
from pyecharts import options

# 1. 创建词云对象
wc = WordCloud()

# 2. 添加数据
wc.add(
    '游戏玩家数量',
    [('王者荣耀', 9894), ('英雄联盟', 7821), ('原神', 2983), ('开心消消乐', 12099), ('保卫萝卜', 892), ('CSGO', 1290), ('永劫无间', 290), ('守望先锋', 120), ('和平精英', 15032), ('qq飞车', 782), ('穿越火线', 3909), ('诛仙', 109), ('红警', 345), ('第五人格', 642)],
    # cardioid(心脏线)、diamond(钻石)、triangle(三角形)、triangle-forward、pentagon(五边形)、star(星形)
    shape='cardioid',
    width='200',
    height='200'
)

# 3. 渲染
wc.render('./files/词云.html')

09作业: 饼图

# 制作不同渠道对应的销售占比的饼图
# 1. 准备数据
import openpyxl
from pyecharts.charts import Pie
from pyecharts import options

# 获取数据对应的工作表
sheet = openpyxl.open('./files/2020年销售数据.xlsx')['data']

# 获取最大行数
m_r = sheet.max_row

# 收集每一行第三列和第七列的数据
# =============方法一===================
data = []
for row in range(3, m_r + 1):
    cell3 = sheet.cell(row, 3).value
    cell7 = sheet.cell(row, 7).value
    data.append((cell3, cell7))

new_data = {}
for x in data:
    value = new_data.get(x[0], 0)
    new_data[x[0]] = value + x[1]
    
# ==============方法二===================
data = {}
for row in range(3, m_r + 1):
    name = sheet.cell(row, 3).value
    num = sheet.cell(row, 7).value
    value = data.get(name, 0)
    data[name] = value + num
    
# 2. 作图
pie = Pie()
pie.add(
    '销量',
    list(data.items()),
    radius=(80, 200),
    rosetype='radius'
)

pie.set_series_opts(
    label_opts=options.LabelOpts(
        formatter='{b}:{d}%'
    )
)

pie.set_global_opts(
    legend_opts=options.LegendOpts(
        is_show=False
    ),
    title_opts=options.TitleOpts(
        title='2020年各大渠道商品销售占比'
    )
)

pie.render('./files/各渠道销量占比.html')

10作业: 柱状图

# 求不同品牌商品的平均售价
# 1. 准备数据
import openpyxl
from pyecharts.charts import Bar
from pyecharts import options
sheet = openpyxl.open('./files/2020年销售数据.xlsx')['data']
m_r = sheet.max_row

data = []
for row in range(3, m_r + 1):
    value1 = sheet.cell(row, 5).value
    value2 = sheet.cell(row, 6).value
    data.append((value1, value2))
    
new_data = {}
for x in data:
    # print(x)
    # x : ('八匹马', 99)
    result = new_data.get(x[0], (0, 0))
    price = result[0]
    num = result[1]
    new_data[x[0]] = (price + x[1], num + 1)
    
x_data = []
y_data = []
for key in new_data:
    value = new_data[key]
    y_data.append(value[0] // value[1])
    x_data.append(key)
    
# 2. 作图
bar = Bar()
bar.add_xaxis(x_data)
bar.add_yaxis('平均价格', y_data)

bar.render('./files/品牌平均价格.html')

11作业: 地图

# 求不同区域的销量
# 1. 准备数据
import openpyxl
from pyecharts.charts import Map
from pyecharts import options

sheet = openpyxl.open('./files/2020年销售数据.xlsx')['data']
m_r = sheet.max_row

data = []
for row in range(3, m_r + 1):
    cell2 = sheet.cell(row, 2).value
    cell7 = sheet.cell(row, 7).value
    data.append((cell2, cell7))

new_data = {}
for x in data:
    value = new_data.get(x[0], 0)
    new_data[x[0]] = value + x[1]
print(new_data)
# 2. 作图
map1 = Map()
map1.add(
    '销量',
    list(new_data.items()),
    maptype='china',
    min_scale_limit=0.7,
    max_scale_limit=2.5,
    zoom=1.6,
    pos_top=145,
    pos_left=400
)
map1.set_global_opts(
    visualmap_opts=options.VisualMapOpts(
        is_show=True,
        min_=1000,
        max_=40000,
        is_piecewise=True
    ),
    title_opts=options.TitleOpts(
        title='不同区域某五大品牌的总销量',
        pos_right=250,
        pos_top=30,
        text_align='center'
    ),
    legend_opts=options.LegendOpts(
        is_show=False
    )
)
map1.render('./files/不同区域的销量.html')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值