HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
With Pyplot, you can use the bar() function to draw bar graphs
With Pyplot, you can use the bar()
function
to draw bar graphs:
Draw 4 bars:
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.
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:
Draw 4 horizontal bars:
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()
The bar()
and barh()
takes the keyword argument
color
to set the color of the bars:
Draw 4 red bars:
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.
Draw 4 "hot pink" bars:
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:
Draw 4 bars with a beautiful green color:
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()
The bar()
takes the keyword argument
width
to set the width of the bars:
Draw 4 very thin bars:
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
.
The barh()
takes the keyword argument
height
to set the height of the bars:
Draw 4 very thin bars:
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