From b4253bad508a16f3bac1a357bb901af53c323411 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Mon, 27 Sep 2021 12:48:25 +0200 Subject: [PATCH] Fix very-edge case in csd(), plus small additional cleanups. Previously, `csd([0, 0], [0, 0])` would fail with a ZeroDivisionError when computing yticks. Fix that by copying the logic from psd, and streamlining it in both places. Also fix an unformatted exception message in specgram. --- lib/matplotlib/axes/_axes.py | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 6855299689fc..81ae260179f7 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -7035,12 +7035,9 @@ def psd(self, x, NFFT=None, Fs=None, Fc=None, detrend=None, self.set_xlabel('Frequency') self.set_ylabel('Power Spectral Density (%s)' % psd_units) self.grid(True) + vmin, vmax = self.viewLim.intervaly - intv = vmax - vmin - logi = int(np.log10(intv)) - if logi == 0: - logi = .1 - step = 10 * logi + step = max(10 * int(np.log10(vmax - vmin)), 1) ticks = np.arange(math.floor(vmin), math.ceil(vmax) + 1, step) self.set_yticks(ticks) @@ -7140,11 +7137,9 @@ def csd(self, x, y, NFFT=None, Fs=None, Fc=None, detrend=None, self.set_xlabel('Frequency') self.set_ylabel('Cross Spectrum Magnitude (dB)') self.grid(True) - vmin, vmax = self.viewLim.intervaly - - intv = vmax - vmin - step = 10 * int(np.log10(intv)) + vmin, vmax = self.viewLim.intervaly + step = max(10 * int(np.log10(vmax - vmin)), 1) ticks = np.arange(math.floor(vmin), math.ceil(vmax) + 1, step) self.set_yticks(ticks) @@ -7576,7 +7571,7 @@ def specgram(self, x, NFFT=None, Fs=None, Fc=None, detrend=None, else: Z = 20. * np.log10(spec) else: - raise ValueError('Unknown scale %s', scale) + raise ValueError(f'Unknown scale {scale!r}') Z = np.flipud(Z)