🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Python Notes
Topic #126

Matplotlib Bars


Creating Bars

With Pyplot, you can use the bar() function to draw bar graphs:

Example

  import matplotlib.pyplot as plt
import numpy as np

x = np.array(["A",
  "B", "C", "D"])
y = np.array([3, 8, 1, 10])

plt.bar(x,y)
plt.show()

image

The bar() function takes arguments that describes the layout of the bars.

The categories and their values represented by the first and second argument as arrays.

Example

x = ["APPLES", "BANANAS"]
y = [400, 350]
plt.bar(x, y)

Horizontal Bars

If you want the bars to be displayed horizontally instead of vertically, use the barh() function:

Example

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

image


Bar Color

The bar() and barh() take the keyword argument color to set the color of the bars:

Example

  import matplotlib.pyplot as plt
import numpy as np

x = np.array(["A",
  "B", "C", "D"])
y = np.array([3, 8, 1, 10])

plt.bar(x, y, color = "red")
plt.show()

image

Color Names

You can use any of the 140 supported color names.

Example

  import matplotlib.pyplot as plt
import numpy as np

x = np.array(["A",
  "B", "C", "D"])
y = np.array([3, 8, 1, 10])

plt.bar(x, y, color = "hotpink")
plt.show()

image

Color Hex

Or you can use Hexadecimal color values:

Example

  import matplotlib.pyplot as plt
import numpy as np

x = np.array(["A",
  "B", "C", "D"])
y = np.array([3, 8, 1, 10])

plt.bar(x, y, color = "#4CAF50")
plt.show()

image


Bar Width

The bar() takes the keyword argument width to set the width of the bars:

Example

  import matplotlib.pyplot as plt
import numpy as np

x = np.array(["A",
  "B", "C", "D"])
y = np.array([3, 8, 1, 10])

plt.bar(x, y, width = 0.1)
plt.show()

image

The default width value is 0.8

Note: For horizontal bars, use height instead of width.


Bar Height

The barh() takes the keyword argument height to set the height of the bars:

Example

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

image

The default height value is 0.8

Want to go beyond the notes?

Join CodingNow 2.0's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Matplotlib Bars – FAQs

Quick answers about learning Matplotlib Bars in Python.

This free note from CodingNow 2.0 explains Matplotlib Bars in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on CodingNow 2.0, including Matplotlib Bars, is 100% free with no signup required.
With focused practice, most students grasp Matplotlib Bars in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now