【问题标题】:How do i extend trend line in matplotlib plot?如何在 matplotlib 图中扩展趋势线?
【发布时间】:2020-10-07 06:27:49
【问题描述】:

Here is a part of the plot that I have

我需要创建将扩展到第三个的 TrendLine 这个情节的四分之一......我想不出任何解决方案。

import matplotlib.pyplot as plt
import warnings

warnings.filterwarnings('ignore')

x = [1, 8, 12, 20]
y = [1, 8.4, 12.5, 20]

fig = plt.figure(figsize=(20,20))
ax = fig.add_subplot()
ax.set_xlim(-30, 30)
ax.set_ylim(-20, 20)

plt.subplot().spines['left'].set_position('center')
plt.subplot().spines['bottom'].set_position('center')
plt.plot(x,y, 'b.', ms=20)
plt.minorticks_on()
ax.grid(True, which='both')
mean_line = ax.plot()
z = np.polyfit(x, y, 1)
p = np.poly1d(z)
plt.plot(x,p(x),"r--")

plt.show()

【问题讨论】:

    标签: python matplotlib data-science


    【解决方案1】:

    我不认为反向 x 和 y 可以完成这项工作,它仅限于通过 (0,0) 的 poly1d 我认为扩展方法应该是使用拟合线本身。

    所以更通用的方法是扩展 x 并使用 poly1d(z) 来计算延长线。 z 是拟合线的描述,因此将 x 值输入 z 将绘制线。

    import matplotlib.pyplot as plt
    import numpy as np
    import warnings
    
    warnings.filterwarnings('ignore')
    
    x = [1, 8, 12, 20]
    y = [1, 8.4, 12.5, 20]
    
    # make an xx that with from -20 to 20
    #xx =np.array(x)
    #xx = sorted(np.concatenate((-xx, xx), axis=0))
    xx = [-20, 20] # also work
    
    
    fig, ax = plt.subplots(figsize=(10,10))
    ax.set_xlim(-30, 30)
    ax.set_ylim(-20, 20)
    
    plt.subplot().spines['left'].set_position('center')
    plt.subplot().spines['bottom'].set_position('center')
    plt.subplot().spines['right'].set_color('none')
    plt.subplot().spines['top'].set_color('none')
    
    
    plt.plot(x,y, 'b.', ms=20)
    plt.minorticks_on()
    #ax.grid(True, which='both')
    plt.subplot().grid(True, which='both')
    mean_line = ax.plot()
    z = np.polyfit(x, y, 1)
    p = np.poly1d(z)
    
    plt.plot(xx,p(xx),"r--")
    
    plt.show()
    

    如果您在 (0,0) 附近放大,您应该会看到它没有经过原点。

    zoomed in near (0,0)

    result image

    【讨论】:

      【解决方案2】:

      我对趋势线没有任何经验,但我创建了具有不同符号的现有 x 和 y 值的组合,并绘制了以下图表。

      import matplotlib.pyplot as plt
      import warnings
      
      warnings.filterwarnings('ignore')
      
      x = [1, 8, 12, 20]
      y = [1, 8.4, 12.5, 20]
      
      fig = plt.figure(figsize=(10,10))
      ax = fig.add_subplot()
      ax.set_xlim(-30, 30)
      ax.set_ylim(-20, 20)
      
      plt.subplot().spines['left'].set_position('center')
      plt.subplot().spines['bottom'].set_position('center')
      plt.plot(x,y, 'b.', ms=20)
      plt.minorticks_on()
      ax.grid(True, which='both')
      mean_line = ax.plot()
      
      #  update
      xx =np.array(x)
      xx = sorted(np.concatenate((-xx, xx), axis=0))
      yy =np.array(y)
      yy = sorted(np.concatenate((-yy, yy), axis=0))
      
      z = np.polyfit(xx, yy, 1)
      p = np.poly1d(z)
      plt.plot(xx,p(xx),"r--")
      
      plt.show()
      

      【讨论】:

      • 谢谢!这正是我所需要的
      猜你喜欢
      • 1970-01-01
      • 2014-12-14
      • 2019-06-15
      • 2020-11-28
      • 2019-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多