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

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

提交回复
热议问题