matplotlib.axes.axes.bar
Matplotlib.axes.axes.bar()函数,使用matplotlib库的Axes模块中的Axes.bar()函数制作条形图。
语法:
Axes.bar(self, x, height, width=0.8, bottom=None, *, align=’center’, data=None, **kwargs)
参数:该方法接受如下参数说明:
- x:该参数是杆的横坐标序列。
- height:该参数是条的高度。
- width:可选参数。是条的宽度(s),默认值为0.8。
- bottom:可选参数。它是杆基的y坐标,默认值为0。
- align:可选参数。它用于使条形图与x坐标对齐。
返回如下内容:
- BarContainer:返回包含所有条和可选错误条的容器。
下面的例子演示了matplotlib.axes.axes.bar()函数在matplotlib.axes中的作用:
示例1
# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
data = ((30, 1000), (10, 28), (100, 30),
(500, 800), (50, 10))
dim = len(data[0])
w = 0.6
dimw = w / dim
fig, ax = plt.subplots()
x = np.arange(len(data))
for i in range(len(data[0])):
y = [d[i] for d in data]
b = ax.bar(x + i * dimw, y,
dimw,
bottom = 0.001)
ax.set_xticks(x + dimw / 2)
ax.set_xticklabels(map(str, x))
ax.set_yscale('log')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_title('matplotlib.axes.Axes.bar Example')
plt.show()
输出:
示例2
# ImpleMinetation of matplotlib function
import numpy as np
import matplotlib.pyplot as plt
labels = ['Month1', 'Month2', 'Month3', 'Month4']
mine = [21, 52, 33, 54]
others = [54, 23, 32, 41]
Mine_std = [2, 3, 4, 1]
Others_std = [3, 5, 2, 3]
width = 0.3
fig, ax = plt.subplots()
ax.bar(labels, mine, width,
yerr = Mine_std,
label ='Mine')
ax.bar(labels, others, width,
yerr = Others_std,
bottom = mine,
label ='Others')
ax.set_ylabel('Articles')
ax.legend()
ax.set_title('matplotlib.axes.Axes.bar Example')
plt.show()
输出: