Home >Backend Development >Python Tutorial >How Can I Display Values on a Horizontal Bar Plot in Matplotlib?
Displaying Values on Horizontal Bars
To display the value of each bar in a horizontal bar plot, you can use the following method:
for i, v in enumerate(y): ax.text(v + 3, i, str(v), color='blue', fontweight='bold', verticalalignment='center')
Explanation:
Example:
# Update the x and y values from the original example x = [u'INFO', u'CUISINE', u'TYPE_OF_PLACE', u'DRINK', u'PLACE', u'MEAL_TIME', u'DISH', u'NEIGHBOURHOOD'] y = [160, 167, 137, 18, 120, 36, 155, 130] fig, ax = plt.subplots() width = 0.75 # the width of the bars ind = np.arange(len(y)) # the x locations for the groups ax.barh(ind, y, width, color="blue") ax.set_yticks(ind + width / 2) ax.set_yticklabels(x, minor=False) plt.title('title') plt.xlabel('x') plt.ylabel('y') # Add code to display the values on the bars for i, v in enumerate(y): ax.text(v + 3, i, str(v), color='blue', fontweight='bold', verticalalignment='center') # Display the plot plt.show()
This will produce a horizontal bar plot with the values displayed on top of each bar.
The above is the detailed content of How Can I Display Values on a Horizontal Bar Plot in Matplotlib?. For more information, please follow other related articles on the PHP Chinese website!