Reverse colormap in matplotlib
The quickest way to reverse a matplotlib colormap is by appending _r
to its name:
Use this reversed map in your plots:
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:
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:
Reversing your custom colormaps
For those who love building their own ListedColormap
or LinearSegmentedColormap
, even these colormaps can learn to walk backwards:
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:
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:
Then plug this custom_cmap_r
into your plots for a tailored reversed visual.
Was this article helpful?