-3

I have multiple csv files. I have to plot the current vs voltage plots in one single graph from the data obtained after cleaning arranging the data frame from multiple csv files.

The code for plotting the single graph is as follows,

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



    

This is an example csv file - https://1drv.ms/u/s!Au0g20aAwHJyhRcHzAVswst8WD-V?e=C5GSAb Like this, I have to pull, clean and plot multiple IV curve in one single plot.

Kindly help to solve this problem

hebin manuel
  • 47
  • 1
  • 7
  • Your code here looks a bit unclear and unnecessarily long. There are some operations you can do sequentially in one line and reduce the number of lines probably by half. Check this on the actual problem. https://stackoverflow.com/questions/20906474/import-multiple-csv-files-into-pandas-and-concatenate-into-one-dataframe – Pubudu Sitinamaluwa Aug 31 '20 at 14:14
  • @Pubudu Sitinamaluwa The data stored in the required column is in row-wise. Hence I had to clean each required column and concatenate into a single data frame. Please check the raw data. – hebin manuel Aug 31 '20 at 14:19

1 Answers1

1
  • pandas.Series.explode is used to convert the lists to individual rows
  • Set up the figure outside of the loop
  • Add lines to the figure inside the loop
  • Show the plot at the end.
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

Output

  • The measurement information is the same, because the same data was used for both files
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

Plot

  • The lines overlap, because the same data was used for both files

enter image description here

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158