python调用matplotlib模块绘制柱状图
作者:君的名字 时间:2024-01-02 04:35:16
我们可以调用matplotlib 绘制我们的柱状图,柱状图可以是水平的也可以是竖直的。
在这里我先记录下竖直的柱状图怎么绘制
在这里一般用到的函数就是bar
# bar(left, height, width=0.8, bottom=None, hold=None, **kwargs)
# 绘制柱形图
# left:柱形图的x坐标
# height柱形图的高度,以0.0为基准
# width:柱形图的宽度,默认0.8
# facecolor:颜色
# edgecolor:边框颜色n
# bottom:表示底部从y轴的哪个刻度开始画
# yerr:应该是对应的数据的误差范围,加上这个参数,柱状图头部会有一个蓝色的范围标识,标出允许的误差范围,在水平柱状图中这个参数为xerr
在这里我一般特别喜欢将柱状图的边缘颜色设置为白色,因为这样画出来比较好看
eg.
plt.bar(x,+y1,width=0.8,facecolor="#9999ff",edgecolor="white",yerr=error)
下面来说一下画bar chart 的步骤
首先我们需要引入两个模块:
import numpy as np
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.pyplot as plt
n = 12
# 生成一个1-12的列表,不包括12,[ 0 1 2 3 4 5 6 7 8 9 10 11]
x = np.arange(n)
# np.random.uniform(0.5,1.0,n),生成n个0.5-1.0之间的随机数
y1 = 3 * np.random.uniform(0.5,1.0,n)
y2 = 3 * np.random.uniform(0.5,1.0,n)
# 在这里我们是使用一个随机生成函数生成了两组y的值,生成的这个随机数是服从均匀分布的
# 如果我们的数值比较少我们可以直接给y赋值
# y = [5,7,3]
# 生成一个包含有n个值,均为0.2的list,表示允许的误差范围[-0.2,0.2]
error = [0.2,] * n
# bar(left, height, width=0.8, bottom=None, hold=None, **kwargs)
# 绘制柱形图
# left:柱形图的x坐标
# height柱形图的高度,以0.0为基准
# width:柱形图的宽度,默认0.8
# facecolor:颜色
# edgecolor:边框颜色n
# bottom:表示底部从y轴的哪个刻度开始画
# yerr:应该是对应的数据的误差范围,加上这个参数,柱状图头部会有一个蓝色的范围标识,标出允许的误差范围,在水平柱状图中这个参数为xerr
plt.bar(x,+y1,width=0.8,facecolor="#9999ff",edgecolor="white",yerr=error)
plt.bar(x,-y2,facecolor="#ff9999",edgecolor="white")
# 绘制文字,显示柱状图形的值
for x,y1,y2 in zip(x,y1,y2):
plt.text(x+0.4,y1+0.05,'%.2f' % y1,ha='center',va='bottom')
plt.text(x+0.4,-(y2+0.05),'%.2f' % y2,ha='center',va='top')
plt.ylim(-3.5,3.5)
plt.show()
如果我们需要的是给我们柱状图绘制一些标记,比如横坐标和纵坐标的值,这个时候我们可以像下面这样做。这个例子我用的是官网上的代码。
# Credit: Josh Hemann
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
from collections import namedtuple
n_groups = 5
means_men = (20, 35, 30, 35, 27)
std_men = (2, 3, 4, 1, 2)
means_women = (25, 32, 34, 20, 25)
std_women = (3, 5, 2, 3, 3)
fig, ax = plt.subplots()
index = np.arange(n_groups)
bar_width = 0.35
opacity = 0.4
error_config = {'ecolor': '0.3'}
rects1 = ax.bar(index, means_men, bar_width,
alpha=opacity, color='b',
yerr=std_men, error_kw=error_config,
label='Men')
rects2 = ax.bar(index + bar_width, means_women, bar_width,
alpha=opacity, color='r',
yerr=std_women, error_kw=error_config,
label='Women')
ax.set_xlabel('Group')
ax.set_ylabel('Scores')
ax.set_title('Scores by group and gender')
ax.set_xticks(index + bar_width / 2)
ax.set_xticklabels(('A', 'B', 'C', 'D', 'E'))
ax.legend()
fig.tight_layout()
plt.show()
在这里我们设置的X的坐标以及上边的标签,我们主要的代码是:
ax.bar(index, means_men, bar_width,
alpha=opacity, color='b',
yerr=std_men, error_kw=error_config,
label='Men')
ax.set_xticks(index + bar_width / 2) # 设置坐标的其实坐标
ax.set_xticklabels(('A', 'B', 'C', 'D', 'E'))
这里的bar函数的参数和我们开始介绍的是一样的,只是我们在设置坐标的时候,一般是我们的条形图的中间所以我们要把宽度除以2
来源:https://blog.csdn.net/chichoxian/article/details/79008423
标签:python,matplotlib,柱状图
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
用Python实现协同过滤的教程
2023-08-30 12:28:55
![](https://img.aspxhome.com/file/2023/5/112795_0s.png)
1 行 Python 代码快速实现 FTP 服务器
2022-02-19 18:17:41
![](https://img.aspxhome.com/file/2023/0/104870_0s.png)
纯js封装的ajax功能函数与用法示例
2024-05-11 09:09:20
Oracle相关组件版本信息的介绍
2023-07-14 09:19:53
![](https://img.aspxhome.com/file/2023/4/64454_0s.png)
ASP访问数量统计代码
2011-04-08 10:59:00
docker上安装使用mysql镜像
2024-01-24 21:04:15
js 目录列举函数
2024-06-05 09:12:50
jquery实现点击页面计算点击次数
2024-04-16 10:33:17
仿google的asp分页代码
2009-03-08 18:27:00
解析JavaScript中 querySelector 与 getElementById 方法的区别
2024-04-19 09:57:32
![](https://img.aspxhome.com/file/2023/4/135994_0s.png)
Mysql数据库手动及定时备份步骤
2024-01-27 10:43:15
![](https://img.aspxhome.com/file/2023/4/69854_0s.png)
Python中反射和描述器总结
2022-06-18 09:38:22
详解Go语言中new和make关键字的区别
2024-05-21 10:19:20
Spark SQL常见4种数据源详解
2024-01-18 09:07:21
Python cookbook(数据结构与算法)对切片命名清除索引的方法
2023-10-31 02:27:35
python实现FTP文件传输的方法(服务器端和客户端)
2021-03-29 23:36:08
![](https://img.aspxhome.com/file/2023/9/65979_0s.png)
pytorch分类模型绘制混淆矩阵以及可视化详解
2023-01-17 17:35:43
![](https://img.aspxhome.com/file/2023/6/106976_0s.png)
对python的bytes类型数据split分割切片方法
2021-01-07 03:09:14
在Python中使用第三方模块的教程
2022-09-06 12:20:27
使用OpenCV实现仿射变换—平移功能
2022-11-01 01:45:06
![](https://img.aspxhome.com/file/2023/2/103172_0s.jpg)