【问题标题】:Is there a way to calculate the slope for each successive point in two data frames, store all the values for slope and then plot it?有没有办法计算两个数据帧中每个连续点的斜率,存储斜率的所有值然后绘制它?
【发布时间】:2017-03-08 19:30:56
【问题描述】:
df = pd.read_csv('data.csv')
v = df['Gate V']
i1 = df['Drain I.1']

     Drain V   Gate V       Drain I    
0       0.01    -5.00  3.270000e-14                
1       0.01    -4.85  1.740000e-14               
2       0.01    -4.70  2.620000e-14                
3       0.01    -4.55  6.270000e-14                
...     ...     ...    ...

我有一个大的 .csv 文件,除了包含更多数据外,它与上面类似。到目前为止,我的目标是为几个不同的Drain V 绘制Drain IGate V。我已经通过使用上面的v = ...i1 = ... 语句来实现这一点,然后简单地绘制i1vi2v 等等。

但是,现在我需要为每个点计算每个 Drain IGate V 的斜率并绘制图表。我最初的想法是使用for 循环来计算i1(和i2i3...)和/或v 系列中每个条目的斜率,如下所示:

for x in i1:
    slope1 = (i1[x+1] - i1[x]) / (v[x+1] - v[x])

理想情况下,我会得到点到点的斜率,并可以使用 matplotlib 绘制它。显然,那个 for 循环不起作用,但我不知道该怎么做。

【问题讨论】:

  • 使用diff和列的比例。

标签: python python-3.x pandas numpy matplotlib


【解决方案1】:

您在循环中计算的斜率只是两列中连续差异的比率:

deltas = df.diff().drop(0)
slope = deltas['Drain I'] / deltas['Gate V']

.drop(0) 将删除差异的第一行,这将是所有 NaN 以保留原始形状。

这是一个小例子:

df = pd.DataFrame({'Gate V': [-5.00, -4.85, -4.70, -4.55], 'Drain V': [0.01, 0.01, 0.01, 0.01], 'Drain I': [3.270000e-14, 1.740000e-14, 2.620000e-14, 6.270000e-14]})
deltas = df.diff().drop(0)
slope = deltas['Drain I'] / deltas['Gate V']

你现在有一个 slope 系列包含

1   -1.020000e-13
2    5.866667e-14
3    2.433333e-13
dtype: float64

基本情节可以通过slope.plot()获得:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-01
    • 2019-12-06
    • 2017-06-01
    • 1970-01-01
    • 2022-10-13
    • 2015-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多