0

I'm new to programming and currently stuck on this: I create 4 different plots using a for loop and I want to assign each plot to a different subplot. Basically, I want 4 plots in a 2x2 grid so I can compare my data. Anyone knows how I can achieve this? My approach was to create a list of subplots and then assign every plot to a subplot using a nested plot:

import matplotlib.pyplot as plt
import numpy as np

def load_file(filename):
    return np.loadtxt(filename, delimiter=',', usecols=(0, 1), unpack=True, skiprows=1)


fig = plt.figure()

ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)

ax_list=[ax1, ax2, ax3, ax4]

for i,filename in enumerate(file_list):
    for p in ax_list:
        x,y = load_file(filename)
        plt.plot(x,y,
        label=l, #I assign labels from a list beforehand, as well as colors
        color=c,
        linewidth=0.5,
        ls='-',
               )
        p.plot()

The problem is, all plots are assigned to only one subplot and I don't know how to correct this. I'd appreciate any help!

2
  • Use p.plot instead of plt.plot. Commented Nov 20, 2017 at 11:46
  • What is your file_list? Commented Nov 20, 2017 at 11:48

2 Answers 2

1

I guess what you want is to show different data on all 4 plots, hence use a single loop. Make sure to use the axes plotting method, not plt.plot as the latter would always plot in the last subplot.

import matplotlib.pyplot as plt
import numpy as np

def load_file(filename):
    return np.loadtxt(filename, delimiter=',', usecols=(0, 1), unpack=True, skiprows=1)

fig, ax_list = plt.subplots(2,2)

for i,(filename,ax) in enumerate(zip(file_list, ax_list.flatten())):
    x,y = load_file(filename)
    ax.plot(x,y, linewidth=0.5,ls='-' )
plt.show()
Sign up to request clarification or add additional context in comments.

Comments

0

You don't need to loop over filenames and plots, only need to select the next plot in the list.

for i, filename in enumerate(file_list):
    p = ax_list[i]:
    x,y = load_file(filename)
    p.plot(x, y,
        label=l, #I assign labels from a list beforehand, as well as colors
        color=c,
        linewidth=0.5,
        ls='-')

plt.plot()

You can also replace

fig = plt.figure()

ax1 = fig.add_subplot(221)
ax2 = fig.add_subplot(222)
ax3 = fig.add_subplot(223)
ax4 = fig.add_subplot(224)

ax_list=[ax1, ax2, ax3, ax4]

with just

fig, ax_list = plt.subplots(2, 2)
ax_list = ax_list.flatten()

To get a simple 2x2 grid.

4 Comments

Thanks a lot, I didn't know how to iterate through a list the way you did. This worked out just the way I wanted it to! But I wanted to ask, which part exactly would you replace with fig, ax_list = plt.subplots(2, 2) ?
I have edited my answer to show what I mean to replace, and added in the flatten() I was missing, as prompted by @ImportanceOfBeingErnest 's answer - subplots returns a 2d array and flatten just gives you a 1d list.
how would you add legends here in the first example ?
@MadhurYadav Just do p.legend() within the for loop.

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.