'Get a list of all figures plotted using Matplotlib in Jupyter Notebook
I am using Jupyter Notebook for data analysis, where I need to plot several figures one after the other, in seperate cells
for example, lets say I have 15 figures, as below:
fig1 = plt.figure(...)
plt.barh(...)
...
fig2 = plt.figure(...)
plt.subplot(1,2,1)
plt.barh(...)
plt.subplot(1,2,2)
plt.barh(...)
...
fig15 = plt.figure(...)
plt.barh(...)
How do I obtain a list of these figure names? like so:
[fig1, fig2, ...., fig15]
P.S: I have already tried fig_nums = plt.get_fignums() - which returns an empty list :(
Solution 1:[1]
plt.get_fignums() returns nothing since you're not creating figures, not directly at least. plt.barh() returns a container. If you want to keep track of your figures, use
fig1 = plt.figure()
ax1 = plt.barh(...)
...
Then plt.get_fignums() returns a list of figure numbers created with plt.figure().
If this is not what you want, the easiest way would be to store all fig# in a list
figures = []
fig1 = plt.barh(...)
figures.append(fig1)
...
``
Solution 2:[2]
I have also found this workaround that seems to help in jupyter:
fig_list = [f for f in globals() if 'Figure' in str(type(globals()[f]))]
print(fig_list)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Lucas |
| Solution 2 | Andre |
