Python 使用Tkinter对话框从多个.csv文件在一个图形中创建多个绘图

Python 使用Tkinter对话框从多个.csv文件在一个图形中创建多个绘图,python,pandas,dataframe,csv,matplotlib,Python,Pandas,Dataframe,Csv,Matplotlib,我有多个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'

我有多个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文件- 像这样,我必须在一个绘图中绘制、清理和绘制多条IV曲线

请帮忙解决这个问题

pandas.Series.explode用于将列表转换为单独的行 在循环外部设置图形 向循环内的图形添加线 在末尾显示绘图。 输出 测量信息相同,因为两个文件使用了相同的数据 测量:1 挥发性有机化合物=43.085883485888566 Isc=6.6041576712193075 Impp=6.3165 Vmpp=34.9897 辐照度=W/m²737.5629 功率=221.0123 测量:2 挥发性有机化合物=43.085883485888566 Isc=6.6041576712193075 Impp=6.3165 Vmpp=34.9897 辐照度=W/m²737.5629 功率=221.0123 情节 两行重叠,因为两个文件使用了相同的数据
这里的代码看起来有点不清楚,而且过长。您可以在一行中按顺序执行一些操作,并将行数减少一半。检查实际问题@Pubudu Sitinamaluwa存储在所需列中的数据是按行的。因此,我必须清理每个必需的列,并将其连接到单个数据帧中。请检查原始数据。比你好多了。我真的很感谢你的帮助。问题解决了。谢谢
files = ['test1.xlsx', 'test2.xlsx']  # some list of all files

# set up the plot figure
fig = plt.figure(figsize=(20, 10))
ax = fig.add_subplot()
ax.grid(True)
ax.set(xlabel="Voltage",
   ylabel="Current",
   title="IV Curve") # xlim=[min , max]
plt.ylim (0.1,10)
plt.xlim (0.1,50)

# iterate through files
for i, file in enumerate(files, 1):
    df = pd.read_excel(file)
    
    # columns that are stringified lists
    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'Measurement {i}')
    
    # print measurement information
    print(f'Measurement: {i}')
    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