matplotlib: 2 different legends on same graph

前端 未结 4 2013
清歌不尽
清歌不尽 2020-12-02 11:08

I have a plot where different colors are used for different parameters, and where different line styles are used for different algorithms. The goal is to compare the results

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-02 11:45

    There's a section in the matplotlib documentation on that exact subject: http://matplotlib.org/users/legend_guide.html#multiple-legends-on-the-same-axes

    Here's code for your specific example:

    import itertools
    from matplotlib import pyplot
    
    colors = ['b', 'r', 'g', 'c']
    cc = itertools.cycle(colors)
    plot_lines = []
    for p in parameters:
    
        d1 = algo1(p)
        d2 = algo2(p)
        d3 = algo3(p)
    
        pyplot.hold(True)
        c = next(cc)
        l1, = pyplot.plot(d1, '-', color=c)
        l2, = pyplot.plot(d2, '--', color=c)
        l3, = pyplot.plot(d3, '.-', color=c)
    
        plot_lines.append([l1, l2, l3])
    
    legend1 = pyplot.legend(plot_lines[0], ["algo1", "algo2", "algo3"], loc=1)
    pyplot.legend([l[0] for l in plot_lines], parameters, loc=4)
    pyplot.gca().add_artist(legend1)
    

    Here's an example of its output: Plot with 2 legends, per-param and per-algo

提交回复
热议问题