matplotlib图例:在一行中包含来自两个不同图形的标记和线 [英] matplotlib legend: Including markers and lines from two different graphs in one line

查看:278
本文介绍了matplotlib图例:在一行中包含来自两个不同图形的标记和线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在进行一些线性回归,并希望在图例中的同一行上绘制标记(原始数据)和线条(回归).为简单起见,这是一个伪回归:

I've been doing some linear regression and want to plot the markers (original data) and the lines (regression) on the same line in the legend. For simplicity, here's a fake regression:

from pylab import *
ax = subplot(1,1,1)
p1, = ax.plot([1,2,3,4,5,6],'r-', label="line 1")
p2, = ax.plot([6,5,4,3,2,1],'b-', label="line 2")

p3, = ax.plot([1.2,1.8,3.1,4.1,4.8,5.9],'ro', label="dots 1")
p4, = ax.plot([6.1,5.1,3.8,3.1,1.9,0.9],'bo', label="dots 2")

ax.legend(loc='center right',numpoints=1)
show()

因此,我希望图例由2行组成,每行显示一行和一个点,而不是4行.我该怎么办?

So I want the legend to consist of 2 lines, each showing a line and a dot, instead of 4 lines. How can I do that?

推荐答案

您只需要直接使用legend即可.参见 Matplotlib -如何在不使线条透明的情况下使标记面颜色透明用户指南.

You just need to use legend a bit more directly. See Matplotlib - How to make the marker face color transparent without making the line transparent and user guide.

ax.legend([(p1, p3), (p2, p4)], ['set 1', 'set 2'])
plt.draw()

这篇关于matplotlib图例:在一行中包含来自两个不同图形的标记和线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆