How can I assign each .csv file to a single subplot?

Viewed 169

This is my code so far - what I am trying to achieve:

I have 3 .csv files (i.e. replicas) with two columns each: time (X-Axis) and potential energy (Y-Axis) Since they have similar paths, I am reading them through filename = with %s for each replica and wanna plot each in one of the subplots so that I can then save a single fig with all three graphs.

My output gives me either all three replicas in all three subplots or only the third one in each. I am not capable of assigning each .csv file to a seperate subplot in the for loop.

Thanks for helping

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

replicas = ['1', '2', '3']

fig, axs = plt.subplots(nrows=3, ncols=1,sharex=True, sharey=True, figsize=(10,7))

for i in range(3): 
    for replica in replicas:
        filename = "elliottIV_HA_IHSS/box4x4x4/bbs60/bbpm2/NA/pH7.0/r%s/analysis/ene_ana/eq/totpot.dat" % replica
        data=pd.read_csv(filename, engine='python', delimiter="\s+", names=["time", "totpot"], usecols=[0, 1], comment="#")
        data.plot(x='time', y='totpot', ax=axs[i], legend=False)
        axs[i].set_title("60bbs: eq - rep{}".format(replica))
plt.tight_layout()
1 Answers

Replace the two for loops:

for i in range(3): 
    for replica in replicas:

with a loop over enumerate:

for i, replica in enumerate(replicas):
Related