coder-谢公子:电影数据的获取zhuanlan.zhihu.com
这节我们就用上次我们获取到的数据做一个可视化——柱状图
直方图调用bar()传入x、y轴的值
不太了解这个方法要怎么传入参数的时候我们可以按住Ctrl键鼠标移到bar()就可以查看这个方法的源码
![da474e45a68937aed506c2f933618e14.png](https://i-blog.csdnimg.cn/blog_migrate/14a780834826085bbd1a87ff843133fa.png)
可以看看前面两个参数是没有值的,这就需要我们传入。看单词就很容易理解出是什么意思:
x:是x轴的值;height就是对应的高度
目录:
1、准备工作
2、基本演示
3、表明数值
4、改变颜色
5、加边缘色
6、横向的柱子
1、准备工作
python3.7.3
首先要确保安装好Matplotlib库用pip下载,打开cmd输入pip install matplotlib即可完成下载
2、基本演示
from matplotlib import pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
x = ['战狼2', '哪吒之魔童降世', '流浪地球', '复仇者联盟4', '红海行动', '美人鱼', '唐人街探案2', '我和我的祖国',
'我不是药神', '中国机长', '速度与激情8', '西虹市首富', '速度与激情7', '捉妖记', '复仇者联盟3', '捉妖记2',
'羞羞的铁拳', '疯狂的外星人', '海王', '变形金刚4']
y = [56.39, 49.34, 46.18, 42.05, 36.22, 33.9, 33.71, 31.46, 30.75, 28.84,
26.49, 25.27, 24.26, 24.21, 23.7, 22.19, 21.9, 21.83, 19.97, 19.79]
plt.bar(x, y)
plt.title('票房数据')
plt.xlabel('名称')
plt.ylabel('票房')
plt.xticks(rotation=45)
plt.show()
运行结果:
![bfd84edbd36addc6cf18bb9bb0b696ce.png](https://i-blog.csdnimg.cn/blog_migrate/900ec470d3d2439ed0f2d167159986c5.jpeg)
3、表明数值
这图看起来不大好看,不容易看出每个柱子的数值,我们就要把每柱子的数值加上去才方便看,这就要调用方法text()
我们还是先看看这个方法怎么传参
![c347cea4994b6f34437bec33c1992374.png](https://i-blog.csdnimg.cn/blog_migrate/029243776dcec94de3ae0984f7f7ca64.png)
x和y可以组成一个坐标(x,y),s是要写入的文本(写在(x,y)上)
from matplotlib import pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
x = ['战狼2', '哪吒之魔童降世', '流浪地球', '复仇者联盟4', '红海行动', '美人鱼', '唐人街探案2', '我和我的祖国',
'我不是药神', '中国机长', '速度与激情8', '西虹市首富', '速度与激情7', '捉妖记', '复仇者联盟3', '捉妖记2',
'羞羞的铁拳', '疯狂的外星人', '海王', '变形金刚4']
y = [56.39, 49.34, 46.18, 42.05, 36.22, 33.9, 33.71, 31.46, 30.75, 28.84,
26.49, 25.27, 24.26, 24.21, 23.7, 22.19, 21.9, 21.83, 19.97, 19.79]
plt.bar(x, y)
plt.title('排名前二十的电影票房')
plt.xlabel('名字')
plt.ylabel('票房')
for i in range(len(x)):
plt.text(x[i], y[i], y[i], ha='center')
plt.xticks(rotation=45)
plt.show()
运行结果:
![52d5b0a32619b18daebc02eb6c637d09.png](https://i-blog.csdnimg.cn/blog_migrate/4c28a27b7502b649b82d4106267096bf.jpeg)
4、改变颜色
这全是一样颜色的就是想改怎么办
from matplotlib import pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
x = ['战狼2', '哪吒之魔童降世', '流浪地球', '复仇者联盟4', '红海行动', '美人鱼', '唐人街探案2', '我和我的祖国',
'我不是药神', '中国机长', '速度与激情8', '西虹市首富', '速度与激情7', '捉妖记', '复仇者联盟3', '捉妖记2',
'羞羞的铁拳', '疯狂的外星人', '海王', '变形金刚4']
y = [56.39, 49.34, 46.18, 42.05, 36.22, 33.9, 33.71, 31.46, 30.75, 28.84,
26.49, 25.27, 24.26, 24.21, 23.7, 22.19, 21.9, 21.83, 19.97, 19.79]
color = ['r', 'g', 'black']
plt.bar(x, y, color=color, edgecolor='black')
plt.title('排名前二十的电影票房')
plt.xlabel('名字')
plt.ylabel('票房')
for i in range(len(x)):
plt.text(x[i], y[i], y[i], ha='center')
plt.xticks(rotation=45)
plt.show()
运行结果:
![39d8a8954b96c4a86a8cd8bb9dacf386.png](https://i-blog.csdnimg.cn/blog_migrate/957c68ddf57c1a51a68e6a4edffff262.jpeg)
不需要每个柱子都设定一个颜色,直接用个颜色列表就好了,会循环设置这些颜色
5、加边缘色
可以在bar中加入edgecolor 参数给柱子设置边缘色
from matplotlib import pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
x = ['战狼2', '哪吒之魔童降世', '流浪地球', '复仇者联盟4', '红海行动', '美人鱼', '唐人街探案2', '我和我的祖国',
'我不是药神', '中国机长', '速度与激情8', '西虹市首富', '速度与激情7', '捉妖记', '复仇者联盟3', '捉妖记2',
'羞羞的铁拳', '疯狂的外星人', '海王', '变形金刚4']
y = [56.39, 49.34, 46.18, 42.05, 36.22, 33.9, 33.71, 31.46, 30.75, 28.84,
26.49, 25.27, 24.26, 24.21, 23.7, 22.19, 21.9, 21.83, 19.97, 19.79]
plt.bar(x, y, color='black', edgecolor='r')
plt.title('排名前二十的电影票房')
plt.xlabel('名字')
plt.ylabel('票房')
for i in range(len(x)):
plt.text(x[i], y[i], y[i], ha='center')
plt.xticks(rotation=45)
plt.show()
运行结果:
![c0cdcddb3a62ac6f43313f58ac9f3db8.png](https://i-blog.csdnimg.cn/blog_migrate/c1ef3929afd8c363d477428aec8e45e4.jpeg)
6、横向的柱子
调用barh()方法
![9ddb29f4221dbc27da51d2bf2d60ba1d.png](https://i-blog.csdnimg.cn/blog_migrate/a69b6ad14d5721c3885e6c54e88ccddf.png)
这个的第一个参数是y对应的值其实的是x轴的值
运行结果:
![914527581693d8775dc1c59604ef0177.png](https://i-blog.csdnimg.cn/blog_migrate/f599deab85c05a894a7cbf71d0f349c0.jpeg)
柱子的值都和柱子重叠了一部分,只要微调一下就好了
from matplotlib import pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei']
x = ['战狼2', '哪吒之魔童降世', '流浪地球', '复仇者联盟4:终局之战', '红海行动', '美人鱼', '唐人街探案2', '我和我的祖国',
'我不是药神', '中国机长', '速度与激情8', '西虹市首富', '速度与激情7', '捉妖记', '复仇者联盟3:无限战争', '捉妖记2',
'羞羞的铁拳', '疯狂的外星人', '海王', '变形金刚4:绝迹重生']
y = [56.39, 49.34, 46.18, 42.05, 36.22, 33.9, 33.71, 31.46, 30.75, 28.84,
26.49, 25.27, 24.26, 24.21, 23.7, 22.19, 21.9, 21.83, 19.97, 19.79]
plt.barh(x, y)
for i in range(len(x)):
plt.text(y[i]+1, x[i], y[i], ha='center')
plt.title('票房数据')
plt.xlabel('名称')
plt.ylabel('票房')
plt.show()
![115f9edf84767e6414b0d4f729693cd1.png](https://i-blog.csdnimg.cn/blog_migrate/30b85935eb4deb91665989ad2ee19d0c.jpeg)