Saving a set of figures in a loop

  • Thread starter mikeph
  • Start date
  • Tags
    Loop Set
In summary, to save a set of figures in a loop, you can use the plt.savefig() function in Python. This function allows you to specify the file name and format for each figure and automatically saves them in the specified directory. You can also save figures in different file formats and customize the file name for each figure by using a variable. To add a figure number or title, you can use the plt.figure() function before saving the figure. To save the figures in a specific order, you can use the sorted() function to sort them before saving.
  • #1
mikeph
1,235
18
Hi,

I have a loop which produces a series of figures, specified by the iteration number.

For example the loop, simplified, is:

for i = 1:10
[code generating X and Y]
figure(i)
plot(X,Y)
end

So running it produces 10 images. I'd like to save them automatically, for future comparison.

How would I do this?

I've tried:

saveas(i,sprintf('%s.jpg',num2str(i)))

to save figure 1 as 1.jpg, and so on, but it's just giving me a blank white image.

Thanks for any help.
 
Physics news on Phys.org
  • #2
maybe try:
Code:
print -djpeg filename
 

1. How can I save a set of figures in a loop?

To save a set of figures in a loop, you can use the plt.savefig() function in Python. This function allows you to specify the file name and format for each figure, and it will automatically save each figure in the specified directory.

2. Can I save figures in different file formats using a loop?

Yes, you can save figures in different file formats using a loop by specifying the format in the plt.savefig() function. For example, you can save one figure as a PNG file and another as a PDF file in the same loop.

3. How can I add a figure number or title to each saved figure in the loop?

To add a figure number or title to each saved figure in the loop, you can use the plt.figure() function before saving the figure. This will create a new figure and allow you to add a title or label to the figure before saving it.

4. Is it possible to customize the file name for each saved figure in the loop?

Yes, you can customize the file name for each saved figure in the loop by using a variable in the file name. For example, you can use fig_1.png for the first figure, fig_2.png for the second figure, and so on.

5. How can I save a set of figures in a specific order using a loop?

To save a set of figures in a specific order using a loop, you can use the sorted() function to sort the figures in a list or dictionary before saving them. This will ensure that the figures are saved in the desired order.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Beyond the Standard Models
Replies
6
Views
378
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top