Matplotlib legends in subplot

前端 未结 3 1770
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-25 10:30

I would like to put legends inside each one of the subplots below. I\'ve tried with plt.legend but it didn\'t work.

Any suggestions?

Thanks in advance :-)

3条回答
  •  梦谈多话
    2020-12-25 10:43

    This does what you want and overcomes some of the problems in other answers:

    import matplotlib.pyplot as plt
    
    labels = ["HHZ 1", "HHN", "HHE"]
    colors = ["r","g","b"]
    
    f,axs = plt.subplots(3, sharex=True, sharey=True)
    
    # ---- loop over axes ----
    for i,ax in enumerate(axs):
      axs[i].plot([0,1],[1,0],color=colors[i],label=labels[i])
      axs[i].legend(loc="upper right")
    
    plt.show()
    

    ... produces ...

提交回复
热议问题