Matplotlib Remove Colorbar and Specific Tick Labels

I was working on a Python data visualization project for a U.S.-based retail analytics dashboard. I had to create several heatmaps and scatter plots using Matplotlib, but I realized that some of the colorbars and tick labels were unnecessary. They made the chart look cluttered and distracted from the main insights.

As a Python developer with over 10 years of experience, I’ve learned that simplicity and clarity are key when it comes to data visualization. In this article, I’ll show you how to remove colorbars and specific tick labels in Matplotlib using two simple methods for each.

Whether you’re working on a financial visualization, a population heatmap, or a climate data chart, these techniques will help you make your plots look clean and professional.

Why You Might Want to Remove Colorbar or Tick Labels

When creating visualizations in Python using Matplotlib, colorbars and tick labels are often automatically added. While they’re useful, sometimes they add unnecessary noise.

For example, if you’re combining multiple subplots or creating a minimalist dashboard, you might want to hide the colorbar or specific tick labels. Removing them helps focus attention on the data itself.

Remove Colorbar in Matplotlib

Let’s start with removing colorbars. There are a couple of easy ways to do this in Python’s Matplotlib library.

Method 1 – Remove Colorbar Using colorbar.remove() in Matplotlib

This is the easy method. When you create a colorbar in Matplotlib, it returns a colorbar object. You can remove it by calling the .remove() method.

Here’s how I do it in my Python projects.

import matplotlib.pyplot as plt
import numpy as np

# Create sample data
data = np.random.rand(10, 10)

# Create a heatmap
fig, ax = plt.subplots()
heatmap = ax.imshow(data, cmap='coolwarm')

# Add a colorbar
cbar = plt.colorbar(heatmap)

# Display the plot with colorbar
plt.show()

# Now remove the colorbar
cbar.remove()

# Display the plot again without the colorbar
plt.show()

I executed the above example code and added the screenshot below.

Matplotlib Remove Colorbar and Specific Tick Labels

In the code above, I first generate a random 10×10 dataset and display it as a heatmap. The plt.colorbar() function adds a colorbar, which I then remove using cbar.remove(). This method works perfectly when you have direct access to the colorbar object.

Method 2 – Remove Colorbar by Clearing the Axes

Sometimes, you might not have direct access to the colorbar object, for example, when you’re dealing with multiple subplots or using Seaborn (which uses Matplotlib under the hood).

Here’s how you can do it in Python.

import matplotlib.pyplot as plt
import numpy as np

# Create sample data
data = np.random.rand(8, 8)

# Create a figure and axes
fig, ax = plt.subplots()

# Display the heatmap
img = ax.imshow(data, cmap='viridis')

# Add a colorbar
cbar = fig.colorbar(img, ax=ax)

# Remove the colorbar by clearing its axes
cbar.ax.clear()

# Display the plot without the colorbar
plt.show()

I executed the above example code and added the screenshot below.

C:\Users\GradyArchie\Downloads\images\Remove Colorbar and Specific Tick Labels in Matplotlib.jpg

In this example, instead of using cbar.remove(), I used cbar.ax.clear(). This clears everything inside the colorbar’s axes, effectively removing it from the visualization.

This method is useful when you’re working with more complex figures or when the colorbar is added automatically by another library.

Part 2 – Remove Specific Tick Labels in Matplotlib

Now, let’s move on to removing specific tick labels in Matplotlib.

Sometimes, you don’t want to remove all tick labels, just a few. For instance, if you’re plotting population data for U.S. states, you might want to hide certain labels to prevent overlapping or clutter.

Python’s Matplotlib gives us several ways to do that.

Method 1 – Remove Specific Tick Labels Using set_xticklabels() or set_yticklabels()

This is the simplest method. You can manually modify the tick labels list and replace specific ones with empty strings.

Here’s a practical Python example.

import matplotlib.pyplot as plt
import numpy as np

# Create sample data
x = np.arange(0, 10)
y = np.random.randint(10, 100, size=10)

# Create a bar chart
plt.bar(x, y, color='orange')

# Set custom tick labels
labels = [str(i) if i not in [2, 5, 8] else '' for i in x]
plt.xticks(x, labels)

plt.title("U.S. Monthly Sales Data (Example)")
plt.xlabel("Month Number")
plt.ylabel("Sales (in thousands)")
plt.show()

I executed the above example code and added the screenshot below.

Remove Colorbar and Specific Tick Labels Matplotlib

In this Python code, I created a simple bar chart showing monthly sales data. I used a list comprehension to hide the tick labels for months 2, 5, and 8 by replacing them with empty strings.

Method 2 – Remove Specific Tick Labels Using tick_params() and Label Visibility

Another way to remove specific tick labels in Matplotlib is by using the tick_params() function. This function allows you to control tick label visibility, direction, size, and more.

Here’s how I use it in my projects.

import matplotlib.pyplot as plt
import numpy as np

# Sample data
x = np.linspace(0, 10, 11)
y = np.sin(x)

# Create a line plot
fig, ax = plt.subplots()
ax.plot(x, y, marker='o', color='green')

# Hide specific tick labels (e.g., for x=2, 6, and 8)
for label in ax.get_xticklabels():
    if label.get_text() in ['2', '6', '8']:
        label.set_visible(False)

plt.title("Python Matplotlib Example – Removing Specific Tick Labels")
plt.xlabel("X-Axis Values")
plt.ylabel("Sine Function Output")
plt.show()

I executed the above example code and added the screenshot below.

Matplotlib Remove Colorbar & Specific Tick Labels

In this Python example, I loop through all the tick labels on the x-axis using ax.get_xticklabels(). Then, I selectively hide the ones I don’t want by setting label.set_visible(False).

This approach gives you more flexibility, especially when dealing with dynamically generated tick labels.

Pro Tip: Remove All Tick Labels (Optional)

If you ever want to remove all tick labels from your Matplotlib chart, you can do it easily using the following Python code:

import matplotlib.pyplot as plt
import numpy as np

data = np.random.rand(10, 10)

plt.imshow(data, cmap='plasma')
plt.xticks([])
plt.yticks([])
plt.title("Heatmap Without Tick Labels")
plt.show()

This method is great for minimalist visualizations or dashboards where tick labels are unnecessary.

Best Practices for Clean Matplotlib Visualizations

Here are a few quick tips I’ve learned over the years as a Python developer:

  • Always label your axes clearly if you remove tick labels.
  • Use colorbars only when they add context (for example, in heatmaps or contour plots).
  • Keep your plots simple; less is often more.
  • When working on dashboards, test how your plots look on different screen sizes.

Following these small steps can make your Python visualizations look professional and presentation-ready.

So that’s how you can remove colorbars and specific tick labels in Matplotlib using Python. We covered two easy methods for each scenario:

  • Removing colorbars using .remove() or by clearing axes
  • Removing specific tick labels using set_xticklabels() or tick_params()

Both techniques are simple yet powerful, and they’ll help you create cleaner, more focused visualizations.

You may also like to 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.