1

I'm using Canopy in a Win7 machine, with %pylab enabled and interactive(Qt4) as backend. IMHO, I'm getting what I consider a weird behavior of matplotlib.

If the code is executed line by line, the frames for the graphs appear as I expect, but not the content of the graphs themselves. If, after the plotting, I need information regarding to those graphs, as I can't see them, I can't answer properly. Once I answer the question with a dummy answer, the graphs appear.

What I would like to achieve is that the graphs show before the question would be asked, in order to have the information to reply.

Thanks in advance.

This is a MWE

import numpy as np
import matplotlib.pyplot as plt

N = 8
y = np.zeros(N)
x1 = np.linspace(0, 10, N, endpoint=True)
x2 = np.linspace(0, 10, N, endpoint=False)
plt.figure()
plt.plot(x1, y, 'o')
plt.plot(x2, y + 0.5, 'o')
plt.ylim([-0.5, 1])
plt.show()


y1 = np.random.random(8)
plt.figure()
plt.plot(x1, y1)
plt.show()

dummy = raw_input("What is the third point in the second graph?")

EDIT: If I change the backend in Canopy from interactive(Qt4) to interactive(wx), it works as expected.

Tim D
  • 1,645
  • 1
  • 25
  • 46
Maxwell's Daemon
  • 587
  • 1
  • 6
  • 21

2 Answers2

1

If I understand, the problem is that plt.show will block and the second figure will not be plotted until the first is closed. With different backends the behaviour may be different but you shouldn't call show more than once (see matplotlib show() doesn't work twice). I'd suggest using two subplots here and maybe turning blocking off as raw_input block and you can enter an input with the figure showing. You code would then look like this,

import numpy as np
import matplotlib.pyplot as plt

N = 8
y = np.zeros(N)
x1 = np.linspace(0, 10, N, endpoint=True)
x2 = np.linspace(0, 10, N, endpoint=False)
fig,ax = plt.subplots(2,1)
ax[0].plot(x1, y, 'o')
ax[0].plot(x2, y + 0.5, 'o')
ax[0].set_ylim([-0.5, 1])

y1 = np.random.random(8)
ax[1].plot(x1, y1)
plt.show(block=False)

dummy = raw_input("What is the third point in the second graph?")
print("dummy = ", dummy)

which works for me.

Community
  • 1
  • 1
Ed Smith
  • 12,716
  • 2
  • 43
  • 55
  • Thanks for your answer. Unfortunately, it didn't work neither in my case. On the other hand, if I changed the backend in Canopy from interactive(Qt4) to interactive(wx), your code and my code, both works! – Maxwell's Daemon Aug 18 '16 at 09:41
1

As Ed Smith said, only the first call to plt.show() works. If you want to force a redraw of the figure, you can use figure.canvas.draw()

import matplotlib.pyplot as plt

fig = plt.figure()
plt.plot([0, 1], [2, 3])
plt.show()

#do more stuff, get user input

plt.plot([5,6], [-7, -8])
fig.canvas.draw()

enter image description here

Tim D
  • 1,645
  • 1
  • 25
  • 46