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

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

提交回复
热议问题