day10-pyecharts的使用
01柱状图
from pyecharts.charts import Bar
bar = Bar()
bar.add_xaxis(['1季度', '2季度', '3季度', '4季度'])
bar.add_yaxis('销售额', [280, 219, 199, 320])
bar.add_yaxis('成本', [120, 110, 200, 150])
bar.render('files/柱状图1.html')
02柱状图的配置
from pyecharts.charts import Bar
from pyecharts import options
bar = Bar()
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
)
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'
),
legend_opts=options.LegendOpts(
is_show=True,
pos_left='0',
item_width=20,
item_height=10
),
xaxis_opts=options.AxisOpts(
name='时间'
),
yaxis_opts=options.AxisOpts(
name='销售额(万元)/成本(万元)'
)
)
bar.set_series_opts(
label_opts=options.LabelOpts(
is_show=True,
color='#151515',
rotate=15
),
markpoint_opts=options.MarkPointOpts(
data=[
options.MarkPointItem('最大值', 'max'),
options.MarkPointItem('最小值', 'min'),
]
)
)
bar.render('./files/柱状图2.html')
03折线图
from pyecharts.charts import Line
from pyecharts import options
line = Line()
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
)
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
),
xaxis_opts=options.AxisOpts(
name='时间'
),
yaxis_opts=options.AxisOpts(
name='销售额(万元)/成本(万元)'
)
)
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'
)
)
line.render('./files/折线图.html')
04饼图
from pyecharts.charts import Pie
from pyecharts import options
pie = Pie()
data = [('Apple', 512), ('HuaWei', 600), ('XiaoMi', 320), ('SanXing', 80), ('Oppo', 120), ('ViVo', 210)]
pie.add(
'销量',
data,
radius=[50, 150],
rosetype='radius'
)
pie.set_global_opts(
legend_opts=options.LegendOpts(
is_show=False
),
title_opts=options.TitleOpts(
title='2023年各手机销量汇总',
pos_right='250',
text_align='center'
)
)
pie.set_series_opts(
label_opts=options.LabelOpts(
formatter='{b}: {d}%'
)
)
pie.render('./files/饼图.html')
05地图
from pyecharts.charts import Map
from pyecharts import options
map1 = Map()
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
)
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
)
)
map1.render('./files/地图.html')
06世界地图
from pyecharts.charts import Map
from pyecharts import options
map1 = Map()
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
)
map1.set_series_opts(
label_opts=options.LabelOpts(
is_show=False
)
)
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'
)
)
map1.render('./files/世界地图.html')
07省份地图
from pyecharts.charts import Map
from pyecharts import options
map1 = Map()
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
)
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
)
)
map1.render('./files/四川地图.html')
08词云
from pyecharts.charts import WordCloud
from pyecharts import options
wc = WordCloud()
wc.add(
'游戏玩家数量',
[('王者荣耀', 9894), ('英雄联盟', 7821), ('原神', 2983), ('开心消消乐', 12099), ('保卫萝卜', 892), ('CSGO', 1290), ('永劫无间', 290), ('守望先锋', 120), ('和平精英', 15032), ('qq飞车', 782), ('穿越火线', 3909), ('诛仙', 109), ('红警', 345), ('第五人格', 642)],
shape='cardioid',
width='200',
height='200'
)
wc.render('./files/词云.html')
09作业: 饼图
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
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作业: 柱状图
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:
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)
bar = Bar()
bar.add_xaxis(x_data)
bar.add_yaxis('平均价格', y_data)
bar.render('./files/品牌平均价格.html')
11作业: 地图
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)
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')