-1

I am trying to use subplots to plot 12x2(rowsxcolumns). However, my code seems to be plotting all the 12 graphs on the same figure 12 times. I have attached my code below, if someone can help me understand where I am going wrong, it would really help me. Note : I am reading the data from two different folders. The first column corresponds to the plots from the first file of the folder and the second column to the second file. I want to plot 1st data from 1st file and 2nd file in positions [0,0] and [0,1] respectively. I want to repeat this for all 12 files. enter image description here

fig, axs = plt.subplots(12, 2,figsize=(20,80))
for file in files: # reading data from 1st file
        with open(file,"r") as f_in: 
            reader=csv.reader(f_in)
            next(reader)
            for line in reader:
                try:
                    float_1,float_2=float(line[0]),float(line[1])
                    xData.append(float_1)
                    yData.append(float_2)
                except ValueError:
                    continue                     
        for i in range(12):
            axs[i,0].plot(xData,yData)
        xData=[]
        yData=[]                            
for file in files1:
    with open(file,"r") as f_in: 
        reader=csv.reader(f_in)
        next(reader)
        for line in reader:
            try:
                float_1,float_2=float(line[0]),float(line[1])
                xData1.append(float_1)
                yData1.append(float_2)
            except ValueError:
                continue
    for i in range(12):
        axs[i, 1].plot(xData1, yData1)            
    xData1=[]
    yData1=[]
Angela Mary
  • 49
  • 1
  • 8
  • There is nothing incorrect about the subplot addressing itself. However, the code appears to be poorly organized. For example, `xData=[]` is after `xData.append(float_1)`. Given the OP, this shouldn't even run, so something is missing. There's not sample data to test. Please see [How to ask a good question](https://stackoverflow.com/help/how-to-ask). Always provide a complete [mre] with **code, data, errors, current output, and expected output**, as **[formatted text](https://stackoverflow.com/help/formatting)**. If relevant, only plot images are okay. – Trenton McKinney Aug 31 '21 at 18:01

1 Answers1

0

In your code you are plotting on all plots for each file axs[i,0].plot(xData,yData) and axs[i,1].plot(xData1,yData1) as you have a loop (for i in range(12)) within a loop (for file in files1).

Here is a quick fix of this error in your code:

fig, axs = plt.subplots(12, 2,figsize=(20,80))
i = 0
for file in files: # reading data from 1st file
        with open(file,"r") as f_in: 
            reader=csv.reader(f_in)
            next(reader)
            for line in reader:
                try:
                    float_1,float_2=float(line[0]),float(line[1])
                    xData.append(float_1)
                    yData.append(float_2)
                except ValueError:
                    continue                     
        
        axs[i,0].plot(xData,yData)
        i+=1
        xData=[]
        yData=[]
i = 0
for file in files1:
    with open(file,"r") as f_in: 
        reader=csv.reader(f_in)
        next(reader)
        for line in reader:
            try:
                float_1,float_2=float(line[0]),float(line[1])
                xData1.append(float_1)
                yData1.append(float_2)
            except ValueError:
                continue
    axs[i, 1].plot(xData1, yData1)  
    i+=1
    xData1=[]
    yData1=[]

NB. of course the number of files should not exceed 12, or you will have an IndexError

NB.2 there are much better ways to organize the code, the ideal might be to create a function that takes the file and an axes object as argument and plots the file's data on this axis. Then you can iterate over the axes.

mozway
  • 194,879
  • 13
  • 39
  • 75