Matplotlib legend, add items across columns instead of down

前端 未结 2 1356
夕颜
夕颜 2020-12-09 07:54

For a simple plot below, is there a way to make matplotlib populate the legend so that it fills the rows left to right, instead of first column then second column?



        
2条回答
  •  轮回少年
    2020-12-09 08:29

    By default, the legend will fill all allocated columns before adding a new row. You can therefore re-order the handles and labels together to take advantage of this:

    handles, labels = ax1.get_legend_handles_labels()
    handles = np.concatenate((handles[::2],handles[1::2]),axis=0)
    labels = np.concatenate((labels[::2],labels[1::2]),axis=0)
    

提交回复
热议问题