Matplotlib set_color_cycle versus set_prop_cycle

14,354

Because the new property cycler can iterate over other properties than just color (e.g. linestyle) you need to specify the label, i.e. the property over which to cycle.

ax.set_prop_cycle('color', colors)

There is no need to import and create a cycler though; so as I see it the only drawback of the new method it that it makes the call 8 characters longer.

There is no magical method that takes a colormap as input and creates the cycler, but you can also shorten your color list creation by directly supplying the numpy array to the colormap.

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

Or in combination

ax.set_prop_cycle('color',plt.cm.Spectral(np.linspace(0,1,30)))
Share:
14,354

Related videos on Youtube

DanHickstein
Author by

DanHickstein

I am currently a postdoc studying laser physics at the University of Colorado in Boulder, CO.

Updated on June 04, 2022

Comments

  • DanHickstein
    DanHickstein almost 2 years

    One of my favorite things to do in Matplotlib is to set the color-cycle to match some colormap, in order to produce line-plots that have a nice progression of colors across the lines. Like this one:

    enter image description here

    Previously, this was one line of code using set_color_cycle:

    ax.set_color_cycle([plt.cm.spectral(i) for i in np.linspace(0, 1, num_lines)])
    

    But, recently I see a warning:

    MatplotlibDeprecationWarning: 
    The set_color_cycle attribute was deprecated in version 1.5. 
    Use set_prop_cycle instead.
    

    Using set_prop_cycle, I can achieve the same result, but I need to import cycler, and the syntax is less compact:

    from cycler import cycler
    colors = [plt.cm.spectral(i) for i in np.linspace(0, 1, num_lines)]
    ax.set_prop_cycle(cycler('color', colors))
    

    So, my questions are:

    Am I using set_prop_cycle correctly? (and in the most efficient way?)

    Is there an easier way to set the color-cycle to a colormap? In other words, is there some mythical function like this?

    ax.set_colorcycle_to_colormap('jet', nlines=30)
    

    Here is the code for the complete example:

    import numpy as np
    import matplotlib.pyplot as plt
    
    ax = plt.subplot(111)
    num_lines = 30
    
    colors = [plt.cm.spectral(i) for i in np.linspace(0, 1, num_lines)]
    
    # old way: 
    ax.set_color_cycle(colors)
    
    # new way:
    from cycler import cycler
    ax.set_prop_cycle(cycler('color', colors))
    
    for n in range(num_lines):
        x = np.linspace(0,10,500)
        y = np.sin(x)+n
        ax.plot(x, y, lw=3)
    
    plt.show()
    
    • jds
      jds about 5 years
      plt.cm.get_cmap('spectral') for anyone who gets an error trying to access the spectral function.
    • Miguel Gonzalez
      Miguel Gonzalez over 2 years
      'spectral'.capitalize()
  • DanHickstein
    DanHickstein almost 7 years
    Ah, so now I can pretty easily specify a whole combination of different colors, linewidths, linestyles, etc. That does seem like an advantage. And the only price to pay is 8 more characters - I can live with that :). And thanks for the tip about passing the linspace to the colormap. In the end, I think that you saved me a few characters!
  • jhaagsma
    jhaagsma about 6 years
    I get this error, sadly: AttributeError: module 'matplotlib.cm' has no attribute 'spectral' Has matplotlib changed yet again?
  • ImportanceOfBeingErnest
    ImportanceOfBeingErnest about 6 years
    @jhaagsma The spectral colormap has been removed. You may use any other valid colormap instead, e.g. Spectral or nipy_spectral. I updated the answer.