Skip to content
Related Articles

Related Articles

Matplotlib.axes.Axes.bar() in Python
  • Last Updated : 13 Apr, 2020
GeeksforGeeks - Summer Carnival Banner

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.bar() Function

The Axes.bar() function in axes module of matplotlib library is used to make a bar plot.

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.

Below examples illustrate the matplotlib.axes.Axes.bar() function in matplotlib.axes:

Example #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()

Output:

Example #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()

Output:

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :