user3191569
user3191569

Reputation: 505

Plot panda series in separate subplots using matplotlib

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

Upvotes: 2

Views: 1247

Answers (1)

knightofni
knightofni

Reputation: 1956

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()

Upvotes: 2

Related Questions