Python中的 matplotlib.axes.Axes.barh()
Matplotlib是Python中的一个库,它是 NumPy 库的数值数学扩展。 Axes 类包含大部分图形元素:Axis、Tick、Line2D、Text、Polygon 等,并设置坐标系。 Axes 的实例通过回调属性支持回调。
matplotlib.axes.Axes.barh()函数
matplotlib 库的 axes 模块中的Axes.barh()函数用于制作水平条形图。
Syntax: Axes.barh(self, y, width, height=0.8, left=None, *, align=’center’, **kwargs)
Parameters: This method accept the following parameters that are described below:
- y: This parameter is the sequence of y 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.
- left : This parameter is also an optional parameter. And it is the x coordinate(s) of the left sides of the bars.
- align: This parameter is also an optional parameter. And it is used for alignment of the bars to the y coordinates.
Returns: This returns the following:
- BarContainer:This returns the container with all the bars and optionally errorbars.
下面的示例说明了 matplotlib.axes 中的 matplotlib.axes.Axes.barh()函数:
示例 #1:
# Implementation of matplotlib function
import matplotlib.pyplot as plt
import numpy as np
data = ((1000, 30), (30, 10),
(30, 100), (800, 500),
(50, 10))
dim = len(data[0])
w = 0.3
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.barh(x + i * dimw, y,
dimw, left = 0.001)
ax.set_yticks(x + dimw / 2)
ax.set_yticklabels(map(str, x))
ax.set_xscale('log')
ax.set_title('matplotlib.axes.Axes.barh 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.barh(labels, mine, width,
xerr = Mine_std,
label ='Mine')
ax.barh(labels, others, width,
xerr = Others_std,
left = mine,
label ='Others')
ax.set_xlabel('Articles')
ax.legend()
ax.set_title('matplotlib.axes.Axes.barh Example')
plt.show()
输出: