diff --git a/lib/matplotlib/backends/web_backend/js/mpl.js b/lib/matplotlib/backends/web_backend/js/mpl.js index 140f5903852e..6e8ec449d92b 100644 --- a/lib/matplotlib/backends/web_backend/js/mpl.js +++ b/lib/matplotlib/backends/web_backend/js/mpl.js @@ -192,6 +192,15 @@ mpl.figure.prototype._init_canvas = function () { } this.resizeObserverInstance = new this.ResizeObserver(function (entries) { + // There's no need to resize if the WebSocket is not connected: + // - If it is still connecting, then we will get an initial resize from + // Python once it connects. + // - If it has disconnected, then resizing will clear the canvas and + // never get anything back to refill it, so better to not resize and + // keep something visible. + if (fig.ws.readyState != 1) { + return; + } var nentries = entries.length; for (var i = 0; i < nentries; i++) { var entry = entries[i]; @@ -239,7 +248,7 @@ mpl.figure.prototype._init_canvas = function () { // And update the size in Python. We ignore the initial 0/0 size // that occurs as the element is placed into the DOM, which should // otherwise not happen due to the minimum size styling. - if (fig.ws.readyState == 1 && width != 0 && height != 0) { + if (width != 0 && height != 0) { fig.request_resize(width, height); } }