Instagram
youtube
Facebook
Twitter

Set Custom Colors for Each Product in a Bar Plot

Description:
This program creates a bar chart that displays units sold for different products, using a different custom color for each product.

Code Explanation:

  • matplotlib.pyplot is used to create the chart.

  • products are shown on the x-axis, and units_sold on the y-axis.

  • A list of custom colors is created, with one color for each product.

  • color=colors in plt.bar() assigns those custom colors to the bars.

  • Labels and a title are added for clarity.

  • plt.show() displays the final colored bar chart.


Program:

import matplotlib.pyplot as plt

# Product names and units sold
products = ['Product A', 'Product B', 'Product C', 'Product D']
units_sold = [150, 200, 120, 180]

# Custom colors for each bar
colors = ['skyblue', 'lightgreen', 'salmon', 'violet']

# Create the bar chart
plt.bar(products, units_sold, color=colors)

# Add labels and title
plt.xlabel('Products')
plt.ylabel('Units Sold')
plt.title('Units Sold per Product with Custom Colors')

# Show the plot
plt.show()


Output: