Pyplot equivalent for pl.cm.Spectral in matplotlib

I am using code from pylab and it works great:

import pylab as pl
colors = pl.cm.Spectral(np.linspace(0, 1, 10))

However, I want to get away from pylab because in the document he said: "The pyplot interface is usually preferable for non-interactive building." So I tried to use matplotlib.cm, but could not find an equivalent. Can anyone help me with this?

Thank!

+4
source share
3 answers

The most common way to use matplotlib from a script is

import matplotlib.pyplot as plt

From there, you can access the color palette Spectralusing plt.cm.Spectralor through a convenient function plt.get_cmap. For instance,

colors = plt.cm.Spectral(np.linspace(0, 1, 10))

or

colors = plt.get_cmap('Spectral')(np.linspace(0, 1, 10))

are equivalent

colors = pl.cm.Spectral(np.linspace(0, 1, 10))
+7
source

Should only be cm.Spectral

In [123]:

import matplotlib.cm as cm
cm.Spectral(np.linspace(0,1,10))
Out[123]:
array([[ 0.61960787,  0.00392157,  0.25882354,  1.        ],
       [ 0.84721262,  0.26120723,  0.30519032,  1.        ],
       [ 0.96378316,  0.47743176,  0.28581316,  1.        ],
       [ 0.99346405,  0.74771243,  0.43529413,  1.        ],
       [ 0.99777009,  0.93087275,  0.63306423,  1.        ],
       [ 0.94425221,  0.97770089,  0.66205308,  1.        ],
       [ 0.74771243,  0.89803922,  0.627451  ,  1.        ],
       [ 0.45305653,  0.78154557,  0.64628991,  1.        ],
       [ 0.21607075,  0.55563248,  0.73194927,  1.        ],
       [ 0.36862746,  0.30980393,  0.63529414,  1.        ]])
In [119]:

import pylab as pl
pl.cm.Spectral(np.linspace(0, 1, 10))
Out[119]:
array([[ 0.61960787,  0.00392157,  0.25882354,  1.        ],
       [ 0.84721262,  0.26120723,  0.30519032,  1.        ],
       [ 0.96378316,  0.47743176,  0.28581316,  1.        ],
       [ 0.99346405,  0.74771243,  0.43529413,  1.        ],
       [ 0.99777009,  0.93087275,  0.63306423,  1.        ],
       [ 0.94425221,  0.97770089,  0.66205308,  1.        ],
       [ 0.74771243,  0.89803922,  0.627451  ,  1.        ],
       [ 0.45305653,  0.78154557,  0.64628991,  1.        ],
       [ 0.21607075,  0.55563248,  0.73194927,  1.        ],
       [ 0.36862746,  0.30980393,  0.63529414,  1.        ]])
+1
source
import matplotlib.pyplot as plt

colors = plt.get_cmap('Spectral')(np.linspace(0, 1, 10))

.

matplotlib :

colors = plt.cm.Spectral(np.linspace(0, 1, 10))

.

0

All Articles