matplotlib.axes.Axes.barh() in Python
Last Updated :
13 Apr, 2020
Matplotlib is a library in Python and it is numerical – mathematical extension for NumPy library. The Axes Class contains most of the figure elements: Axis, Tick, Line2D, Text, Polygon, etc., and sets the coordinate system. And the instances of Axes supports callbacks through a callbacks attribute.
matplotlib.axes.Axes.barh() Function
The Axes.barh() function in axes module of matplotlib library is used to make a horizontal bar plot.
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.
Below examples illustrate the matplotlib.axes.Axes.barh() function in matplotlib.axes:
Example #1:
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()
|
Output:
Example #2:
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()
|
Output:
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...