加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-16 21:49:16 所属栏目:Python 来源:网络整理
导读:我想在图例下的“红色圆圈”符号旁边添加一个“红色填充方形”符号.我该如何实现这一目标?我更喜欢坚持使用pyplot而不是pylab. 以下是我一直在使用的代码: fig = plt.figure()ax1 = fig.add_axes([0.1,0.29,0.86,0.68])plt.ylabel('Radial Velocity (km s$

我想在图例下的“红色圆圈”符号旁边添加一个“红色填充方形”符号.我该如何实现这一目标?我更喜欢坚持使用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,'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')

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

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

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

最佳答案
解决异常问题

怀疑你需要这样做:

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)})

gist

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读