python绘制横向水平柱状条形图
作者:zhangphil 时间:2022-01-10 01:01:49
python绘制横向水平柱状条形图Bar,供大家参考,具体内容如下
import matplotlib
import random
import matplotlib.pyplot as plt
# 中文乱码和坐标轴负号处理。
matplotlib.rc('font', family='SimHei', weight='bold')
plt.rcParams['axes.unicode_minus'] = False
#城市数据。
city_name = ['北京', '上海', '广州', '深圳', '成都']
#数组反转。
city_name.reverse()
#装载随机数据。
data = []
for i in range(len(city_name)):
data.append(random.randint(100, 150))
#绘图。
fig, ax = plt.subplots()
b = ax.barh(range(len(city_name)), data, color='#6699CC')
#为横向水平的柱图右侧添加数据标签。
for rect in b:
w = rect.get_width()
ax.text(w, rect.get_y()+rect.get_height()/2, '%d' %
int(w), ha='left', va='center')
#设置Y轴纵坐标上的刻度线标签。
ax.set_yticks(range(len(city_name)))
ax.set_yticklabels(city_name)
#不要X横坐标上的label标签。
plt.xticks(())
plt.title('水平横向的柱状图', loc='center', fontsize='25',
fontweight='bold', color='red')
plt.show()
运行结果:
可以简单的绘制一个水平横向的柱状图:
import matplotlib.pyplot as plt
import random
import matplotlib
matplotlib.rc('font', family='SimHei', weight='bold')
city_name = ['北京', '上海', '广州', '深圳', '成都']
city_name.reverse()
data = []
for i in range(len(city_name)):
data.append(random.randint(100, 200))
colors = ['red', 'yellow', 'blue', 'green', 'gray']
colors.reverse()
plt.barh(range(len(data)), data, tick_label=city_name, color=colors)
#不要X横坐标标签。
#plt.xticks(())
plt.show()
输出结果如图:
来源:https://blog.csdn.net/zhangphil/article/details/88390032
标签:python,条形图
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
基于wxpython开发的简单gui计算器实例
2023-09-09 04:42:25
python求列表交集的方法汇总
2023-03-01 10:32:40
Python3+PyCharm+Django+Django REST framework配置与简单开发教程
2023-06-15 09:26:28
![](https://img.aspxhome.com/file/2023/3/76003_0s.png)
Python Django框架设计模式详解
2021-01-16 12:52:40
![](https://img.aspxhome.com/file/2023/4/72864_0s.jpg)
《悟透JavaScript》感谢语
2008-11-12 12:59:00
![](https://img.aspxhome.com/file/UploadPic/200811/12/covera-92s.jpg)
简单介绍一下pyinstaller打包以及安全性的实现
2021-07-23 15:34:56
![](https://img.aspxhome.com/file/2023/8/78718_0s.png)
Flask框架请求钩子与request请求对象用法实例分析
2021-11-23 20:14:48
![](https://img.aspxhome.com/file/2023/8/95148_0s.png)
python中的turtle库函数简单使用教程
2022-08-06 23:22:18
![](https://img.aspxhome.com/file/2023/4/92054_0s.jpg)
Scrapy-Redis结合POST请求获取数据的方法示例
2023-08-25 06:31:56
windows 7安装ORACLE 10g客户端的方法分享
2012-07-11 15:36:18
Mysql 数据库双机热备的配置方法
2010-06-09 19:13:00
利用Opencv实现图片的油画特效实例
2022-01-26 14:31:59
![](https://img.aspxhome.com/file/2023/0/81530_0s.jpg)
瞎扯之Web导航
2009-03-18 19:32:00
![](https://img.aspxhome.com/file/UploadPic/20093/18/01-85s.jpg)
Python中函数的参数定义和可变参数用法实例分析
2023-10-04 00:26:30
Python中方法的缺省参数问题解读
2022-10-07 17:00:45
![](https://img.aspxhome.com/file/2023/9/92059_0s.png)
python 随机森林算法及其优化详解
2023-03-30 20:20:45
python中判断文件结束符的具体方法
2021-09-28 13:31:53
教你如何在SQL Server计算机列和平均值
2009-01-20 15:10:00
asp中常用的文件处理函数
2009-01-08 18:09:00
python多进程执行方法apply_async使用说明
2023-01-31 11:56:10