Matplotlib Bar Chart with Different Colors in Python

I was working on a Python project where I had to visualize sales data for different states in the USA. The dataset contained multiple categories, and I wanted each bar in my chart to stand out with its own unique color.

At first, I thought Matplotlib would have a quick one-click option to do this. But as I dug deeper, I realized there are multiple ways to color bars differently in Python, each with its own advantages.

In this tutorial, I will share my firsthand experience of plotting bar charts with different colors in Python using Matplotlib. I’ll cover simple methods, more advanced customization, and practical tips that I’ve learned working with Python.

Methods to Use Different Colors in a Python Bar Chart?

When you’re analyzing data, colors help highlight differences between categories. Instead of looking at plain blue bars, your audience can immediately identify key insights.

For example, if you’re comparing sales across different U.S. states, giving each state a unique color makes the chart easier to read and more engaging.

Method 1 – Assigning Colors Directly in Matplotlib

The simplest way to give each bar a different color in Python is to pass a list of colors to the color parameter of the bar() function.

import matplotlib.pyplot as plt

# Sample data: Sales in different US states
states = ["California", "Texas", "New York", "Florida", "Illinois"]
sales = [250, 180, 220, 150, 200]

# Define colors for each bar
colors = ["red", "green", "blue", "orange", "purple"]

# Create bar chart
plt.bar(states, sales, color=colors)

# Add title and labels
plt.title("Sales by State in USA")
plt.xlabel("States")
plt.ylabel("Sales (in thousands)")

# Show chart
plt.show()

You can refer to the screenshot below to see the output.

Matplotlib Bar Chart with Different Colors Python

In this example, each state is represented with a unique color. This method is quick, and it works perfectly when you have a small dataset.

Method 2 – Use a Colormap in Python

When I deal with larger datasets, assigning colors manually becomes impractical. That’s where Matplotlib colormaps come in handy.

A colormap automatically generates a range of colors that you can apply to your bars.

import matplotlib.pyplot as plt
import numpy as np

# Sample data: Random sales for 10 states
states = ["CA", "TX", "NY", "FL", "IL", "PA", "OH", "GA", "NC", "MI"]
sales = np.random.randint(100, 300, size=10)

# Generate colors using a colormap
colors = plt.cm.viridis(np.linspace(0, 1, len(states)))

# Create bar chart
plt.bar(states, sales, color=colors)

plt.title("Sales by State (Colormap Example)")
plt.xlabel("States")
plt.ylabel("Sales (in thousands)")
plt.show()

You can refer to the screenshot below to see the output.

Python Matplotlib Bar Chart with Different Colors

Here, I used the viridis colormap, but you can try others like plasma, coolwarm, or autumn. This method is more scalable and gives your chart a professional look.

Method 3 – Highlight Specific Bars in Python

Sometimes, I want to highlight one or two specific categories while keeping the rest in a neutral color. This is useful when I want to draw attention to a particular state or value.

import matplotlib.pyplot as plt

# Sample data
states = ["California", "Texas", "New York", "Florida", "Illinois"]
sales = [250, 180, 220, 150, 200]

# Default color for all bars
colors = ["lightgray"] * len(states)

# Highlight California in red
highlight_index = states.index("California")
colors[highlight_index] = "red"

# Create bar chart
plt.bar(states, sales, color=colors)

plt.title("Highlighting California Sales")
plt.xlabel("States")
plt.ylabel("Sales (in thousands)")
plt.show()

You can refer to the screenshot below to see the output.

Matplotlib Python Bar Chart with Different Colors in Python

This method is great when you want to emphasize one category without overwhelming the chart with too many colors.

Method 4 – Use Hex Codes for Custom Colors

Python allows you to use hex codes for precise color control. This is especially useful when you want to match brand colors or follow a specific design guideline.

import matplotlib.pyplot as plt

# Sample data
states = ["California", "Texas", "New York", "Florida", "Illinois"]
sales = [250, 180, 220, 150, 200]

# Custom hex colors
colors = ["#FF5733", "#33FF57", "#3357FF", "#FFC300", "#8E44AD"]

plt.bar(states, sales, color=colors)

plt.title("Sales by State with Custom Hex Colors")
plt.xlabel("States")
plt.ylabel("Sales (in thousands)")
plt.show()

You can refer to the screenshot below to see the output.

Matplotlib Bar Chart with Different Colors in Python

Using hex codes gives you complete control over the chart’s appearance and ensures consistency with other visuals.

Method 5 – Gradient Effect in Python Bar Charts

One trick I like to use is applying a gradient effect across the bars. This creates a smooth transition of colors and makes the chart visually striking.

import matplotlib.pyplot as plt
import numpy as np

# Sample data
states = ["CA", "TX", "NY", "FL", "IL", "PA", "OH", "GA", "NC", "MI"]
sales = np.random.randint(100, 300, size=10)

# Generate gradient colors
colors = plt.cm.cool(np.linspace(0, 1, len(states)))

plt.bar(states, sales, color=colors)

plt.title("Sales by State with Gradient Colors")
plt.xlabel("States")
plt.ylabel("Sales (in thousands)")
plt.show()

Gradients are particularly useful when you want to show a sense of progression or ranking across categories.

Best Practices for Coloring Bars in Python

Over the years, I’ve learned a few best practices when working with bar chart colors in Python:

  • Use consistent colors when comparing similar categories across multiple charts.
  • Avoid too many bright colors in one chart; it can overwhelm the reader.
  • Highlight only important data points instead of coloring everything differently.
  • Consider colorblind-friendly palettes to make your charts accessible.

Common Mistakes to Avoid

When I first started, I made some mistakes that I now avoid:

  • Using random colors without meaning, which confused the audience.
  • Choosing colors that were too similar making it hard to distinguish bars.
  • Forgetting to add a legend when colors had specific meanings.

Creating a Matplotlib bar chart with different colors in Python is not difficult once you know the right methods.

You can start with the simple approach of passing a list of colors, and then explore advanced techniques like colormaps, hex codes, and gradients.

Over time, you’ll develop your own style of using colors effectively to tell a story with your data.

You may read:

Leave a Comment

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.