0

I have to load 3 numpy files using for loop. After loading data I want to plot 3 subplots because I want to make a comparison between them. If I don't apply for loop and load files one by one then I can easily do that. But is there any way that I load data in for loop and then make 3 subplots.

fig = plt.figure()
ax1 = fig.add_subplot(311)
ax2 = fig.add_subplot(312)
ax3 = fig.add_subplot(313)
ind1 =[1,2,3]
ind2 = [4,5,6]
for i in range(len(3)):
    data1=np.load(..)
    data2=np.load(..)
    ax1.plot(data1, data2)
DavidG
  • 24,279
  • 14
  • 89
  • 82
herry
  • 155
  • 10

1 Answers1

2

Without knowing what your file contains and how they look, here is the basic idea which you can adapt to your example. Here, I store the names of the files you want to read in a list and then I loop over the subplots one at a time. I use enumerate to get the axis instance and the index so that I can access/loaf files one at a time. P.S: I used np.loadtxt just as an example. You can basically replace the commands with whatever way you want to load files.

Instead of axes.ravel(), you can also use either axes.flatten() or axes.flat.

fig, axes = plt.subplots(nrows=3, ncols=1)
files = ['file1.txt', 'file2.txt', 'file3.txt']

for i, ax in enumerate(axes.ravel()): # or axes.flatten() or axes.flat
    data1=np.loadtxt(files[i])
    data2=np.loadtxt(files[i])
    ax.plot(data1, data2)
plt.tight_layout() # To get a better spacing between the subplots

Alternative solution without using ravel and enumerate as suggested by Tom de Geus is:

for i in range(3):
    axes[i].plot(x, y, label='File %d' %i)
    axes[i].legend()

Following is a simple example to showcase the idea (Same can be done using the above alternative solution)

fig, axes = plt.subplots(nrows=3, ncols=1)
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)

for i, ax in enumerate(axes.ravel()): # or axes.flatten() or axes.flat
    ax.plot(x, y, label='File %d' %i)
    ax.legend()
fig.text(0.5, 0.01, 'X-label', ha='center')
fig.text(0.01, 0.5, 'Y-label', va='center', rotation='vertical')  
plt.tight_layout() # To get a better spacing between the subplots

enter image description here

Sheldore
  • 37,862
  • 7
  • 57
  • 71
  • 1
    Since you use an index anyway, it could be more transparent to also use it on the axes. Also you could avoid the `ravel` by not specifying `ncols` at all. (All a matter of taste of course.) – Tom de Geus Oct 05 '18 at 16:17
  • @TomdeGeus: I added your suggestion. Thanks – Sheldore Oct 05 '18 at 16:21
  • What if I want to set xlabel and y label only once rather than on every subplot – herry Oct 05 '18 at 16:36
  • @herry: Have a look at the top most answer of [this](https://stackoverflow.com/questions/16150819/common-xlabel-ylabel-for-matplotlib-subplots). Let me know if that doesn't work for you. It's just a two line thing – Sheldore Oct 05 '18 at 16:38
  • @Bazingaa why in the fig.text command we don't mention (0.5,0). Instead of this you mentioned (0.5, 0.1). According to my understanding these are values of x and y axis. 0.5 indicate that center of the x-axis. So for y-axis value shouldn't it be 0 rather than 0.1 – herry Oct 06 '18 at 09:20
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/181393/discussion-between-bazingaa-and-herry). – Sheldore Oct 06 '18 at 09:50
  • You can also try that – Sheldore Oct 06 '18 at 09:50