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

前端 未结 7 710
日久生厌
日久生厌 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: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

提交回复
热议问题