我正在使用matplotlib和numpy,并且我正在制作图表。我使用的数据格式是.csv。在我使用的csv文件中,有三列。我想知道,有没有办法只导入数据,直到我的某一列的峰值/最低值?
背景:我正在使用带有脂质单层的Langmuir槽,并压缩和扩展屏障来增加/减少面积,我试图绘制该区域的压力和荧光。然而,获取这些数据的程序执行了一个完整的压缩和扩展周期,我不能简单地在低谷处于最小区域时停止数据收集。所以我想让Python只导入,直到面积值达到最低点。
example of how my data looks Area | Presure | Intensity 12500 |3 | 1 11500 |6 | 12 etc |8 |25 3000 |12 |38 3500 |19 |54 <==want it to stop importing here 4500 |16 |47
这个是可能的吗??
我已经添加了Phi放入的内容,但它似乎不起作用?我仍然得到了包含在我的图中的所有值,代码看起来像这个import matplotlib.pyplot as plt import numpy as np import pandas pd
df = pd.read_csv("C:\\Users\\Owner\\Desktop\\Thunberg,Dametre\\5-29 Data and
movies\\New folder (2)\\Data 2.csv", sep=',')
rowmin = df.area.idxmax()
df[:(1 + rowmin)]
fig, ax1 = plt.subplots()
area, pressure, pixel = np.loadtxt
("C:\\Users\\Owner\\Desktop\\Thunberg,Dametre\\5-29 Data and movies\\New
folder
(2)\\Data 2.csv", delimiter=",", skiprows=1, unpack=True)
plt.plot(area,pressure, label='area/pressure!',color='b')
plt.xlabel('area', color='b')
plt.ylabel('Pressure', color='b')
ax1.tick_params('y', colors='b')
ax2 = ax1.twinx()
this ax2 creates a second x axis
ax2.set_ylabel('Intensity (measured by average pixel value)', color='r')
this labels the secondary axis and chooses its color
ax2.tick_params('y', colors='r')
this Chooses the color of the ticks in the axis
ax2.plot(area,pixel, color='r')
this is what actually plots the second graph of area vs intensity
plt.title('Kibron Trough Pressure-Area-Intensity Graph')
plt.legend()
plt.show()发布于 2018-06-01 05:53:33
在读取整个文件之前,您不能确定哪个值最高。更简单的解决方案是读取整个文件,然后删除行。
import pandas as pd
df = pd.read_csv('yourfile.csv', sep=',')
rowmax = df.Intensity.idxmax()
df[:(1 + rowmax)]发布于 2018-06-01 08:36:52
我的理解是,文件正在及时更改,因此您希望能够检查是否检测到最小值。如果你观察文件的变化,我认为你可以做到这一点。下面我提供了最简单的方法,但您可以通过添加一些暂停来“加强”它。
import os
import numpy as np
stat_prev = os.stat(fname)
while True:
data = np.genfromtxt(fname, dtype=np.int, delimiter=',', names=True)
min_idx = np.argmin(data['Area'])
if min_idx < len(data) - 1 and data['Area'][min_idx] < data['Area'][min_idx+1]:
data = data[:min_idx + 1] # <-- remove +1 if min row is the last one
break # exit main loop;
# wait for the file to change
stat_now = os.stat(fname)
while stat_prev == stat_now: # add some time-out, if you want
stat_prev = os.stat(fname)此外,如果不需要结构化数组而只需要简单数组,则可以使用this recipe将data转换为简单数组
data.view(data.dtype[0]).reshape(data.shape + (-1,))https://stackoverflow.com/questions/50633132
复制相似问题