import pandas as pd import numpy as np import matplotlib.pyplot as plt file_name ='E:/liuyuan/ceshi/4cmH20_long_breaths.csv' names = ['Time', 'Flow'] data = pd.read_csv(file_name, names=names) data1 = list(data) len_zu = len(data1) len_yuan = len(data1[0]) x = list() y = list() for i in range(0,len_zu): x.append(float(data1[i][0])) y.append(float(data1[i][1])) start_index = 0 end_index = 100 fig, ax = plt.subplots() plt.xlim(0, 50) plt.ylim(0, 1) time = np.arange(start_index, end_index) flow = np.arange(start_index, end_index) ax.set_xlabel('Time(s)', fontsize=14) ax.set_ylabel('Flow(L/s)', fontsize=14) ax.set_title('Breath Waveform ', fontsize=14) flow = data['Flow'][start_index:end_index] ax.plot(time, flow) plt.show()
时间: 2024-03-06 09:51:49 浏览: 89
pandas,matplotlib,numpy详解
根据你提供的代码,可能是由于 `list(data)`返回的是 DataFrame 中的列名,而不是数据本身。因此,在 `for` 循环中,你试图访问列名的第一个字符,而不是实际数据中的第一个字符。
为了解决这个问题,你可以使用 `data.values` 来访问数据本身,而不是列名。请尝试以下代码:
```
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
file_name ='E:/liuyuan/ceshi/4cmH20_long_breaths.csv'
names = ['Time', 'Flow']
data = pd.read_csv(file_name, names=names)
x = list()
y = list()
for i in range(len(data)):
x.append(float(data.values[i][0]))
y.append(float(data.values[i][1]))
start_index = 0
end_index = 100
fig, ax = plt.subplots()
plt.xlim(0, 50)
plt.ylim(0, 1)
time = np.arange(start_index, end_index)
flow = np.arange(start_index, end_index)
ax.set_xlabel('Time(s)', fontsize=14)
ax.set_ylabel('Flow(L/s)', fontsize=14)
ax.set_title('Breath Waveform', fontsize=14)
flow = data['Flow'][start_index:end_index]
ax.plot(time, flow)
plt.show()
```
希望这可以帮助到你!
阅读全文