matplotlib pyplot.colormaps 'function' object is not subscriptable

79 Views Asked by At

Python 3.9.7 anaconda command line client 1.9.0

I copy + paste code from this tutorial from matplotlib.

The error is at line 24.

cmap = plt.colormaps["plasma"]

TypeError: 'function' object is not subscriptable

So perhaps matplotlib.pyplot.colormaps used to be a dictionary but now it is not. The documentation on matplotlib also suggests similar form as in the tutorial.

import matplotlib as mpl
cmap = mpl.colormaps['viridis']

There are a number of questions here, answers to which would be helpful.

• How to report to maintainiers of the matplotlib site?

• What is the most up to date way to obtain a colormap from matplotlib.pyplot.colorplots?

Thanks all

1

There are 1 best solutions below

0
elscan On

Thanks all,

JohanC answered in the comments. Older versions of Matplotlib ( including version 3.4.3 which I am on ) use:

cmap = plt.get_cmap('viridis')

I should indeed have gotten my matplotlib version information and either upgraded or looked at the matplotlib homepage [https://matplotlib.org/stable/] more closely. Toward the top right, there is a drop down menu with the word "stable" on it (similar to the path of the url, stable ). I can select 3.4 from the dropdown menu to get to https://matplotlib.org/3.4.3/index.html. From there I eventually find the matplotlib.cm.get_cmap page.