matplotlib:超过 2 个控制点的延长线 [英] matplotlib: extended line over 2 control points

查看:93
本文介绍了matplotlib:超过 2 个控制点的延长线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在matplotlib中,我们可以使用至少两种方法绘制线条:

In matplotlib we can draw lines using at least 2 methods:

  1. plt.plot

  1. plt.plot

plt.plot([1,2],[1,2],color='k',marker='o')

  • Line2D 方法

  • Line2D method

    line = lines.Line2D([0.3,0.6],[0.9,0.3],linestyle='dashed',color='k')
    plt.axes().add_line(line)
    

  • 我怀疑这两种方法当然在实现上是相同的.但无论如何,它在两个规定的点之间精确地画了一条线.有时我需要将这 2 个点上的线延长到图形限制.当然我可以用 y=ax+b 的形式计算它,但有人知道更简单的方法吗?

    I suspect both methods are the same in implementation of course. But anyway, it draws a line exactly between 2 stated points. Sometimes I need to extend line over those 2 points up to graph limits. Sure I can calculate it in form of y=ax+b, but does anybody know way easier?

    如果我可以添加一些额外的选项,但我找不到它,那就太完美了.

    Perfect case if I can just put some additional option, but I wasn't able to find it.

    推荐答案

    吃过午饭后,我找到了使用 numpy 的方法.

    After good lunch I was able to find a way using numpy.

    def drawLine2P(x,y,xlims):
        xrange = np.arange(xlims[0],xlims[1],0.1)
        A = np.vstack([x, np.ones(len(x))]).T
        k, b = np.linalg.lstsq(A, y)[0]
        plt.plot(xrange, k*xrange + b, 'k')
    

    这篇关于matplotlib:超过 2 个控制点的延长线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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