4

Matplotlib plt.show(block=False) gives me a black window when plotting something that normally would work with plt.show(block=True). With my previous Ubuntu install I could always run plt.show(block=False) without any issues.

Here is a simple piece of code that gives me the black screen:

y = np.random.random(10)
x = np.linspace(1, len(y), len(y))
plt.plot(x, y)
plt.show(block=False)
time.sleep(5)

The graph is displayed properly when I use block=True, but gives a black window with the code above. This happens in both Python 3 console and IPython.

Extra information:

  • Fresh install of Ubuntu 20.04.4.
  • Python 3.8.10.
  • Matplotlib version 3.5.1.
  • Tested with backends: [QtAag, Qt5Aag, TkAgg], with the same results.

Any help would be much appreciated. I mostly use the block=False argument to view/analyze the data whilst zooming-in, etc., on the graph.

1 Answers1

3

Got the same, even when using plt.ion(), both on Ubuntu 20.04 and 18.04, with Python 3.8 and Matplotlib 3.5.0. I noticed that I encounter this issue only when running a Python script like:

 python myscript.py 

If I type direct commands to the same interpreter in the Python console, the plot is fine.

I also noticed that refreshing the axis with set_ydata, the OS sees the figure in a pending state and asks continuously to force quit or wait, which is annoying.

I managed to solve the issue by completely removing the plt.show() call. Here is an example:

import matplotlib
print(matplotlib.get_backend())
matplotlib.use('Qt5Agg')

from matplotlib import pyplot as plt import matplotlib.gridspec as gridspec import numpy as np

plt.ion() gs = gridspec.GridSpec(2, 1, height_ratios=[6, 1]) fig = plt.figure(figsize=(9, 6), facecolor='w') plt.pause(0.1) ax1 = fig.add_subplot(gs[0]) ax2 = fig.add_subplot(gs[1]) line, = ax1.plot(np.arange(1024), np.arange(1024), color='w') ax1.set_xlim(0, 400) ax1.set_xlabel("MHz") ax1.set_ylim(-100, 10) ax1.set_ylabel("dB") ax1.grid(True) ax1.set_title("Starting Measurements...") ax2.plot(range(100), color='w') ann1 = ax2.annotate("Foundamental Tone Frequency: - MHz", (1, 72), fontsize=12) ann2 = ax2.annotate("Foundamental Tone Power: - dBFS", (1, 27), fontsize=12) ax2.set_axis_off() fig.tight_layout() fig.canvas.draw() fig.canvas.flush_events()

And in the loop where you have to redraw the axis:

line.set_ydata(spectrum)
line.set_color('b')
ann1.set_text("Foundamental Tone Frequency: %3.1f MHz" % tone_freq)
plt.draw()
fig.canvas.draw()
fig.canvas.flush_events()
Andrea
  • 51