1

Using Jupyter, i'm facing this issue:

I declare several matplotlib figures in cell 1 with an identifier using num attribute
I want to get the figures back in cell 2 but it seems to be erased even when using the identifier.

Cell n°1

for i in range(2):
    plt.figure(i, figsize=(15,5))
    plt.plot([0,1], [0,1])
print(plt.get_fignums())
#Output
[0, 1]

Cell n°2

print(plt.get_fignums())
#Output
[]

Can someone explain this behaviour and how to recall my figure from cell n°1 ?

Clément
  • 1,128
  • 7
  • 21
  • what happens when you delete the `fig = plt.figure(1)` in cell 2? do you get an error or does it print the first figure? – Finn Apr 21 '22 at 12:58
  • It works, but If I have 2 figures declared in cell 1, I would like to access from cell 2 the first figure of cell 1. And I would like to do it not using variable (only identifier), but maybe this is where I lost my mind. In my real case, I instance several figures in a loop so they are not stored in variable. – Clément Apr 21 '22 at 13:05
  • I don't know why the `plt.figure(1)` doens't work so i cant help you there, but if this is an option for you: Create the figures individual names for the handles `fig1, `fig2` or store them in list, so you have all of them and can avoid to recall a previous handle – Finn Apr 21 '22 at 13:12
  • I've edited my exemple so the issue behaviour is more explicit. – Clément Apr 21 '22 at 13:13
  • Yes, but that is still not a minimal reproducible example. For example, I would just collect each plot in a list or dictionary as I made it so that I can recall any one of them using the `.figure` attribute of the plot later myself. However, I cannot tell if that is compatible with what you have. Similarly, I cannot test with what you have provided if the print is consuming what `plt.get_fignums()` returns (it shouldn't since supposed to return a list) or your backend is closing your figures outside of that cell, as discussed [here](https://stackoverflow.com/a/45766059/8508004). – Wayne Apr 21 '22 at 17:32

0 Answers0