Python中的 Matplotlib.axes.Axes.bar()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Axes 类包含大部分图形元素:Axis、Tick、Line2D、Text、Polygon 等,并设置坐标系。 Axes 的实例通过回调属性支持回调。
Matplotlib.axes.Axes.bar()函数
matplotlib 库的 axes 模块中的Axes.bar()函数用于制作条形图。
Syntax: Axes.bar(self, x, height, width=0.8, bottom=None, *, align=’center’, data=None, **kwargs)
Parameters: This method accept the following parameters that are described below:
- x: This parameter is the sequence of horizontal coordinates of the bar.
- height: This parameter is the height(s) of the bars.
- width: This parameter is an optional parameter. And it is the width(s) of the bars with default value 0.8.
- bottom: This parameter is also an optional parameter. And it is the y coordinate(s) of the bars bases with default value 0.
- alighn: This parameter is also an optional parameter. And it is used for alignment of the bars to the x coordinates.
Returns: This returns the following:
- BarContainer:This returns the container with all the bars and optionally errorbars.
下面的示例说明了 matplotlib.axes 中的 matplotlib.axes.Axes.bar()函数:
示例 #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()
输出: