从图形中创建matplotlib图例

-1 投票
1 回答
3999 浏览
提问于 2025-04-15 21:41

我这样添加了图例:

leg = fig.legend((l0,l1,l2,l3,l4,l5,l6), 
    ('0 Cl : r2, slope, origin',
    '1 Cl :'+str(r1b)+' , '+str(m1)+' , '+str(b1),  
    '2 Cl :'+str(r2b)+' , '+str(m2)+' , '+str(b2),  
    '3 Cl :'+str(r3b)+' , '+str(m3)+' , '+str(b3),  
    '4 Cl :'+str(r4b)+' , '+str(m4)+' , '+str(b4),  
    '5 Cl :'+str(r5b)+' , '+str(m5)+' , '+str(b5),  
    '6 Cl :'+str(r6b)+' , '+str(m6)+' , '+str(b6),  
    ),  'upper right')

但是图例却出现在了图表内部。

我该怎么告诉matplotlib把图例放到图表的右边呢?

1 个回答

3

你试过这个吗:

fig.legend((plot1,plot2), (lab1,lab2), 'right')

'right' 这个选项会把图例显示在坐标轴的右边。

至于第二个问题(关于命令的帮助),你可以看看 matplotlib 的示例(比如这个 http://matplotlib.sourceforge.net/examples/api/legend_demo.html)和 API 文档(例如 http://matplotlib.sourceforge.net/api/figure_api.html#module-matplotlib.figure

撰写回答