From c3e84b042cbc9c6e805fa96968817317d6942391 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 25 Aug 2016 20:47:29 -0700 Subject: [PATCH] Catch invalid interactive switch to log scale. `plt.plot([1, 2], [-1, -2])` and press `l` (switch y to log-scale). Before, this would crash the Qt5 backend and cause an invalid internal state in others, due to the complete lack of positive values. Now a warning is printed and the command is cancelled. --- lib/matplotlib/backend_bases.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/backend_bases.py b/lib/matplotlib/backend_bases.py index c66c10196d5a..adc2922ab4df 100644 --- a/lib/matplotlib/backend_bases.py +++ b/lib/matplotlib/backend_bases.py @@ -2578,7 +2578,11 @@ def _get_uniform_gridstate(ticks): ax.set_yscale('linear') ax.figure.canvas.draw_idle() elif scale == 'linear': - ax.set_yscale('log') + try: + ax.set_yscale('log') + except ValueError as exc: + warnings.warn(str(exc)) + ax.set_yscale('linear') ax.figure.canvas.draw_idle() # toggle scaling of x-axes between 'log and 'linear' (default key 'k') elif event.key in toggle_xscale_keys: @@ -2587,7 +2591,11 @@ def _get_uniform_gridstate(ticks): ax.set_xscale('linear') ax.figure.canvas.draw_idle() elif scalex == 'linear': - ax.set_xscale('log') + try: + ax.set_xscale('log') + except ValueError: + warnings.warn(str(exc)) + ax.set_xscale('linear') ax.figure.canvas.draw_idle() elif (event.key.isdigit() and event.key != '0') or event.key in all_keys: