Commit f39153db authored by Gordian Edenhofer's avatar Gordian Edenhofer
Browse files

Allow for non-blocking plots

Pass the `block` keyword on to `matplotlib.pyplot` as to allow for
non-blocking interactive plot windows to be created. This is useful for
creating several plots in fast succession without the python script
blocking the current process.
parent 2e63dd00
Pipeline #62615 passed with stages
in 7 minutes and 33 seconds
...@@ -171,14 +171,15 @@ def _find_closest(A, target): ...@@ -171,14 +171,15 @@ def _find_closest(A, target):
return idx return idx
def _makeplot(name): def _makeplot(name, block=True):
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
if dobj.rank != 0: if dobj.rank != 0:
plt.close() plt.close()
return return
if name is None: if name is None:
plt.show() plt.show(block=block)
plt.close() if block:
plt.close()
return return
extension = os.path.splitext(name)[1] extension = os.path.splitext(name)[1]
if extension in (".pdf", ".png", ".svg"): if extension in (".pdf", ".png", ".svg"):
...@@ -496,6 +497,9 @@ class Plot(object): ...@@ -496,6 +497,9 @@ class Plot(object):
If left empty, the plot will be shown on the screen, If left empty, the plot will be shown on the screen,
otherwise it will be written to a file with the given name. otherwise it will be written to a file with the given name.
Supported extensions: .png and .pdf. Default: None. Supported extensions: .png and .pdf. Default: None.
block: bool
Override the blocking behavior of the non-interactive plotting
mode. The plot will not be closed in this case but is left open!
""" """
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
nplot = len(self._plots) nplot = len(self._plots)
...@@ -522,4 +526,4 @@ class Plot(object): ...@@ -522,4 +526,4 @@ class Plot(object):
ax = fig.add_subplot(ny, nx, i+1) ax = fig.add_subplot(ny, nx, i+1)
_plot(self._plots[i], ax, **self._kwargs[i]) _plot(self._plots[i], ax, **self._kwargs[i])
fig.tight_layout() fig.tight_layout()
_makeplot(kwargs.pop("name", None)) _makeplot(kwargs.pop("name", None), block=kwargs.pop("block", True))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment