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

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

提交回复
热议问题