Matplotlib: how do I know which colormap is being used?

Viewed 2673

I played around with colormaps, trying many of them, trying to make my own, both in matplotlib and seaborn.

However now I would like to know which colormap I am using. How can I do that? Is there a command like matplotlib.whichColormap ?

1 Answers

Usually there would be no need to find out the colormap you are using because you define that yourself. I.e. when calling

plt.imshow(..., cmap="viridis")

you already know that you are using "viridis".

If you still feel it would be useful to get that information from an existing ScalarMappable, you may use get_cmap() and it's name attribute:

import matplotlib.pyplot as plt
import numpy as np

a = np.random.rand(4,5)
fig, ax = plt.subplots()
im = ax.imshow(a, cmap="viridis")

cm = im.get_cmap()
print(cm.name)  # prints viridis
Related