Hello programmers, in today’s article, we will discuss the Matplotlib barh() in Python. The Pyplot library of the Matplotlib module helps plot graphs and bars very easily in Python. The matplotlib.pyplot.barh() function helps to make a horizontal bar plot. The bars are positioned at specific input values of ‘y’ with the given alignment. Their dimensions are specified by width and height. The horizontal baseline is left (default 0). Before we cite examples of the barh() function, let me just brief you with the syntax and return the same.
Syntax of Matplotlib barh()
matplotlib.pyplot.barh(self, y, width, height=0.8, left=None, *, align=’center’, **kwargs)
Parameters
y: Contains the y coordinates of the bars. It may be scalar or array-like.
width: The width(s) of the bars. It may be scalar or array-like.
height: Sequence of heights of the bars. It is scalar and default: 0.8. (Optional)
left: The sequence of x coordinates of the bars’ left sides (default: 0).
align: {‘center,’ ‘edge’} Alignment of the base to the y coordinates with default:
‘center.’ (optional)
‘center’: Center the bars on the y positions.
‘edge’: Align the bottom edges of the bars with the y positions.
**kwargs: Controls the properties and other parameters like edgecolor, linewidth, etc., of the horizontal bar graph.
Return type
The Matplotlib barh() function returns Bar Container with all the bars and optionally errorbars.
Example of Matplotlib barh() in Python
import matplotlib.pyplot as plt import numpy as np x = np.array(["A", "B", "C", "D"]) y = np.array([3, 8, 1, 10]) plt.barh(x, y) plt.show()
OUTPUT:
EXPLANATION:
The Matplotlib barh() function is used to plot horizontal bar graphs in Python. Two arrays are are defined using the Numpy arrange function. The array ‘x’ contains the name of the four horizontal bars. And the array ‘y’ contains the sequence of y coordinates of the bar. The ‘x’ and ‘y’ are passed as arguments to the matplotlib barh() function to get the desired horizontal bar plot.
Matplotlib barh() Height
import matplotlib.pyplot as plt import numpy as np x = np.array(["A", "B", "C", "D"]) y = np.array([3, 8, 1, 10]) plt.barh(x, y, height = 0.1) plt.show()
OUTPUT:
EXPLANATION:
The matplotlib barh() in python takes the keyword argument height to set the bars’ height. In the above example, two arrays, ‘x’ and ‘y’, are defined using the np.arrange(). The array ‘x’ basically contains the labels of the four horizontal bars as A, B, C, and D. The array ‘y’ contains the y coordinates of the bar to be plotted. And the height passed to the barh() function is 0.1, which plots four very thin horizontal bars. The default value of height parameters used in the barh() function is 0.8.
Matplotlib barh() Color
import matplotlib.pyplot as plt import numpy as np y = np.array([26, 17, 30]) plt.bar(y, color = "blue") plt.show()
OUTPUT:
EXPLANATION:
In the above example, the array ‘y’ contains the y – coordinates of the horizontal bar. The array ‘y’ and color is passed to the barh() function to get the desired bar plot. The color is set to ‘blue’ to obtain blue colored horizontal bar plots.
Other parameters with Matplotlib Stacked barh()
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:
EXPLANATION:
In the above example, other parameters like errorbars and labels are included. Two arrays – mine and others, are defined containing the coordinates points along the y-axis for the horizontal bar. The other two arrays – Mine_std and Others_std, contains the errorbars to the plotted along the x-axis for the bars. Two barh() functions are used to derive a stacked bar plot. The labels Mine and Others are used for the two bar plots.
Conclusion
In this article, we discussed different ways of implementing the horizontal bar plot using the Matplotlib barh() in Python. We have laid out examples of barh() height, color, etc., with detailed explanations. The barh() function to plot stacked horizontal bars is also explained with an example. Refer to this article for any queries related to this function.
However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.
Happy Pythoning!
The post Matplotlib barh() in Python With Examples appeared first on Python Pool.
from Planet Python
via read more
No comments:
Post a Comment