I have been using the code from pylab, and it works fine:
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 it said "The pyplot interface is generally preferred for non-interactive plotting". So I tried to use matplotlib.cm but cannot find the equivalent. Could anyone help me with this?
Thanks!
The most common way to use matplotlib from within a script is to
import matplotlib.pyplot as plt
From there, you can access the Spectral colormap using
plt.cm.Spectral or through the convenience function plt.get_cmap. For example,
colors = plt.cm.Spectral(np.linspace(0, 1, 10))
or
colors = plt.get_cmap('Spectral')(np.linspace(0, 1, 10))
are equivalent to
colors = pl.cm.Spectral(np.linspace(0, 1, 10))
                        Should just 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.        ]])
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With