【发布时间】:2020-12-19 15:56:28
【问题描述】:
我有多个 csv 文件。我必须根据从多个 csv 文件中清理排列数据帧后获得的数据,在一张图中绘制电流与电压的关系图。
绘制单图的代码如下,
import pandas as pd
import tkinter as tk
import matplotlib.ticker as ticker
from tkinter import filedialog
import matplotlib.pyplot as plt
root = tk.Tk()
root.withdraw()
root.call('wm', 'attributes', '.', '-topmost', True)
files = filedialog.askopenfilename(multiple=True)
%gui tk
var = root.tk.splitlist(files)
files = []
fig = plt.figure(figsize=(30, 20))
ax = fig.add_subplot()
ax.grid(True)
#ax.set(xlabel="Voltage", ylabel="Current", title="IV Curve")
ax.set_xlabel("Voltage [V]",fontsize = 20)
ax.set_ylabel("Current [I]",fontsize = 20)
ax.set_title("IV Curve Plot",fontweight ='bold', fontsize = 30)
ax.tick_params(axis='both', which='major', labelsize=20)
plt.ylim (0.1,10) #adjust the voltage limits
plt.xlim (0.1,50) #adjust the current limits
plt.savefig('/home/hebin/Desktop/PV/Mitsui/hotspot/IV.png', dpi=100)
for i,file in enumerate(files,1):
df = pd.read_csv(file, index_col=None, header=0, encoding='ISO-8859–1')
cols_of_lists = ['RawCurrent', 'RawVoltage', 'RawPower', 'CorrectedCurrent', 'CorrectedVoltage', 'CorrectedPower']
# convert these columns from strings to lists
df[cols_of_lists] = df[cols_of_lists].apply(lambda x: x.str.replace('[', '').str.replace(']', '').str.split(','))
# get CorrectedVoltage and CorrectedPower
cv = df[cols_of_lists[3:5]].apply(pd.Series.explode).astype(float).reset_index(drop=True)
# add line to figure
ax.plot('CorrectedVoltage', 'CorrectedCurrent', data=cv, linewidth=2, label =f'IV: {i}')
# print measurement information
print(f'IV: {file}')
voltage = cv["CorrectedVoltage"].values.max()
current = cv["CorrectedCurrent"].values.max()
Power = df["Pmax"].values.max()
print("Voc =", voltage)
print("Isc =", current)
print('Impp =', df['I MPP'].values.max())
print('Vmpp =', df['V MPP'].values.max())
print('Irradiance = W/m²', df['Irradiance W/m²'].values.max())
print('Power=', Power)
print('\n')
plt.legend() # add the legend
plt.show() # show the plot
这是一个示例 csv 文件 - https://1drv.ms/u/s!Au0g20aAwHJyhRcHzAVswst8WD-V?e=C5GSAb 像这样,我必须在一个图中提取、清理和绘制多条 IV 曲线。
请帮忙解决这个问题
【问题讨论】:
-
您的代码看起来有点不清楚而且不必要地冗长。您可以在一行中按顺序执行一些操作,并可能将行数减少一半。检查实际问题。 stackoverflow.com/questions/20906474/…
-
@Pubudu Sitinamaluwa 存储在所需列中的数据是按行排列的。因此,我必须清理每个所需的列并连接到一个数据框中。请检查原始数据。
标签: python pandas dataframe csv matplotlib