height This parameter is either a scalar or sequence of scalar values representing the height(s) of the bars which makes the y-axis value. ,This parameter is either a scalar or sequence of scalar values representing the height(s) of the bars which makes the y-axis value.,This parameter is used to represent a sequence of scalar values that represents the x coordinates of the bars. The align parameter controls if x is the bar center (default) or left edge, x This parameter is used to represent a sequence of scalar values that represents the x coordinates of the bars. The align parameter controls if x is the bar center (default) or left edge

The required syntax to use this function with axes object is as follows:

`ax.bar(x, height, width, bottom, align)`

Given below is a simple example of the bar plot, which represents the number of books offered by the institute:

```
import numpy as np
import matplotlib.pyplot as plt
data = {
'Computer Networks': 20,
'DBMS': 15,
'Java': 30,
'C': 35
}
courses = list(data.keys())
values = list(data.values())
fig = plt.figure(figsize = (10, 5))
plt.bar(courses, values, color = 'magenta', width = 0.4)
plt.xlabel("Books offered")
plt.ylabel("No. of books provided")
plt.title("Books provided by the institute")
plt.show()
```

Now let us create a stacked plot and the code is given below:

import numpy as np import matplotlib.pyplot as plt N = 5 group_a = (25, 37, 39, 23, 56) group_b = (29, 36, 38, 25, 22) ind = np.arange(N) # the x locations for the groups width = 0.39 fig = plt.figure() ax = fig.add_axes([0, 0, 1, 1]) ax.bar(ind, group_a, width, color = 'c') ax.bar(ind, group_b, width, bottom = group_a, color = 'b') ax.set_ylabel('Scores') ax.set_title('Scores by two groups') ax.set_xticks(ind, ('G1', 'G2', 'G3', 'G4', 'G5')) ax.set_yticks(np.arange(0, 81, 10)) ax.legend(labels = ['Group A', 'Group B']) plt.show()

A simple thing you can do:

plt.bar(range(len(x)), x)

`left`

is the left ends of the bars. You're telling it where to place the bars on the horizontal axis. Here's something you can play around with until you get it:

```
>>>
import matplotlib.pyplot as plt >>>
plt.bar(range(10), range(20, 10, -1)) >>>
plt.show()
```

From the documentation http://matplotlib.sourceforge.net/api/pyplot_api.html#matplotlib.pyplot.bar

bar(left, height, width = 0.8, bottom = 0, ** kwargs)

where:

```
Argument Description
left-- > the x coordinates of the left sides of the bars
height-- > the heights of the bars
```

A simple example from http://scienceoss.com/bar-plot-with-custom-axis-labels/

# pylab contains matplotlib plus other goodies. import pylab as p #make a new figure fig = p.figure() # make a new axis on that figure.Syntax for add_subplot() is # number of rows of subplots, number of columns, and the # which subplot.So this says one row, one column, first # subplot--the simplest setup you can get. # See later examples for more. ax = fig.add_subplot(1, 1, 1) # your data here: x = [1, 2, 3] y = [4, 6, 3] # add a bar plot to the axis, ax. ax.bar(x, y) # after you 're all done with plotting commands, show the plot. p.show()

Last Updated : 04 Mar, 2021,GATE CS 2021 Syllabus

The **matplotlib** API in Python provides the bar() function which can be used in MATLAB style use or as an object-oriented API. The syntax of the bar() function to be used with the axes is as follows:-

`plt.bar(x, height, width, bottom, align)`

A bar chart or bar graph is a chart or graph that presents categorical data with rectangular bars with heights or lengths proportional to the values that they represent. The bars can be plotted vertically or horizontally.,The stacked bar chart stacks bars that represent different groups on top of each other. The height of the resulting bar shows the combined result of the groups.,Following is a simple example of the Matplotlib bar plot. It shows the number of students enrolled for various courses offered at an institute.,The optional bottom parameter of the pyplot.bar() function allows you to specify a starting value for a bar. Instead of running from zero to a value, it will go from the bottom to the value. The first call to pyplot.bar() plots the blue bars. The second call to pyplot.bar() plots the red bars, with the bottom of the blue bars being at the top of the red bars.

Matplotlib API provides the **bar()** function that can be used in the MATLAB style use as well as object oriented API. The signature of bar() function to be used with axes object is as follows −

`ax.bar(x, height, width, bottom, align)`

Following is a simple example of the Matplotlib bar plot. It shows the number of students enrolled for various courses offered at an institute.

```
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1])
langs = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23, 17, 35, 29, 12]
ax.bar(langs, students)
plt.show()
```

We can plot multiple bar charts by playing with the thickness and the positions of the bars. The data variable contains three series of four values. The following script will show three bar charts of four bars. The bars will have a thickness of 0.25 units. Each bar chart will be shifted 0.25 units from the previous one. The data object is a multidict containing number of students passed in three branches of an engineering college over the last four years.

```
import numpy as np
import matplotlib.pyplot as plt
data = [
[30, 25, 50, 20],
[40, 23, 51, 17],
[35, 22, 45, 19]
]
X = np.arange(4)
fig = plt.figure()
ax = fig.add_axes([0, 0, 1, 1])
ax.bar(X + 0.00, data[0], color = 'b', width = 0.25)
ax.bar(X + 0.25, data[1], color = 'g', width = 0.25)
ax.bar(X + 0.50, data[2], color = 'r', width = 0.25)
```

The code below adds two bar chars by calling the method twice. A width parameter is specified.,You can plot multiple bar charts in one plot. Need multiple bar charts?,You can change the color of the bar chart. To do that, just add the color parameter.The parameter can be set to an English color definition like ‘red’.,Matplotlib is a Python module that lets you plot all kinds of charts. Bar charts is one of the type of charts it can be plot. There are many different variations of bar charts.

123456789

#!/usr/bin/python3import numpy as npimport pandas as pdfrom pandas import Series, DataFrameimport matplotlib.pyplot as pltdata = [23, 45, 56, 78, 213]plt.bar([1,2,3,4,5], data)plt.show()

12345678

```
import numpy as npimport pandas as pdfrom pandas
import Series, DataFrameimport matplotlib.pyplot as pltdata = [23, 45, 56, 78, 213] plt.bar(range(len(data)), data, color = 'red') plt.show()
```

12345678910

```
import numpy as npimport pandas as pdfrom pandas
import Series, DataFrameimport matplotlib.pyplot as pltdata = [23, 45, 56, 78, 213] plt.bar(range(len(data)), data, color = 'royalblue', alpha = 0.7) plt.grid(color = '#95a5a6', linestyle = '--', linewidth = 2, axis = 'y', alpha = 0.7) plt.show()
```