Matplotlib set_color_cycle versus set_prop_cycle

前端 未结 1 830
我在风中等你
我在风中等你 2020-12-09 16:23

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

1条回答
  •  萌比男神i
    2020-12-09 16:51

    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)))
    

    0 讨论(0)
提交回复
热议问题