Explain Codes LogoExplain Codes Logo

Reverse colormap in matplotlib

python
matplotlib
colormap
data-visualization
Nikita BarsukovbyNikita Barsukov·Mar 5, 2025
TLDR

The quickest way to reverse a matplotlib colormap is by appending _r to its name:

cmap_reversed = plt.cm.viridis_r # Now 'viridis' is teaching colors to count backwards!

Use this reversed map in your plots:

plt.imshow(data, cmap=cmap_reversed)

More ways to reverse a colormap

Beyond the fast answer, there are more nuances to reversing a colormap in matplotlib. Let's have a look at some additional methods that can come in handy in different situations!

Reversal method for built-in colormaps

Every built-in colormap in matplotlib has a twin sibling who just likes to do everything backwards, identifiable by the _r suffix. For instance, when using plot_surface, the reversed colormap can be implemented this way:

from mpl_toolkits.mplot3d import Axes3D fig = plt.figure() ax = fig.add_subplot(111, projection='3d') surface = ax.plot_surface(X, Y, Z, cmap='coolwarm_r') # Suddenly, cool things are getting warmer! plt.show()

Dynamic reversing in codes

Sometimes, you might want to reverse a colormap on the fly. Fear not, matplotlib's reversed method can allow you to create a "rewind" button for your colormaps. Available from matplotlib 2.0 onward:

from matplotlib.pyplot import get_cmap original_cmap = get_cmap('cividis') reversed_cmap = original_cmap.reversed() # Reverse, reverse! 🎵

Reversing your custom colormaps

For those who love building their own ListedColormap or LinearSegmentedColormap, even these colormaps can learn to walk backwards:

from matplotlib.colors import ListedColormap, LinearSegmentedColormap # A ListedColormap that learned to bend its stride listed_cmap = ListedColormap(['red', 'green', 'blue']) listed_cmap_reversed = ListedColormap(listed_cmap.colors[::-1]) # Walking on the color moonwalk # Same for the LinearSegmentedColormap linear_cmap = LinearSegmentedColormap.from_list('my_cmap', ['red', 'green', 'blue']) linear_cmap_reversed = LinearSegmentedColormap.from_list('my_cmap_r', linear_cmap.colors[::-1]) # Turning the time-turner like Hermione

A visual comparison of original and reversed colormaps

If you're a fan of comparisons and enjoy seeing your progress, ColorbarBase from matplotlib.colorbar can be your buddy:

from matplotlib.colorbar import ColorbarBase from matplotlib.colors import Normalize norm = Normalize(vmin=0, vmax=1) fig, ax = plt.subplots(2) cb1 = ColorbarBase(ax[0], cmap=listed_cmap, norm=norm, orientation='horizontal') # 😎 Here's how you looked before reversal cb2 = ColorbarBase(ax[1], cmap=listed_cmap_reversed, norm=norm, orientation='horizontal') # 🔄 And voilà, the new you! plt.tight_layout() plt.show()

Ensuring quality visualization with reversed colormaps

Reversing a colormap is not just about aesthetics, it's about maintaining data representation. Always confirm if the reversal aids or hinders comprehension of the data. Be a responsible data scientist!

In some special situations, you may need to handcraft a reversed colormap. Here's how:

from matplotlib.colors import ListedColormap custom_colors = ['#000000', '#123456', '#654321', '#ffffff'] # It's okay to be picky about your colors custom_cmap = ListedColormap(custom_colors) reverse_colors = custom_cmap.colors[::-1] # Colors, fall in line... from the back! custom_cmap_r = ListedColormap(reverse_colors)

Then plug this custom_cmap_r into your plots for a tailored reversed visual.