Reputation: 612
Say I have a figure which I am able to generate using plt.show
. Now I wish to pass off this plt
object to a generic function like this -
generate_figs(plt):
frmt = ['jpg','png']
for i in frmt:
plt.savefig('name.{}'.format(i))
generate_figs(plt)
Can something like this be done?
Upvotes: 2
Views: 1419
Reputation: 25362
This depends on how many figures you have. If it is only one figure you can simply keep the function (mostly) as it is as plt.savefig
will save the current figure.
def generate_figs():
frmt = ['jpg','png']
for i in frmt:
plt.savefig('name.{}'.format(i))
If you have multiple figures, then you can pass the specific figure object into the function and use fig.savefig
import matplotlob.pyplot as plt
def generate_figs(fig):
frmt = ['jpg','png']
for i in frmt:
fig.savefig('name.{}'.format(i))
fig1 = plt.figure()
plt.plot(some_data)
fig2 = plt.figure()
plt.plot(some_other_data)
generate_figs(fig1)
plt.show()
Note: you must save the figure before any call to plt.show()
Upvotes: 3