一个传奇的两个独特的标记符号

Roh*_*hit 5 python matplotlib

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

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

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) 
Run Code Online (Sandbox Code Playgroud)

这是我试过的:

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"])
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

在此输入图像描述

通过将numpoints = 1添加到常规图例()的最终解决方案起作用.这就是我想要的方式.谢谢tcaswell!

在此输入图像描述

tac*_*ell 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')
Run Code Online (Sandbox Code Playgroud)

plot返回一个Line2D对象列表(额外的将其,解包),我认为中的预期类型正在变得混乱。这解决了您的异常问题,但实际上并没有解决您的问题。

hacky 解决方案

解决这个问题的一些hacky方法是:

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

这给你三分,一分二分,一分一分。

清洁剂解决方案

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)})
Run Code Online (Sandbox Code Playgroud)

要旨

您可能需要x_offset稍微尝试一下以使其看起来正确,并且可能有更好的方法来自动确定它的价值应该是什么,但这应该足以让您开始。

在此处输入图片说明