diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 96e233ed92f8..4eca2e6b0bdb 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -7042,7 +7042,7 @@ def psd(self, x, NFFT=None, Fs=None, Fc=None, detrend=None, self.set_ylabel('Power Spectral Density (%s)' % psd_units) self.grid(True) - vmin, vmax = self.viewLim.intervaly + vmin, vmax = self.get_ybound() step = max(10 * int(np.log10(vmax - vmin)), 1) ticks = np.arange(math.floor(vmin), math.ceil(vmax) + 1, step) self.set_yticks(ticks) @@ -7144,7 +7144,7 @@ def csd(self, x, y, NFFT=None, Fs=None, Fc=None, detrend=None, self.set_ylabel('Cross Spectrum Magnitude (dB)') self.grid(True) - vmin, vmax = self.viewLim.intervaly + vmin, vmax = self.get_ybound() step = max(10 * int(np.log10(vmax - vmin)), 1) ticks = np.arange(math.floor(vmin), math.ceil(vmax) + 1, step) self.set_yticks(ticks) diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 68a698332424..3168fe86ad66 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -4671,6 +4671,16 @@ def test_spectrum(): ax.set(xlabel="", ylabel="") +def test_psd_csd_edge_cases(): + # Inverted yaxis or fully zero inputs used to throw exceptions. + axs = plt.figure().subplots(2) + for ax in axs: + ax.yaxis.set(inverted=True) + with np.errstate(divide="ignore"): + axs[0].psd(np.zeros(5)) + axs[1].csd(np.zeros(5), np.zeros(5)) + + @check_figures_equal(extensions=['png']) def test_twin_remove(fig_test, fig_ref): ax_test = fig_test.add_subplot()