一个图例的两个唯一标记符号 [英] Two unique marker symbols for one legend

查看:67
本文介绍了一个图例的两个唯一标记符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在图例下的红色实心圆"符号旁边添加一个红色实心方形"符号.我如何实现这一目标?我更喜欢坚持使用 pyplot 而不是 pylab.

I would like to add a "red filled square" symbol beside the "red filled circle" symbol under legend. How do I achieve this? I prefer to stick with pyplot rather than pylab.

下面是我一直在使用的代码:

Below is the code I've been using:

fig = plt.figure()
ax1 = fig.add_axes([0.1,0.29,0.86,0.68])
plt.ylabel('Radial Velocity (km s$^{-1}$)')
plt.plot(time_model, rv_model_primary, 'k-', label = 'Primary')
plt.plot(time_model_sec, rv_model_secondary, 'k--', label = 'Secondary')
plt.plot(time_obs, rv_obs_primary, 'bo', label='XYZ')

plt.plot(time_obs_apg, rv_obs_primary_apg, 'ro', label='This Work')
plt.plot(time_obs_apg_sec, rv_obs_secondary_apg, 'rs')
plt.plot((0.0, 1.0),(0.0,0.0), 'k-.')
plt.legend(loc='upper left', numpoints=1) 

这是我尝试过的:

p1=plt.plot(time_model, rv_model_primary, 'k-')
p2=plt.plot(time_model_sec, rv_model_secondary, 'k--')
p3=plt.plot(time_obs, rv_obs_primary, 'bo')
p4=plt.plot(time_obs_apg, rv_obs_primary_apg, 'ro')
p5=plt.plot(time_obs_apg_sec, rv_obs_secondary_apg, 'rs')

plt.legend([p1,p2,p3,(p4,p5)],["Primary", "Secondary", "XYZ", "This Work"])

使用tcaswell的建议对代码进行更改后,我得到以下内容.看起来不错,但我希望只有一个蓝色符号,同时保留两个红色符号.目前有两个.

After making changes to the code using tcaswell's suggestions I get the following. The looks good but I would like to have just one symbol for blue while keeping the two for red. Currently there are two.

通过将numpoints = 1添加到一般legend()的最终解决方案起作用.这就是我想要的.谢谢你!

The final solution by adding numpoints=1 to the general legend() worked. Here's how I wanted it. Thanks tcaswell!

推荐答案

解决异常问题

怀疑你需要做的:

solve exception issue

Suspect you need to do:

p1, = plt.plot(time_model, rv_model_primary, 'k-')
p2, = plt.plot(time_model_sec, rv_model_secondary, 'k--')
p3, = plt.plot(time_obs, rv_obs_primary, 'bo')
p4, = plt.plot(time_obs_apg, rv_obs_primary_apg, 'ro')
p5, = plt.plot(time_obs_apg_sec, rv_obs_secondary_apg, 'rs')

plot 返回一个 Line2D 对象列表(额外的 , 将其解包),我认为其中的预期类型被搞砸了.这解决了您的异常问题,但实际上并没有解决您的问题.

plot returns a list of Line2D objects (the extra , unpacks it) and I think that the expected types in are getting mucked up. This fixes your exceptions issue, but does not actually solve your problem.

一些解决问题的方法是:

A some what hacky way to solve this is:

plt.legend([p1,p2,p3,(p5,p4)],["Primary", "Secondary", "XYZ", "This Work"],
           handler_map={p4:HandlerLine2D(numpoints=2), p5:HandlerLine2D(numpoints=1)})

这会给你三分,一分二分,一分一分.

which gives you three points, two of one and one of the other.

from matplotlib.legend_handler import HandlerLine2D

class HandlerXoffset(HandlerLine2D):
    def __init__(self, marker_pad=0.3, numpoints=1, x_offset=0,  **kw):
        HandlerLine2D.__init__(self, marker_pad=marker_pad, numpoints=numpoints, **kw)
        self._xoffset = x_offset
    def get_xdata(self, legend, xdescent, ydescent, width, height, fontsize):
        numpoints = self.get_numpoints(legend)

        if numpoints > 1:
            # we put some pad here to compensate the size of the
            # marker
            xdata = np.linspace(-xdescent + self._marker_pad * fontsize,
                                width - self._marker_pad * fontsize,
                                numpoints) - self._xoffset
            xdata_marker = xdata
        elif numpoints == 1:
            xdata = np.linspace(-xdescent, width, 2) - self._xoffset
            xdata_marker = [0.5 * width - 0.5 * xdescent - self._xoffset]

        print xdata, self._xoffset
        print xdata_marker

        return xdata, xdata_marker

time_model = time_model_sec = time_obs = time_obs_apg = time_obs_apg_sec = range(5)

rv_model_primary = np.random.rand(5)
rv_model_secondary = np.random.rand(5)
rv_obs_primary = np.random.rand(5)
rv_obs_primary_apg =  np.random.rand(5)
rv_obs_secondary_apg =  np.random.rand(5)

p1,=plt.plot(time_model, rv_model_primary, 'k-')
p2,=plt.plot(time_model_sec, rv_model_secondary, 'k--')
p3,=plt.plot(time_obs, rv_obs_primary, 'bo')
p4,=plt.plot(time_obs_apg, rv_obs_primary_apg, 'ro')
p5,=plt.plot(time_obs_apg_sec, rv_obs_secondary_apg, 'rs')

plt.legend([p1,p2,p3,(p5,p4)], 
           ["Primary", "Secondary", "XYZ", "This Work"],
            handler_map={p4:HandlerXoffset(x_offset=10),   
                         p5:HandlerXoffset(x_offset=-10)})

要点

您可能必须稍微尝试一下 x_offset 才能使它看起来正确,并且可能有更好的方法来自动确定其值是多少,但这应该足以获取您开始了.

You will probably have to play with x_offset a bit to make it look right, and there is probably a better way to automatically figure out what it's value should be, but this should be enough to get you started.

这篇关于一个图例的两个唯一标记符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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