python标记_python – 一个图例的两个唯一标记符号

解决异常问题

怀疑你需要这样做:

p1,= plt.plot(time_model,'k-')

p2,= plt.plot(time_model_sec,'k--')

p3,= plt.plot(time_obs,'bo')

p4,= plt.plot(time_obs_apg,'ro')

p5,= plt.plot(time_obs_apg_sec,'rs')

plot返回一个Line2D对象列表(额外的,解压缩它),我认为预期的类型正在被破坏.这可以解决您的异常问题,但实际上并不能解决您的问题.

hacky解决方案

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

plt.legend([p1,(p5,p4)],"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,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,=plt.plot(time_model_sec,=plt.plot(time_obs,=plt.plot(time_obs_apg,=plt.plot(time_obs_apg_sec,handler_map={p4:HandlerXoffset(x_offset=10),p5:HandlerXoffset(x_offset=-10)})

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值