1

In Jupyter Notebook, I write the following code, as MCVE:

import matplotlib.pyplot as plt
import numpy as np

x1 = np.linspace(-1, 1, 50)
for x in range(4):
    plt.figure()
    plt.plot(x1)
    plt.savefig(str(x)+".png") # save figures separately

It produces 4 plots, as expected. They are below each other in a row.

I want to have a single saved output, containing all 4 plots. I don't mind if Jupyter Notebook does not show the plots as long as they are saved.

My attempt: save the figures separately, as 0.png, 1.png, 2.png, 3.png, then merge them vertically.

I hope there is an easier way to do this. How can I have a single saved output?

EDIT:

As suggested, I could do subplots, like this:

fig,axes = plt.subplots(nrows=4)
fig.set_size_inches(8,12)
x1 = np.linspace(-1, 1, 50)
for ax in axes:
    ax.plot(x1)
plt.savefig("save_fig.png")

I want to have many figures, not one fiugre with many subplots. How can I save the multiple figures without using subplots?

zabop
  • 6,750
  • 3
  • 39
  • 84
  • 3
    Create a figure with 4 subplots? – DavidG Aug 31 '18 at 14:59
  • Use `plt.subplots`. Read the basic documentation. – Denziloe Aug 31 '18 at 15:00
  • There are several duplicates of this question with different answers on creating subplots. I would suggest to mark it as duplicate. – Sheldore Aug 31 '18 at 15:04
  • yes, subplot method works. I wanted to have other method, but it does the job. – zabop Aug 31 '18 at 15:05
  • 1
    I dont think it is dublicate, because the subplot method basically creates one plot and than saves that, and I'd like to save many plots as one, so the Q is not answered by subplotting. – zabop Aug 31 '18 at 15:07
  • Once it is saved how can you tell the difference between multiple figures or multiple subplots? If subplots actually give you the same results it's a duplicate; if the results are different explain how in the question. – Sam Hartman Aug 31 '18 at 18:29

1 Answers1

1

initalize a figure and add a subplot for each loop. then save after your loops are finished!

Cut7er
  • 1,209
  • 9
  • 24
  • Yes, I like that method as a work-around, but does not really show how to sova multiple plots and not one big plot with many subplots. – zabop Sep 01 '18 at 06:12