Using
color parameter of bar method, we can set different colors to the bar chart
categories in Pyplot.
Example
fruits = ['Apple', 'Banana', 'Orange', 'Grapes']
liked_by_count = [120, 45, 32, 87]
colors = [(1, 0, 0), 'yellow', 'orange', '#00ff00']
plt.bar(fruits, liked_by_count, color=colors)
In the above example, the colors list contains colors corresponding to each category ('Apple', 'Banana', 'Orange', 'Grapes'). The color parameter is set to this list when calling plt.bar(), and each bar will be colored accordingly (For example, Apple is in red color, Banana is in yellow color etc.,).
Colors can be mentioned in
a. Simple names like red, blue, green etc.,
b. Using RGB notation (1, 0, 0) represent red color
c. Using hexadecimal color codes, #00ff00 specifies green color.
Find the below working application.
add_category_colors.py
import matplotlib.pyplot as plt
fruits = ['Apple', 'Banana', 'Orange', 'Grapes']
liked_by_count = [120, 45, 32, 87]
colors = [(1, 0, 0), 'yellow', 'orange', '#00ff00']
plt.bar(fruits, liked_by_count, color=colors)
plt.xlabel('Fruits')
plt.ylabel('Liked by people')
plt.title('Bar Chart Example')
plt.show()
Output
No comments:
Post a Comment