【问题标题】:How to display slope of a section of a line如何显示线段的斜率
【发布时间】:2019-10-22 03:07:12
【问题描述】:

基本上我有一个数据集,我只想找到我绘制的线段的斜率。我看到的每个答案都只解释了如何创建最适合的一般线,但这与我的需要无关。

我在 python 3 中使用 Jupyter Notebook,带有 pandas、matplotlib.pyplot、numpy,并从 scipy.stats 导入了 linregress(根据另一篇解释如何计算斜率的帖子的说明)。

这是我使用定义为 df 和 df2 的两个数据框绘制的图。

plt.figure(figsize=(20,10))
line1 = plt.plot(df['Time (s)'],df['Oxygen (μmol/L)'], 'b-', label='1mM Ru(Eto)(bpy) with 20eq Ce')
line2 = plt.plot(df2['Time (s)'],df2['Oxygen (μmol/L)'], 'r-', label='1mM Ru(tpy)(bpyCO)PF_6 with 20eq Ce')
plt.legend()

My Plot

基本上我想在线条不完全平坦的地方显示最佳拟合线的斜率。

【问题讨论】:

    标签: python-3.x pandas matplotlib jupyter-notebook


    【解决方案1】:

    尝试使用一些阈值来获取不平坦的数组子集。类似于

    idx = np.array([])
    
    for i in range(len(array)):
    if np.abs(array[i] - array[i+1]) > some_threshold:
       idx = np.append(idx,i)
    
    new_array = array[idx[0]:idx[-1]]
    

    那你可以在new_array上做一条最合适的线

    (无论如何,这都不是“最干净”的方法,但它是一个提供总体思路的草图)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-15
      • 1970-01-01
      • 2021-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-20
      相关资源
      最近更新 更多