我想在图例下的“红色圆圈”符号旁边添加一个“红色填充方形”符号。我该如何实现这一目标?我更喜欢坚持使用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)
这是我试过的:
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的建议更改代码后,我得到以下内容。看起来不错,但我希望只有一个蓝色符号,同时保持两个为红色。目前有两个。
通过将numpoints = 1添加到常规图例()的最终解决方案起作用。这就是我想要的方式。谢谢tcaswell!
答案 0 :(得分:4)
怀疑你需要这样做:
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
个对象的列表(额外的,
解包它),我认为预期的类型会被破坏。这可以解决您的异常问题,但实际上并不能解决您的问题。
一些解决这个问题的hacky方法是:
plt.legend([p1,p2,p3,(p5,p4)],["Primary", "Secondary", "XYZ", "This Work"],
handler_map={p4:HandlerLine2D(numpoints=2), p5:HandlerLine2D(numpoints=1)})
它给你三个点,一个中的两个,另一个中的一个。
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
来使其看起来正确,并且可能有更好的方法来自动确定它的值应该是什么,但这应该足以让您开始。