bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Python/Data Science and Scientific Python
Python•Data Science and Scientific Python

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

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

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

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

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

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

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

The default height value is 0.8

Previous

Machine Learning - Multiple Regression

Next

Machine Learning - Scale

This chapter

Overview
38

Lessons

159m

Read time

1. NumPy Tutorial2. Matplotlib Tutorial3. Machine Learning4. Pandas Tutorial5. Matplotlib Getting Started6. Machine Learning - Mean Median Mode7. Matplotlib Pyplot8. Machine Learning - Standard Deviation9. Matplotlib Plotting10. Machine Learning - Percentiles11. Matplotlib Markers12. Machine Learning - Data Distribution13. Matplotlib Line14. Machine Learning - Normal Data Distribution15. Matplotlib Labels and Title16. Machine Learning - Scatter Plot17. Matplotlib Adding Grid Lines18. Machine Learning - Linear Regression19. Matplotlib Subplot20. Machine Learning - Polynomial Regression21. Matplotlib Scatter22. Machine Learning - Multiple Regression23. Matplotlib Bars24. Machine Learning - Scale25. Matplotlib Histograms26. Machine Learning - Train/Test27. Matplotlib Pie Charts28. Machine Learning - Decision Tree29. Machine Learning - Confusion Matrix30. Machine Learning - Hierarchical Clustering31. Machine Learning - Logistic Regression32. Machine Learning - Grid Search33. Preprocessing - Categorical Data34. Machine Learning - K-means35. Machine Learning - Bootstrap Aggregation (Bagging)36. Machine Learning - Cross Validation37. Machine Learning - AUC - ROC Curve38. Machine Learning - K-nearest neighbors (KNN)

On this page

Creating BarsHorizontal BarsBar ColorColor NamesColor HexBar WidthBar Height