1

I manage to save several figures created by generic class. Is there a way of omitting figure creation and closing every time I want to save a figure to make it shorter or that it would work faster if there were many plots? Here is the example code:

import numpy as np


class plotting():
    def plot1(self,x,y,fig):
        ax = fig.add_subplot()
        ax.plot(x,y)

    def plot2(self,x,y,fig):
        ax = fig.add_subplot()
        ax.scatter(x, y)

x = np.linspace(0, 10, 10)
y = np.linspace(20, 30, 10)
x1 = np.linspace(20, 10, 10)
y1 = np.linspace(60, 30, 10)

plotting = plotting()
f = plt.figure()
plotting.plot1(x = x,y= y, fig= f)
f.savefig('plot2', bbox_inches='tight')
plt.close(f)
f = plt.figure()
plotting.plot2(x = x1,y= y1, fig= f)
f.savefig('plot3', bbox_inches='tight')
plt.close(f)
Jerzy
  • 155
  • 1
  • 8
  • 1
    See https://stackoverflow.com/questions/9622163/save-plot-to-image-file-instead-of-displaying-it-using-matplotlib – JohanC Dec 09 '21 at 11:57

0 Answers0