【问题标题】:To create multiple plots in one single graph from multiple .csv files using Tkinter dialogue box [closed]使用 Tkinter 对话框从多个 .csv 文件在一个图形中创建多个图 [关闭]
【发布时间】: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


【解决方案1】:
  • pandas.Series.explode 用于将列表转换为单独的行
  • 在循环外设置图形
  • 向循环内的图形添加线条
  • 在最后显示情节。
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

输出

  • 测量信息相同,因为两个文件使用了相同的数据
Measurement: 1
Voc = 43.085883485888566
Isc = 6.6041576712193075
Impp = 6.3165
Vmpp = 34.9897
Irradiance = W/m² 737.5629
Power= 221.0123


Measurement: 2
Voc = 43.085883485888566
Isc = 6.6041576712193075
Impp = 6.3165
Vmpp = 34.9897
Irradiance = W/m² 737.5629
Power= 221.0123

情节

  • 这两行重叠,因为两个文件使用了相同的数据

【讨论】:

  • 非常感谢。非常感谢您的帮助。
  • 问题解决了。谢谢。
猜你喜欢
  • 2018-03-20
  • 2017-07-20
  • 2020-06-21
  • 1970-01-01
  • 1970-01-01
  • 2017-05-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
相关资源
最近更新 更多