How to pick a new color for each plotted line within a figure in matplotlib?

前端 未结 7 670
日久生厌
日久生厌 2020-11-27 11:06

I\'d like to NOT specify a color for each plotted line, and have each line get a distinct color. But if I run:

from matplotlib import pyplot as plt
for i in          


        
相关标签:
7条回答
  • 2020-11-27 11:25

    I usually use the second one of these:

    from matplotlib.pyplot import cm
    import numpy as np
    
    #variable n below should be number of curves to plot
    
    #version 1:
    
    color=cm.rainbow(np.linspace(0,1,n))
    for i,c in zip(range(n),color):
       plt.plot(x, y,c=c)
    
    #or version 2:
    
    color=iter(cm.rainbow(np.linspace(0,1,n)))
    for i in range(n):
       c=next(color)
       plt.plot(x, y,c=c)
    

    Example of 2: example plot with iter,next color

    0 讨论(0)
  • 2020-11-27 11:27

    prop_cycle

    color_cycle was deprecated in 1.5 in favor of this generalization: http://matplotlib.org/users/whats_new.html#added-axes-prop-cycle-key-to-rcparams

    # cycler is a separate package extracted from matplotlib.
    from cycler import cycler
    import matplotlib.pyplot as plt
    
    plt.rc('axes', prop_cycle=(cycler('color', ['r', 'g', 'b'])))
    plt.plot([1, 2])
    plt.plot([2, 3])
    plt.plot([3, 4])
    plt.plot([4, 5])
    plt.plot([5, 6])
    plt.show()
    

    Also shown in the (now badly named) example: http://matplotlib.org/1.5.1/examples/color/color_cycle_demo.html mentioned at: https://stackoverflow.com/a/4971431/895245

    Tested in matplotlib 1.5.1.

    0 讨论(0)
  • 2020-11-27 11:33

    matplotlib 1.5+

    You can use axes.set_prop_cycle (example).

    matplotlib 1.0-1.4

    You can use axes.set_color_cycle (example).

    matplotlib 0.x

    You can use Axes.set_default_color_cycle.

    0 讨论(0)
  • 2020-11-27 11:34

    I don't know if you can automatically change the color, but you could exploit your loop to generate different colors:

    for i in range(20):
       ax1.plot(x, y, color = (0, i / 20.0, 0, 1)
    

    In this case, colors will vary from black to 100% green, but you can tune it if you want.

    See the matplotlib plot() docs and look for the color keyword argument.

    If you want to feed a list of colors, just make sure that you have a list big enough and then use the index of the loop to select the color

    colors = ['r', 'b', ...., 'w']
    
    for i in range(20):
       ax1.plot(x, y, color = colors[i])
    
    0 讨论(0)
  • 2020-11-27 11:39

    As Ciro's answer notes, you can use prop_cycle to set a list of colors for matplotlib to cycle through. But how many colors? What if you want to use the same color cycle for lots of plots, with different numbers of lines?

    One tactic would be to use a formula like the one from https://gamedev.stackexchange.com/a/46469/22397, to generate an infinite sequence of colors where each color tries to be significantly different from all those that preceded it.

    Unfortunately, prop_cycle won't accept infinite sequences - it will hang forever if you pass it one. But we can take, say, the first 1000 colors generated from such a sequence, and set it as the color cycle. That way, for plots with any sane number of lines, you should get distinguishable colors.

    Example:

    from matplotlib import pyplot as plt
    from matplotlib.colors import hsv_to_rgb
    from cycler import cycler
    
    # 1000 distinct colors:
    colors = [hsv_to_rgb([(i * 0.618033988749895) % 1.0, 1, 1])
              for i in range(1000)]
    plt.rc('axes', prop_cycle=(cycler('color', colors)))
    
    for i in range(20):
        plt.plot([1, 0], [i, i])
    
    plt.show()
    

    Output:

    Now, all the colors are different - although I admit that I struggle to distinguish a few of them!

    0 讨论(0)
  • 2020-11-27 11:46

    You can use a predefined "qualitative colormap" like this:

    from matplotlib.cm import get_cmap
    
    name = "Accent"
    cmap = get_cmap(name)  # type: matplotlib.colors.ListedColormap
    colors = cmap.colors  # type: list
    axes.set_prop_cycle(color=colors)
    

    Tested on matplotlib 3.0.3. See https://github.com/matplotlib/matplotlib/issues/10840 for discussion on why you can't call axes.set_prop_cycle(color=cmap).

    A list of predefined qualititative colormaps is available at https://matplotlib.org/gallery/color/colormap_reference.html :

    List of qualitative colormaps

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