【问题标题】:python matplotlib: How can I add a point mark to curve knowing only the x value?python matplotlib:如何在只知道 x 值的曲线上添加点标记?
【发布时间】:2019-12-29 07:40:14
【问题描述】:

例如,在 matplotlib 中,我根据几个点绘制了一条简单的曲线:

from matplotlib import pyplot as plt
import numpy as np

x=[0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. , 1.1, 1.2,
       1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1, 2.2, 2.3, 2.4, 2.5,
       2.6, 2.7, 2.8, 2.9]
y=[0.0, 0.19, 0.36, 0.51, 0.64, 0.75, 0.8400000000000001, 0.91, 0.96, 0.99, 1.0, 
   0.99, 0.96, 0.9099999999999999, 0.8399999999999999, 0.75, 0.6399999999999997, 
   0.5099999999999998, 0.3599999999999999, 0.18999999999999995, 0.0, 
   -0.20999999999999996, -0.4400000000000004, -0.6900000000000004, 
   -0.9600000000000009, -1.25, -1.5600000000000005, -1.8900000000000006, 
   -2.240000000000001, -2.610000000000001]

plt.plot(x,y)
plt.show()

假设,假设我想突出曲线上 x 值为 0.25 的点,但我不知道该点的 y 值。我该怎么办?

【问题讨论】:

    标签: python matplotlib


    【解决方案1】:

    最简单的解决方案是在提供的 x 值的相邻点之间执行线性插值。下面是一个示例代码来展示一般原理:

    X=[0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2,
       1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1, 2.2, 2.3, 2.4, 2.5,
       2.6, 2.7, 2.8, 2.9]
    Y=[0.0, 0.19, 0.36, 0.51, 0.64, 0.75, 0.8400000000000001, 0.91, 0.96,
       0.99, 1.0, 0.99, 0.96, 0.9099999999999999, 0.8399999999999999, 0.75,
       0.6399999999999997, 0.5099999999999998, 0.3599999999999999,
       0.18999999999999995, 0.0, -0.20999999999999996, -0.4400000000000004,
       -0.6900000000000004, -0.9600000000000009, -1.25, -1.5600000000000005,
       -1.8900000000000006, -2.240000000000001, -2.610000000000001]
    
    def interpolate(X, Y, xval):
        for n, x in enumerate(X):
            if x > xval: break
        else: return None # xval > last x value
        if n == 0: return None # xval < first x value
        xa, xb = X[n-1], X[n] # get surrounding x values
        ya, yb = Y[n-1], Y[n] # get surrounding y values
        if xb == xa: return ya # 
        return ya + (xval - xa) * (yb - ya) / (xb - xa) # compute yval by interpolation
    
    print(interpolate(X, Y, 0.25)) # --> 0.435 
    print(interpolate(X, Y, 0.85)) # --> 0.975
    print(interpolate(X, Y, 2.15)) # --> -0.3259999999999997
    print(interpolate(X, Y, -1.0)) # --> None (out of bounds)
    print(interpolate(X, Y, 3.33)) # --> None (out of bounds)
    

    注意:当提供的xval不在x值范围内时,函数返回None

    【讨论】:

      【解决方案2】:

      您可以像这样手动进行线性插值:

      def get_y_val(p):
          lower_i = max(i for (i, v) in enumerate(x) if v<= p)
          upper_i = min(i for (i, v) in enumerate(x) if v>= p)
          d = x[upper_i] - x[lower_i]
          if d == 0:
              return y[lower_i]
          y_pt = y[lower_i] * (x[upper_i] - p) / d+ y[upper_i] * (p - 
          x[lower_i]) / d
          return y_pt
      

      【讨论】:

      • 好点。我没有意识到默认情况下 pyplot 使用线性插值,曲线​​对我来说看起来很平滑。但是当我再次尝试使用越来越少的点时,我发现它是线性插值。
      猜你喜欢
      • 2017-10-11
      • 1970-01-01
      • 1970-01-01
      • 2020-05-19
      • 1970-01-01
      • 2019-08-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多