pyplot.figure(fig.number) messed up in PyCharm

Answered

Running the following script in PyCharm

import matplotlib.pyplot as plt
import numpy as np
import time

fig = plt.figure()
print(fig.number)
print(plt.gcf().number)
ax = plt.imshow(np.random.randint(0, 10, (120,)).reshape((10, 12)))
fig.show()
time.sleep(2)

fig2 = plt.figure()
print(fig2.number)
print(plt.gcf().number)
ax = plt.imshow(np.random.randint(0, 10, (120,)).reshape((10, 12)))
plt.show()
time.sleep(2)

plt.figure(fig.number)
print(fig.number)
print(plt.gcf().number)
ax = plt.imshow(np.random.randint(0, 10, (120,)).reshape((10, 12)))
plt.show()
time.sleep(2)

I get that 3 figures are produced in "SciView > Plots", instead of 2 (the first one should be re-drawn). Also, in the Console only the number 1 is printed (six times) whilethe lines

print(fig2.number)
print(plt.gcf().number)

should both return 2.

Also, re-running the same script actually re-draw some of the figures (randomly?), for instance I get 4 figures in total. Can anyone tell me why? I am interested in re-drawing the same figure, i.e. making it the "current" figure with

plt.figure(fig.number) 

and then drawn on it.

Please note that running the same thing in Jupyter do not give any problem (for instance I get the correct print 1 1 2 2 1 1).

 

0
1 comment

Apparently, PyCharm's matplotlib backend and hence SciView can't handle the situation when you redraw the plot.

I guess it's a part of https://youtrack.jetbrains.com/issue/PY-31615, feel free to vote and leave comments.

0

Please sign in to leave a comment.