diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index d379e08fda10..ec06d15ab9bd 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -7617,6 +7617,8 @@ def specgram(self, x, NFFT=None, Fs=None, Fc=None, detrend=None, Fc = 0 # same default as in mlab._spectral_helper() if noverlap is None: noverlap = 128 # same default as in mlab.specgram() + if Fs is None: + Fs = 2 # same default as in mlab._spectral_helper() if mode == 'complex': raise ValueError('Cannot plot a complex specgram') diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 47a028256640..acc32bac243a 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -4166,6 +4166,13 @@ def test_specgram_angle(): scale="dB") +def test_specgram_fs_none(): + """Test axes.specgram when Fs is None, should not throw error.""" + spec, freqs, t, im = plt.specgram(np.ones(300), Fs=None) + xmin, xmax, freq0, freq1 = im.get_extent() + assert xmin == 32 and xmax == 96 + + @image_comparison( ["psd_freqs.png", "csd_freqs.png", "psd_noise.png", "csd_noise.png"], remove_text=True, tol=0.002)