Loading lesson path
Concept visual
Start from A
With Pyplot, you can use the bar() function to draw bar graphs:
Formula
import matplotlib.pyplot as plt import numpy as np x = np.array(["A","B", "C", "D"])
Formula
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)
If you want the bars to be displayed horizontally instead of vertically, use the barh() function:
Formula
import matplotlib.pyplot as plt import numpy as np x = np.array(["A","B", "C", "D"])
Formula
y = np.array([3, 8, 1, 10])plt.barh(x, y) plt.show()
and barh() take the keyword argument color to set the color of the bars:
Formula
import matplotlib.pyplot as plt import numpy as np x = np.array(["A","B", "C", "D"])
Formula
y = np.array([3, 8, 1, 10])plt.bar(x, y, color = "red") plt.show()
140 supported color names.
Draw 4 "hot pink" bars:
Formula
import matplotlib.pyplot as plt import numpy as np x = np.array(["A","B", "C", "D"])
Formula
y = np.array([3, 8, 1, 10])plt.bar(x, y, color = "hotpink") plt.show()
Draw 4 bars with a beautiful green color:
Formula
import matplotlib.pyplot as plt import numpy as np x = np.array(["A","B", "C", "D"])
Formula
y = np.array([3, 8, 1, 10])plt.bar(x, y, color = "#4CAF50") plt.show()
takes the keyword argument width to set the width of the bars: