2

Hoping to get some help please, I'm trying plot simulation data in separate subplots using pandas and matplotlib my code so far is:

import matplotlib.pylab as plt
import pandas as pd
fig, ax = plt.subplots(2, 3)
for i in range(2):
    for j in range(50, 101, 10):
        for e in range(3):
            Var=(700* j)/ 100
            Names1 = ['ig','M_GZ']
            Data1 = pd.read_csv('~/File/JTL_'+str(Var)+'/GZ.csv', names=Names1)
            ig = Data1['ig']
            M_GZ=Data1['M_GZ']
            MGZ = Data1[Data1.M_GZ != 0]
            ax[i, e].plot(MGZ['ig'][:4], MGZ['M_GZ'][:4], '--v', linewidth=1.75)
plt.tight_layout()
plt.show()

But the code gives me 6 duplicate copies of the same plot: enter image description here instead of each iteration of Var having its own plot, I've tried changing the loop and using different variations like:

fig = plt.figure()
for i in range(1, 7):
      ax = fig.add_subplot(2, 3, i)
            for j in range(50, 101, 10):                
                     Var=(700* j)/ 100
                     Names1 = ['ig','M_GZ']
                     Data1 = pd.read_csv('~/File/JTL_'+str(Var)+'/GZ.csv', names=Names1)
                     ig = Data1['ig']
                     M_GZ=Data1['M_GZ']
                     MGZ = Data1[Data1.M_GZ != 0]
                     ax.plot(MGZ['ig'][:4], MGZ['M_GZ'][:4], '--v', linewidth=1.75)
plt.tight_layout()
plt.show()

but that changes nothing I still get the same plot as above. Any help would be appreciated, I'm hoping that each subplot contains one set of data instead of all six

This is a Link to one of the Dataframes each subdirectory ~/File/JTL_'+str(Var)+'/ contains a copy of this file there are 6 in total

2
  • Can you show what Data1 (the data frame) looks like? Commented Feb 5, 2015 at 3:57
  • @user3645626 I've added a link to an example of how one of the Data frames would look like would that be enough or do you think all 6 would be more helpful? Commented Feb 5, 2015 at 11:19

1 Answer 1

2

The problem is in your loop

for i in range(2):  # Iterating rows of the plot
    for j in range(50, 101, 10): # Iterating your file names
        for e in range(3): # iterating the columns of the plot

The end result is that you iterate all the columns for each file name

For it two work, you should have only two nesting levels in your loop. Potential code (updated) :

import matplotlib.pylab as plt
import pandas as pd
fig, ax = plt.subplots(2, 3)
for row in range(2):
    for col in range(3):
        f_index = range(50, 101, 10)[row+1 * col]
        print row, col, f_index
        Var=(700* f_index)/ 100
        Names1 = ['ig','M_GZ']
        Data1 = pd.read_csv('~/File/JTL_'+str(Var)+'/GZ.csv', names=Names1)
        ig = Data1['ig']
        M_GZ=Data1['M_GZ']
        MGZ = Data1[Data1.M_GZ != 0]
        ax[row, col].plot(MGZ['ig'][:4], MGZ['M_GZ'][:4], '--v',linewidth=1.75)
plt.tight_layout()
plt.show()
Sign up to request clarification or add additional context in comments.

2 Comments

I tried your suggestion but I keep getting IndexError: index 3 is out of bounds for axis 1 with size 3 however when i change the line fig, ax to fig, ax = plt.subplots(2, 6) a plot is produced where the top row is exactly the same as the bottom row, when print row, col is added I get 0,0 0,1 0,2 0,3 instead the loop should be giving 0,0 0,1 0,2 1,0 1,1 1,2
haha, sorry, i read too fast, for some reason i thought there was only 3 files. Have a look at the updated code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.