用pyecharts包画统计图
1.柱状图
1.基本柱状图
from pyecharts.charts import Bar
#1.创建柱状图对象
bar = Bar()
#2.添加数据
#1)添加x轴数据
bar.add_xaxis(['1季度','2季度','3季度','4季度'])
#2)添加x轴上每一个选项对应的数据
bar.add_yaxis('销售额',[280,219,199,300])
bar.add_yaxis('成本',[120,110,200,150])
#3.制图
bar.render('files/柱状图1.html')
2.柱状图的各种配置
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,300],
#柱子的颜色
color='pink',
#柱子的宽度
bar_width=50,
)
bar.add_yaxis(
'成本',
[120,110,200,150],
)
#3.添加配置
#全局配置 数据轴以外的部分 图表外围 图的标题,图例,
bar.set_global_opts(
#设置标题 相关设置
title_opts=options.TitleOpts(
title='某公司2022年全年销售信息', #标题内容
title_link='https://www.baidu.com', #超链接
subtitle = '销售额数据和成本数据', #副标题
subtitle_link='https://www.jd.com',
pos_right='180', #设置标题的位置,left,right,top.bottom
text_align='center', #设制对齐方式 副标题和正标题在哪头对齐 left,center,right
),
#设置图例
legend_opts=options.LegendOpts(
is_show=True, #隐藏图例
pos_right=0,
item_width=20,
item_height=10,
),
#针对x,y轴的配置
#设置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='green', #字体颜色
rotate=45, #字体旋转
),
#添加最大值和最小值的标记点
markpoint_opts=options.MarkPointOpts(
data=[
options.MarkPointItem('最大值','max'),
#options.MarkPointItem('最小值','min')
]
)
)
#数据配置 对某一个数据进行配置
#添加数据的时候,可以单独针对某种数据进行相关配置
#4.渲染图表
bar.render('files/柱状图2.html')
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jXRHDag9-1682598405615)(C:\Users\86152\AppData\Roaming\Typora\typora-user-images\image-20230427202530307.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-73WYbaEs-1682598405617)(C:\Users\86152\AppData\Roaming\Typora\typora-user-images\image-20230427202553421.png)]
2.折线图
from pyecharts.charts import Line
from pyecharts import options
#1.创建折线图对象
line = Line()
#2.添加数据
line.add_xaxis(['1季度','2季度','3季度','4季度'])
line.add_yaxis(
'成本',
[120,110,200,150],
color='lightPink',
#是否平滑 只能在数据配置里面加
is_smooth=True
)
line.add_yaxis('销售额',[280,219,199,300])
#3.配置
line.set_global_opts(
title_opts=options.TitleOpts(
title='某公司2022年全年销售信息', # 标题内容
subtitle='销售额数据和成本数据', # 副标题
pos_right='180', # 设置标题的位置,left,right,top.bottom
text_align='center', # 设制对齐方式 副标题和正标题在哪头对齐 left,center,right
is_show=True
),
# 设置图例
legend_opts=options.LegendOpts(
is_show=True, # 隐藏图例
pos_right=0,
item_width=20,
item_height=10,
),
#针对x,y轴的配置
#设置x轴
xaxis_opts=options.AxisOpts(
name='时间'
),
#设置y轴
yaxis_opts=options.AxisOpts(
name='销售额(万元)/成本(万元)'
)
)
#系列配置 标记线
line.set_series_opts(
markline_opts=options.MarkLineOpts(
data=[options.MarkLineItem('平均值','average')]
),
)
#4.渲染图
line.render('files/折线图.html')
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-HRH4c47a-1682598405618)(C:\Users\86152\AppData\Roaming\Typora\typora-user-images\image-20230427202509564.png)]
3.饼图
from pyecharts.charts import Pie
from pyecharts import options
#1.创建饼图对象
pie = Pie()
#2.添加数据
#[('Apple',512)]
data = {'Apple':512,'HuaWei':600,'XiaoMi':320,
'SanXing':80,'Oppo':120,'vivo':210}
pie.add(
'销量',
list(data.items()),
#[('Apple', 512), ('HuaWei', 600), ('XiaoMi', 320),
# ('SanXing', 80), ('Oppo', 120), ('ViVo', 210)]
#数据配置
#设置圆环 只能有一个括号,不能套叠括号
radius=[60,120],
#外圆以占比多少显示
rosetype='radius',
)
#3.添加配置
#全局配置
pie.set_global_opts(
legend_opts=options.LegendOpts(
is_show=False
),
title_opts=options.TitleOpts(
title='京东购物节各品牌手机销量',
pos_right=180,
text_align='center',
)
)
#系列配置
pie.set_series_opts(
#设置数据的显示格式
label_opts=options.LabelOpts(
#特殊符号 {b} - 数据名称 {d} -一个数据在整个数据中的百分比比值
formatter='{b}:{d}%'
),
)
#渲染
pie.render('files/饼图.html')
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ytAx1jA2-1682598405619)(C:\Users\86152\AppData\Roaming\Typora\typora-user-images\image-20230427202448109.png)]
4.地图
1.中国地图
from pyecharts.charts import Map
from pyecharts import options
import random
#1.创建地图对象
map1 = Map()
#2.添加数据 人口数量单位是 万人
data = [('四川省',8374),('浙江省',6577),
('江西省',4527.98),('广东省',12656.8),('山东省',10162.79),('河南省',9872)]
map1.add(
'人口数量',
data,
maptype='china',
#缩放比例 让地图限制缩放范围
min_scale_limit=0.5,
max_scale_limit=3,
#改变默认大小
zoom=2,
)
#3.配置地图
map1.set_global_opts(
#在地图上显示根据数据的大小涂色
visualmap_opts=options.VisualMapOpts(
is_show=True,
min_=3000,
max_=15000,
is_piecewise=True,
),
title_opts=options.TitleOpts(
title='2022年全国各省份人口数据',
pos_right='170',
text_align='center',
),
legend_opts=options.LegendOpts(
is_show=False
)
)
#渲染地图
map1.render('files/中国地图.html')
2.世界地图
from pyecharts.charts import Map
from pyecharts import options
import random
#1.创建地图对象
map1 = Map()
#2.添加数据
map1.add(
'人均GDP',
[('China',19280),('United States',22000),('Japan',12300),('Sudan',8000),('Australia',10000),
('Russia',9980)],
maptype='world',
#缩放比例 让地图限制缩放范围
min_scale_limit=0.5,
max_scale_limit=3,
#改变默认大小
zoom=1,
)
#全局配置
map1.set_global_opts(
# 在地图上显示根据数据的大小涂色
visualmap_opts=options.VisualMapOpts(
is_show=True,
min_=8000,
max_=25000,
is_piecewise=True,
),
title_opts=options.TitleOpts(
title='2022年全球各国家GDP数据',
pos_right='170',
text_align='center',
),
legend_opts=options.LegendOpts(
is_show=False
)
)
#系列配置 不显示世界地图上各个国家的名字
map1.set_series_opts(
label_opts=options.LabelOpts(
is_show=False
)
)
#渲染地图
map1.render('files/世界地图.html')
3.四川省地图
from pyecharts.charts import Map
from pyecharts import options
import random
#1.创建地图对象
map1 = Map()
#2.添加数据
map1.add(
'平均薪资',
[('成都市',5000),('广元市',3000),('绵阳市',4500),('乐山市',4000),
('甘孜藏族自治州',2000),('南充市',3500),('德阳市',3700),('凉山彝族自治州',2300)],
maptype='四川',
#缩放比例 让地图限制缩放范围
min_scale_limit=0.5,
max_scale_limit=3,
#改变默认大小
zoom=1.25,
)
#3.配置地图
map1.set_global_opts(
#在地图上显示根据数据的大小涂色
visualmap_opts=options.VisualMapOpts(
is_show=True,
min_=1500,
max_=5000,
is_piecewise=True,
),
title_opts=options.TitleOpts(
title='2022年四川省各城市人均薪资',
pos_right='0',
text_align='center',
),
legend_opts=options.LegendOpts(
is_show=False
)
)
#渲染地图
map1.render('files/四川地图.html')
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Z72MWiyy-1682598405620)(C:\Users\86152\AppData\Roaming\Typora\typora-user-images\image-20230427202401888.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-o5SvWYLu-1682598405620)(C:\Users\86152\AppData\Roaming\Typora\typora-user-images\image-20230427202422276.png)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RVVCxCmf-1682598405621)(C:\Users\86152\AppData\Roaming\Typora\typora-user-images\image-20230427202221377.png)]
5.词云图
from pyecharts.charts import WordCloud
from pyecharts import options
#1.创建词云对象
wc = WordCloud()
#2.添加数据
wc.add(
'游戏玩家数量',
[('王者荣耀',9893),('英雄联盟',7821),('原神',2983),
('开心消消乐',12009),('保卫萝卜',892),('CSGO',1290),
('永劫无间',290),('守望先锋',120),('和平精英',15032),
('qq飞车',782),('第五人格',403),('穿越火线',3909),
('诛仙',109),('红警',99)],
# 词云图的形状cardioid(心脏线)、diamond(钻石)、triangle(三角形)、triangle-forward、pentagon(五边形)、star(星形)
shape='cardioid',
#词云图形状的宽高
width='200',
height='300',
)
#渲染
wc.render('files/词云图.html')
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-C4kMRvHA-1682598405622)(C:\Users\86152\AppData\Roaming\Typora\typora-user-images\image-20230427202156102.png)]
6.从表中取出数据作图
import openpyxl
from pyecharts.charts import Bar
from pyecharts import options
#提取销售表里的数据,按照销售渠道:拼多多,抖音,天猫,京东,实体 五类
#提取出销售数量,制作条形图
#1.获得工作簿对象
wb = openpyxl.open('files/2020年销售数据.xlsx')
#2.获得工作表
sheet1 = wb.active
print(sheet1)
sheet2 = wb['data']
print(sheet2)
'''
#获得销售渠道一整列的数据
col = 3
for row in range(1,sheet2.max_row+1):
cell = sheet2.cell(row,col)
print(cell.value)
#获取销售额一整列的数据
col = 7
for row in range(1,sheet2.max_row+1):
cell = sheet2.cell(row,col)
print(cell.value)
'''
qudao_lt = []
xiaolaing_lt =[]
#
for row in range(3,sheet2.max_row):
if sheet2.cell(row,3).value not in qudao_lt:
qudao_lt.append(sheet2.cell(row,3).value)
xiaolaing_lt.append(0)
for index in range(len(qudao_lt)):
if sheet2.cell(row,3).value == qudao_lt[index]:
xiaolaing_lt[index] += sheet2.cell(row,7).value
print(qudao_lt)
print(xiaolaing_lt)
#柱状图
bar = Bar()
bar.add_xaxis(['拼多多','抖音','天猫','京东','实体'])
bar.add_yaxis('销量',
[22398, 12545, 37000, 21522, 13911],
# 柱子的颜色
color='#8ADCDC',
# 柱子的宽度
bar_width=50,
)
bar.set_global_opts(
#设置标题 相关设置
title_opts=options.TitleOpts(
title='2020年销售数据图', #标题内容
#title_link='https://www.baidu.com', #超链接
subtitle = '销售渠道和销量数据', #副标题
#subtitle_link='https://www.jd.com',
pos_right='180', #设置标题的位置,left,right,top.bottom
text_align='center', #设制对齐方式 副标题和正标题在哪头对齐 left,center,right
),
legend_opts=options.LegendOpts(
is_show=True, #隐藏图例
pos_right=0,
item_width=20,
item_height=10,
),
# 针对x,y轴的配置
# 设置x轴
xaxis_opts=options.AxisOpts(
name='渠道'
),
# 设置y轴
yaxis_opts=options.AxisOpts(
name='销售数量'
)
)
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FAFF1sN3-1682598405623)(C:\Users\86152\AppData\Roaming\Typora\typora-user-images\image-20230427202119426.png)]
pos_right='180', #设置标题的位置,left,right,top.bottom
text_align='center', #设制对齐方式 副标题和正标题在哪头对齐 left,center,right
),
legend_opts=options.LegendOpts(
is_show=True, #隐藏图例
pos_right=0,
item_width=20,
item_height=10,
),
# 针对x,y轴的配置
# 设置x轴
xaxis_opts=options.AxisOpts(
name=‘渠道’
),
# 设置y轴
yaxis_opts=options.AxisOpts(
name=‘销售数量’
)
)
[外链图片转存中...(img-FAFF1sN3-1682598405623)]