10【matplotlib常用统计图】02绘制条形图

案例

假设你获取到了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

在什么时候绘制条形图

  • 统计离散的数据,也就是数据之间本身并没有什么关系

如何绘制条形图

代码+结果1.0

# -*- 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()

10【matplotlib常用统计图】02绘制条形图_第1张图片

一步步修改

设置x轴刻度字符

10【matplotlib常用统计图】02绘制条形图_第2张图片

如何设置

10【matplotlib常用统计图】02绘制条形图_第3张图片

# 设置x轴刻度字符
_x=["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:最后的骑士","摔跤吧!爸爸","加勒比海盗5:死无对证","金刚:骷髅岛","极限特工:终极回归","生化危机6:终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:殊死一战","蜘蛛侠:英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"]
plt.xticks(range(len(_x)),_x,rotation=90)  # 注意:rotation设置的是中心旋转,也就是以【x轴刻度】的中心为基点旋转一定的角度

10【matplotlib常用统计图】02绘制条形图_第4张图片
解决方法

1.将图片变大
2.将刻度字符换行显示在x轴上

1.将图片变大

10【matplotlib常用统计图】02绘制条形图_第5张图片
10【matplotlib常用统计图】02绘制条形图_第6张图片

将图片保存到本地看一下是什么情况

10【matplotlib常用统计图】02绘制条形图_第7张图片
10【matplotlib常用统计图】02绘制条形图_第8张图片
10【matplotlib常用统计图】02绘制条形图_第9张图片
10【matplotlib常用统计图】02绘制条形图_第10张图片

2.将刻度字符换行显示在x轴上

10【matplotlib常用统计图】02绘制条形图_第11张图片

# 设置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轴刻度】的中心为基点旋转一定的角度

10【matplotlib常用统计图】02绘制条形图_第12张图片

最终的效果图

10【matplotlib常用统计图】02绘制条形图_第13张图片

问题

10【matplotlib常用统计图】02绘制条形图_第14张图片

我们想要的条形图

10【matplotlib常用统计图】02绘制条形图_第15张图片

如何绘制横着的条形图

代码+结果1.0

# -*- 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()

代码在运行的时候出现了错误

10【matplotlib常用统计图】02绘制条形图_第16张图片
为什么会出现这个错误呢?

10【matplotlib常用统计图】02绘制条形图_第17张图片
我们可以看一下plt.barh的源代码

10【matplotlib常用统计图】02绘制条形图_第18张图片

结合我们写的代码和源代码分析一下错误出现的原因

10【matplotlib常用统计图】02绘制条形图_第19张图片
第一次赋值是通过位置形参给width赋值,第二次赋值是手动给width赋值
所以一共是赋了两次值,出现了错误

修改之后再次运行
10【matplotlib常用统计图】02绘制条形图_第20张图片

# 如何绘制横着的条形图  plt.barh()
plt.barh(range(len(x)),  # 表示每一条在轴上的位置
         y,  # 每一条的长度
         height=0.3,  # 表示每一条的宽度,由于是横着的条形图,是在竖直方向上,所以使用height参数
color=["#98FB98","#8FBC8F","#32CD32","#228B22","#008000","#006400","#7FFF00"],  # 条形图中每一条形的颜色
)

10【matplotlib常用统计图】02绘制条形图_第21张图片

代码+结果2.0

10【matplotlib常用统计图】02绘制条形图_第22张图片

"""设置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)

10【matplotlib常用统计图】02绘制条形图_第23张图片

10【matplotlib常用统计图】02绘制条形图_第24张图片
10【matplotlib常用统计图】02绘制条形图_第25张图片
在这里插入图片描述

"""设置x轴刻度和y轴刻度字符"""
_y=["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:最后的骑士","摔跤吧!爸爸","加勒比海盗5:死无对证","金刚:骷髅岛","极限特工:终极回归","生化危机6:终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:殊死一战","蜘蛛侠:英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"]
plt.yticks(range(len(_y)),_y)

10【matplotlib常用统计图】02绘制条形图_第26张图片

添加网格线

10【matplotlib常用统计图】02绘制条形图_第27张图片

10【matplotlib常用统计图】02绘制条形图_第28张图片

最终效果图

10【matplotlib常用统计图】02绘制条形图_第29张图片

总结

绘制竖着的条形图

  • 如何绘制
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轴数据)  绘制条形图

10【matplotlib常用统计图】02绘制条形图_第30张图片

  • 设置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轴刻度】的中心为基点旋转一定的角度

10【matplotlib常用统计图】02绘制条形图_第31张图片

  • x轴字符换行显示
_x = ["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:\n最后的骑士","摔跤吧!\n爸爸","加勒比海盗5:\n死无对证","金刚:\n骷髅岛","极限特工:\n终极回归","生化危机6:\n终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:\n殊死一战","蜘蛛侠:\n英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"]

10【matplotlib常用统计图】02绘制条形图_第32张图片

绘制横着的条形图

  • 如何绘制
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"],  # 条形图中每一条形的颜色
)

10【matplotlib常用统计图】02绘制条形图_第33张图片

  • 重新设置条的名字
"""设置x轴刻度和y轴刻度字符"""
_y=["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:最后的骑士","摔跤吧!爸爸","加勒比海盗5:死无对证","金刚:骷髅岛","极限特工:终极回归","生化危机6:终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天竺","金刚狼3:殊死一战","蜘蛛侠:英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"]
plt.yticks(range(len(_y)),_y)

10【matplotlib常用统计图】02绘制条形图_第34张图片

所有的代码

代码一(绘制竖着的条形图)

# -*- 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()

你可能感兴趣的:(数据分析2,python,可视化,数据分析)