假设你获取到了2017年内地电影票房前20的电影(列表a)和电影票房数据(列表b),那么如何更加直观的展示该数据?
a = ["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:最后的骑士","摔跤吧!爸爸","加勒比海盗5:死无对证","金刚:骷髅岛","极限特工:终极回归","生化危机6:终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:殊死一战","蜘蛛侠:英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"]
b=[56.01,26.94,17.53,16.49,15.45,12.96,11.8,11.61,11.28,11.12,10.49,10.3,8.75,7.55,7.32,6.99,6.88,6.86,6.58,6.23](单位:亿)
数据来源:http://58921.com/alltime/2017
# -*- coding: utf-8 -*-
'''
@Time : 2020/11/28 23:19
@Author : yuhui
@Email : [email protected]
@FileName: matplotlib_7.py
@Software: PyCharm
'''
"""10【matplotlib常用统计图】02绘制条形图"""
"""假设你获取到了2017年内地电影票房前20的电影(列表a)和电影票房数据(列表b),那么如何更加直观的展示该数据?
数据来源:http://58921.com/alltime/2017"""
from matplotlib import pyplot as plt
# 解决中文乱码
plt.rcParams["font.sans-serif"]=["KaiTi"]
plt.rcParams["font.family"]="sans-serif"
# 解决符号无法显示的问题
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像时负号'-'显示为方块的问题
"""设置图形大小和图片品质"""
fig=plt.figure(
figsize=(16,9),
dpi=100,
)
# x轴数据
x = ["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:最后的骑士","摔跤吧!爸爸","加勒比海盗5:死无对证","金刚:骷髅岛","极限特工:终极回归","生化危机6:终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:殊死一战","蜘蛛侠:英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"]
# y轴数据
y=[56.01,26.94,17.53,16.49,15.45,12.96,11.8,11.61,11.28,11.12,10.49,10.3,8.75,7.55,7.32,6.99,6.88,6.86,6.58,6.23]
# 绘制条形图
plt.bar(range(len(x)),y, # 注意:x轴数据应该是数字的列表
# 然后我们再绘制x轴刻度 将x轴字符数据与x轴数字列表做一个一一对应
width=0.3, # 条形图的宽度
color=["#98FB98","#8FBC8F","#32CD32","#228B22","#008000","#006400","#7FFF00"], # 条形图中每一条形的颜色
) # plt.bar(x轴数据,y轴数据) 绘制条形图
plt.show()
# 设置x轴刻度字符
_x=["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:最后的骑士","摔跤吧!爸爸","加勒比海盗5:死无对证","金刚:骷髅岛","极限特工:终极回归","生化危机6:终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:殊死一战","蜘蛛侠:英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"]
plt.xticks(range(len(_x)),_x,rotation=90) # 注意:rotation设置的是中心旋转,也就是以【x轴刻度】的中心为基点旋转一定的角度
1.将图片变大
2.将刻度字符换行显示在x轴上
将图片保存到本地看一下是什么情况
# 设置x轴刻度字符
# 当名字比较长的时候,在名字中间插入一个\n换行符,使名字在x轴上显示的时候会换行显示,不至于过长超出了图片的长度
_x = ["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:\n最后的骑士","摔跤吧!\n爸爸","加勒比海盗5:\n死无对证","金刚:\n骷髅岛","极限特工:\n终极回归","生化危机6:\n终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:\n殊死一战","蜘蛛侠:\n英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"]
plt.xticks(range(len(_x)),_x,rotation=90) # 注意:rotation设置的是中心旋转,也就是以【x轴刻度】的中心为基点旋转一定的角度
# -*- coding: utf-8 -*-
'''
@Time : 2020/11/29 9:48
@Author : yuhui
@Email : [email protected]
@FileName: matplotlib_8.py
@Software: PyCharm
'''
"""10【matplotlib常用统计图】02绘制条形图"""
from matplotlib import pyplot as plt
# 解决中文乱码
plt.rcParams["font.sans-serif"]=["KaiTi"]
plt.rcParams["font.family"]="sans-serif"
# 解决符号无法显示的问题
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像时负号'-'显示为方块的问题
"""设置图形大小和图片品质"""
fig=plt.figure(
figsize=(16,9),
dpi=100,
)
# x轴数据
x = ["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:最后的骑士","摔跤吧!爸爸","加勒比海盗5:死无对证","金刚:骷髅岛","极限特工:终极回归","生化危机6:终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:殊死一战","蜘蛛侠:英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"]
# y轴数据
y=[56.01,26.94,17.53,16.49,15.45,12.96,11.8,11.61,11.28,11.12,10.49,10.3,8.75,7.55,7.32,6.99,6.88,6.86,6.58,6.23]
# 如何绘制横着的条形图 plt.barh()
plt.barh(range(len(x)),y,width=0.3,
color=["#98FB98","#8FBC8F","#32CD32","#228B22","#008000","#006400","#7FFF00"], # 条形图中每一条形的颜色
)
# 设置x轴刻度字符
# 当名字比较长的时候,在名字中间插入一个\n换行符,使名字在x轴上显示的时候会换行显示,不至于过长超出了图片的长度
_x = ["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:\n最后的骑士","摔跤吧!\n爸爸","加勒比海盗5:\n死无对证","金刚:\n骷髅岛","极限特工:\n终极回归","生化危机6:\n终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:\n殊死一战","蜘蛛侠:\n英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"]
plt.xticks(range(len(_x)),_x,rotation=90) # 注意:rotation设置的是中心旋转,也就是以【x轴刻度】的中心为基点旋转一定的角度
plt.show()
代码在运行的时候出现了错误
结合我们写的代码和源代码分析一下错误出现的原因
第一次赋值是通过位置形参给width赋值,第二次赋值是手动给width赋值
所以一共是赋了两次值,出现了错误
# 如何绘制横着的条形图 plt.barh()
plt.barh(range(len(x)), # 表示每一条在轴上的位置
y, # 每一条的长度
height=0.3, # 表示每一条的宽度,由于是横着的条形图,是在竖直方向上,所以使用height参数
color=["#98FB98","#8FBC8F","#32CD32","#228B22","#008000","#006400","#7FFF00"], # 条形图中每一条形的颜色
)
"""设置x轴刻度和y轴刻度字符"""
_y=["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:\n最后的骑士","摔跤吧!\n爸爸","加勒比海盗5:\n死无对证","金刚:\n骷髅岛","极限特工:\n终极回归","生化危机6:\n终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:\n殊死一战","蜘蛛侠:\n英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"]
plt.yticks(range(len(_y)),_y,rotation=90)
"""设置x轴刻度和y轴刻度字符"""
_y=["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:最后的骑士","摔跤吧!爸爸","加勒比海盗5:死无对证","金刚:骷髅岛","极限特工:终极回归","生化危机6:终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:殊死一战","蜘蛛侠:英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"]
plt.yticks(range(len(_y)),_y)
plt.bar(x轴数据,y轴数据) # 绘制条形图
# 绘制条形图
plt.bar(range(len(x)),y, # 注意:x轴数据应该是数字的列表
# 然后我们再绘制x轴刻度 将x轴字符数据与x轴数字列表做一个一一对应
width=0.3, # 条形图的宽度
color=["#98FB98","#8FBC8F","#32CD32","#228B22","#008000","#006400","#7FFF00"], # 条形图中每一条形的颜色
) # plt.bar(x轴数据,y轴数据) 绘制条形图
# 设置x轴刻度字符
# 当名字比较长的时候,在名字中间插入一个\n换行符,使名字在x轴上显示的时候会换行显示,不至于过长超出了图片的长度
_x = ["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:\n最后的骑士","摔跤吧!\n爸爸","加勒比海盗5:\n死无对证","金刚:\n骷髅岛","极限特工:\n终极回归","生化危机6:\n终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:\n殊死一战","蜘蛛侠:\n英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"]
plt.xticks(range(len(_x)),_x,rotation=90) # 注意:rotation设置的是中心旋转,也就是以【x轴刻度】的中心为基点旋转一定的角度
_x = ["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:\n最后的骑士","摔跤吧!\n爸爸","加勒比海盗5:\n死无对证","金刚:\n骷髅岛","极限特工:\n终极回归","生化危机6:\n终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:\n殊死一战","蜘蛛侠:\n英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"]
plt.barh(条在y轴上的位置,条的长度)
# 如何绘制横着的条形图 plt.barh(条在y轴上的位置,条的长度)
plt.barh(range(len(x)), # 表示每一条在轴上的位置
y, # 每一条的长度
height=0.3, # 表示每一条的宽度,由于是横着的条形图,是在竖直方向上,所以使用height参数
color=["#98FB98","#8FBC8F","#32CD32","#228B22","#008000","#006400","#7FFF00"], # 条形图中每一条形的颜色
)
# 如何绘制横着的条形图 plt.barh(条在y轴上的位置,条的长度)
plt.barh(range(len(x)), # 表示每一条在轴上的位置
y, # 每一条的长度
height=0.3, # 表示每一条的宽度,由于是横着的条形图,是在竖直方向上,所以使用height参数
color=["#98FB98","#8FBC8F","#32CD32","#228B22","#008000","#006400","#7FFF00"], # 条形图中每一条形的颜色
)
"""设置x轴刻度和y轴刻度字符"""
_y=["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:最后的骑士","摔跤吧!爸爸","加勒比海盗5:死无对证","金刚:骷髅岛","极限特工:终极回归","生化危机6:终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:殊死一战","蜘蛛侠:英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"]
plt.yticks(range(len(_y)),_y)
# -*- coding: utf-8 -*-
'''
@Time : 2020/11/28 23:19
@Author : yuhui
@Email : [email protected]
@FileName: matplotlib_7.py
@Software: PyCharm
'''
"""10【matplotlib常用统计图】02绘制条形图"""
"""假设你获取到了2017年内地电影票房前20的电影(列表a)和电影票房数据(列表b),那么如何更加直观的展示该数据?
数据来源:http://58921.com/alltime/2017"""
from matplotlib import pyplot as plt
# 解决中文乱码
plt.rcParams["font.sans-serif"]=["KaiTi"]
plt.rcParams["font.family"]="sans-serif"
# 解决符号无法显示的问题
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像时负号'-'显示为方块的问题
"""设置图形大小和图片品质"""
fig=plt.figure(
figsize=(16,9),
dpi=100,
)
# x轴数据
x = ["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:最后的骑士","摔跤吧!爸爸","加勒比海盗5:死无对证","金刚:骷髅岛","极限特工:终极回归","生化危机6:终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:殊死一战","蜘蛛侠:英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"]
# y轴数据
y=[56.01,26.94,17.53,16.49,15.45,12.96,11.8,11.61,11.28,11.12,10.49,10.3,8.75,7.55,7.32,6.99,6.88,6.86,6.58,6.23]
# 绘制条形图
plt.bar(range(len(x)),y, # 注意:x轴数据应该是数字的列表
# 然后我们再绘制x轴刻度 将x轴字符数据与x轴数字列表做一个一一对应
width=0.3, # 条形图的宽度
color=["#98FB98","#8FBC8F","#32CD32","#228B22","#008000","#006400","#7FFF00"], # 条形图中每一条形的颜色
) # plt.bar(x轴数据,y轴数据) 绘制条形图
# 设置x轴刻度字符
# 当名字比较长的时候,在名字中间插入一个\n换行符,使名字在x轴上显示的时候会换行显示,不至于过长超出了图片的长度
_x = ["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:\n最后的骑士","摔跤吧!\n爸爸","加勒比海盗5:\n死无对证","金刚:\n骷髅岛","极限特工:\n终极回归","生化危机6:\n终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:\n殊死一战","蜘蛛侠:\n英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"]
plt.xticks(range(len(_x)),_x,rotation=90) # 注意:rotation设置的是中心旋转,也就是以【x轴刻度】的中心为基点旋转一定的角度
# # 设置x轴刻度字符
# _x=["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:最后的骑士","摔跤吧!爸爸","加勒比海盗5:死无对证","金刚:骷髅岛","极限特工:终极回归","生化危机6:终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:殊死一战","蜘蛛侠:英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"]
# plt.xticks(range(len(_x)),_x,rotation=90) # 注意:rotation设置的是中心旋转,也就是以【x轴刻度】的中心为基点旋转一定的角度
# plt.savefig("../data/matplotlib_7.svg")
plt.show()
# -*- coding: utf-8 -*-
'''
@Time : 2020/11/29 9:48
@Author : yuhui
@Email : [email protected]
@FileName: matplotlib_8.py
@Software: PyCharm
'''
"""10【matplotlib常用统计图】02绘制条形图"""
from matplotlib import pyplot as plt
# 解决中文乱码
plt.rcParams["font.sans-serif"]=["KaiTi"]
plt.rcParams["font.family"]="sans-serif"
# 解决符号无法显示的问题
plt.rcParams['axes.unicode_minus'] = False # 解决保存图像时负号'-'显示为方块的问题
"""设置图形大小和图片品质"""
fig=plt.figure(
figsize=(16,9),
dpi=100,
)
# x轴数据
x = ["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:最后的骑士","摔跤吧!爸爸","加勒比海盗5:死无对证","金刚:骷髅岛","极限特工:终极回归","生化危机6:终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:殊死一战","蜘蛛侠:英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"]
# y轴数据
y=[56.01,26.94,17.53,16.49,15.45,12.96,11.8,11.61,11.28,11.12,10.49,10.3,8.75,7.55,7.32,6.99,6.88,6.86,6.58,6.23]
# 如何绘制横着的条形图 plt.barh()
plt.barh(range(len(x)), # 表示每一条在轴上的位置
y, # 每一条的长度
height=0.3, # 表示每一条的宽度,由于是横着的条形图,是在竖直方向上,所以使用height参数
color=["#98FB98","#8FBC8F","#32CD32","#228B22","#008000","#006400","#7FFF00"], # 条形图中每一条形的颜色
)
# # 设置x轴刻度字符
# # 当名字比较长的时候,在名字中间插入一个\n换行符,使名字在x轴上显示的时候会换行显示,不至于过长超出了图片的长度
# _x = ["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:\n最后的骑士","摔跤吧!\n爸爸","加勒比海盗5:\n死无对证","金刚:\n骷髅岛","极限特工:\n终极回归","生化危机6:\n终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:\n殊死一战","蜘蛛侠:\n英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"]
# plt.xticks(range(len(_x)),_x,rotation=90) # 注意:rotation设置的是中心旋转,也就是以【x轴刻度】的中心为基点旋转一定的角度
"""设置x轴刻度和y轴刻度字符"""
_y=["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:最后的骑士","摔跤吧!爸爸","加勒比海盗5:死无对证","金刚:骷髅岛","极限特工:终极回归","生化危机6:终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:殊死一战","蜘蛛侠:英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"]
plt.yticks(range(len(_y)),_y)
# 添加网格线
plt.grid(
linestyle=":", # 线型
color="#6495ED", # 颜色 CornflowerBlue 矢车菊的蓝色 #6495ED 100,149,237
alpha=0.4, # 透明度
)
plt.show()