0

I'm doing cyclic voltammetry and receives data for each cycle in a file. I open and read every file in a directory and put the data in a list with a list of a each cycle for a specific variable. This was not a problem and works fine. (I added the code in case someone found it useful)

My issues is I would like to plot each cycle on the same graph but smartly with a loop instead of writing every plot manually as I have done. Do anyone have a good idea? I have tried different things but I haven't even been able to plot in a loop.

In Python

import re
import matplotlib.pyplot as plt
import os


def CreateData(Directory):
        assert os.path.isdir(Directory), "Not a directory"                # Test whether argument given is an actual directory
        cycleE, cycleI = [], []                                           # empty list
        directory = os.fsencode(Directory)                                # encode pathlike filename (define directory)
        for file in os.listdir(directory):                                # Loop through directory
                filename = os.fsdecode(file)                              # Decode pathlike filename (Define filename)
                filename = Directory + '\\' +filename                     # Make sure filename is pathlike
                E, I = [], []                                             # Create empty E and I for each cycle file
                with open(filename, 'r') as f:                            # open file
                        for _ in range(2):                                # Loop over first two lines
                                next(f)                                   # Remove lines looped
                        for line in f:                                    # Loop through file  
                                f = re.findall(r'(\S+)', line)            # Create list ['E', 'I']
                                E.append(float(f[0]))                     # Isolate E in list
                                I.append(float(f[1])*1000000)             # Isolate I in list
                        cycleE.append(E)                                  # Create list cycle [E1, E2, E3, ..]
                        cycleI.append(I)                                  # Create list cycle [I1, I2, I3, ..]
        return [cycleE, cycleI]                                           # return List of each cycle list


data = CreateData('Pt(15 15 14)')

fig, ax = plt.subplots(1)

ax.plot(data[0][0], data[1][0], 'r-', linewidth=.2, label='Cycle 1')
ax.plot(data[0][1], data[1][1], 'k-', linewidth=.6, label='Cycle 2-4')
ax.plot(data[0][2], data[1][2], 'k-', linewidth=.4)
ax.plot(data[0][3], data[1][3], 'k-', linewidth=.2)
ax.plot(data[0][4], data[1][4], 'b-', linewidth=.8, label='Cycle 5-9')
ax.plot(data[0][5], data[1][5], 'b-', linewidth=.6)
ax.plot(data[0][6], data[1][6], 'b-', linewidth=.4)
ax.plot(data[0][7], data[1][7], 'b-', linewidth=.2)
ax.plot(data[0][8], data[1][8], 'c-', linewidth=.8, label='Cycle 10-13')
ax.plot(data[0][9], data[1][9], 'c-', linewidth=.6)
ax.plot(data[0][10], data[1][10], 'c-', linewidth=.4)

ax.legend()

ax.set_ylabel('i / \u03BCA ', fontsize=15)
ax.set_xlabel('E vs RHE/ V', fontsize=15)

plt.show()

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jon Bjarke
  • 11
  • 2

0 Answers0