Uniform color using RGB
You can change the color of bars in a barplot using color
argument.
RGB is a way of making colors. You have to to provide an amount of red, green, blue, and the transparency value to the color argument and it returns a color.
# libraries
import numpy as np
import matplotlib.pyplot as plt
# create a dataset
height = [3, 12, 5, 18, 45]
bars = ('A', 'B', 'C', 'D', 'E')
x_pos = np.arange(len(bars))
# Create bars
plt.bar(x_pos, height, color=(0.2, 0.4, 0.6, 0.6))
# Create names on the x-axis
plt.xticks(x_pos, bars)
# Show graph
plt.show()
Different color for each bar
If you want to give different colors to each bar, just provide a list of color names to the color
argument:
# libraries
import numpy as np
import matplotlib.pyplot as plt
# create a dataset
height = [3, 12, 5, 18, 45]
bars = ('A', 'B', 'C', 'D', 'E')
x_pos = np.arange(len(bars))
# Create bars with different colors
plt.bar(x_pos, height, color=['black', 'red', 'green', 'blue', 'cyan'])
# Create names on the x-axis
plt.xticks(x_pos, bars)
# Show graph
plt.show()
Control color of border
The edgecolor
argument allows you to color the borders of barplots.
# libraries
import numpy as np
import matplotlib.pyplot as plt
# create a dataset
height = [3, 12, 5, 18, 45]
bars = ('A', 'B', 'C', 'D', 'E')
x_pos = np.arange(len(bars))
# Create bars with blue edge color
plt.bar(x_pos, height, color=(0.1, 0.1, 0.1, 0.1), edgecolor='blue')
# Create names on the x-axis
plt.xticks(x_pos, bars)
# Show graph
plt.show()