Matplotlib Bar Chart
We can use the bar() method in pyplot to draw bar charts.
The syntax of the bar() method is as follows:
matplotlib.pyplot.bar(x, height, width=0.8, bottom=None, *, align='center', data=None, **kwargs)
Parameter Description:
x: Float array, x-axis data for the bar chart.
height: Float array, the height of the bars.
width: Float array, the width of the bars.
bottom: Float array, the y-coordinate of the base, default 0.
align: The alignment of the bars with the x coordinates; 'center' centers on the x position, which is the default. 'edge': aligns the left edge of the bar with the x position. To align the right edge of a bar, you can pass a negative width value with align='edge'.
****kwargs: **: Other parameters.
In the following example we simply use bar() to create a bar chart:
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
x
=
np.
array
(
[
"Runoob-1"
,
"Runoob-2"
,
"Runoob-3"
,
"C-RUNOOB"
]
)
y
=
np.
array
(
[
12
,
22
,
6
,
18
]
)
plt.
bar
(
x
,
y
)
plt.
show
(
)
The display result is as follows:

Vertical bar charts can be set using the barh() method:
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
x
=
np.
array
(
[
"Runoob-1"
,
"Runoob-2"
,
"Runoob-3"
,
"C-RUNOOB"
]
)
y
=
np.
array
(
[
12
,
22
,
6
,
18
]
)
plt.
barh
(
x
,
y
)
plt.
show
(
)
The display result is as follows:

Set the bar chart color:
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
x
=
np.
array
(
[
"Runoob-1"
,
"Runoob-2"
,
"Runoob-3"
,
"C-RUNOOB"
]
)
y
=
np.
array
(
[
12
,
22
,
6
,
18
]
)
plt.
bar
(
x
,
y
,
color
=
"#4CAF50"
)
plt.
show
(
)
The display result is as follows:

Customize the color of each bar:
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
x
=
np.
array
(
[
"Runoob-1"
,
"Runoob-2"
,
"Runoob-3"
,
"C-RUNOOB"
]
)
y
=
np.
array
(
[
12
,
22
,
6
,
18
]
)
plt.
bar
(
x
,
y
,
color
=
[
"#4CAF50"
,
"red"
,
"hotpink"
,
"#556B2F"
]
)
plt.
show
(
)
The display result is as follows:

Set the bar chart width,bar() method uses width Set ,barh() method uses height Set height
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
x
=
np.
array
(
[
"Runoob-1"
,
"Runoob-2"
,
"Runoob-3"
,
"C-RUNOOB"
]
)
y
=
np.
array
(
[
12
,
22
,
6
,
18
]
)
plt.
bar
(
x
,
y
,
width
=
0.1
)
plt.
show
(
)
The display result is as follows:

Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
x
=
np.
array
(
[
"Runoob-1"
,
"Runoob-2"
,
"Runoob-3"
,
"C-RUNOOB"
]
)
y
=
np.
array
(
[
12
,
22
,
6
,
18
]
)
plt.
barh
(
x
,
y
,
height
=
0.1
)
plt.
show
(
)
The display result is as follows:
