Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7a4df1a

Browse files
kate-perkinsdstansbytheodorjutimhoffm
authored
Force origin='upper' in pyplot.specgram (#17897)
* Force origin='upper' in specgram function Specify origin='upper' in call to imshow within specgram to override rcparams setting for origin without changing rcparams itself. Delete origin from kwargs so there are not two occurrences of the origin parameter in the call to imshow (this also means the origin kwarg cannot be changed by the caller). * DOC: indicate that origin is force set to upper * Adding test for issue 17878 * DOC: Change rcParam from 'origin' to 'image.origin' * TST: try changing origin in specgram call * Document behavior change for specgram * Document api behavior change * Raise exception if origin kwarg passed to specgram This ensures the origin kwarg is not silently ignored if passed to pyplot.specgram. * TST: passing origin kwarg to specgram should fail The function pyplot.specgram should raise a TypeError if origin is passed as a keyword argument. * DOC: Remove note that origin='upper' in specgram This note referenced the call to imshow inside of specgram, which might confuse users who do not know the implementation of specgram. * TST: change from pytest xfail to pytest raises Passing origin as a keyword argument should raise a type error, so test_specgram_origin_kwarg should raise an exception. Therefore, raises is a more precise check than xfail. Co-authored-by: David Stansby <[email protected]> Co-authored-by: Theodor Athanasiadis <[email protected]> Co-authored-by: Tim Hoffmann <[email protected]>
1 parent b7ba9e4 commit 7a4df1a

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
pyplot.specgram always uses origin='upper'
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Previously if ``image.origin`` was set to something other than 'upper' or if the
5+
``origin`` keyword argument was passed with a value other than 'upper', the spectrogram
6+
itself would flip, but the axes would remain oriented for an origin value of 'upper', so
7+
that the resulting plot was incorrectly labelled.
8+
9+
Now, the ``origin`` keyword argument is not supported and the ``image.origin`` rcParam is
10+
ignored. The function matplotlib.pyplot.specgram is forced to use ``origin='upper'``, so
11+
that the axes are correct for the plotted spectrogram.

lib/matplotlib/axes/_axes.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7464,7 +7464,8 @@ def specgram(self, x, NFFT=None, Fs=None, Fc=None, detrend=None,
74647464
74657465
**kwargs
74667466
Additional keyword arguments are passed on to `~.axes.Axes.imshow`
7467-
which makes the specgram image.
7467+
which makes the specgram image. The origin keyword argument
7468+
is not supported.
74687469
74697470
Returns
74707471
-------
@@ -7548,8 +7549,13 @@ def specgram(self, x, NFFT=None, Fs=None, Fc=None, detrend=None,
75487549
xmin, xmax = xextent
75497550
freqs += Fc
75507551
extent = xmin, xmax, freqs[0], freqs[-1]
7552+
7553+
if 'origin' in kwargs:
7554+
raise TypeError("specgram() got an unexpected keyword argument "
7555+
"'origin'")
7556+
75517557
im = self.imshow(Z, cmap, extent=extent, vmin=vmin, vmax=vmax,
7552-
**kwargs)
7558+
origin='upper', **kwargs)
75537559
self.axis('auto')
75547560

75557561
return spec, freqs, t, im

lib/matplotlib/tests/test_axes.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4104,6 +4104,33 @@ def test_specgram_fs_none():
41044104
assert xmin == 32 and xmax == 96
41054105

41064106

4107+
@check_figures_equal(extensions=["png"])
4108+
def test_specgram_origin_rcparam(fig_test, fig_ref):
4109+
"""Test specgram ignores image.origin rcParam and uses origin 'upper'."""
4110+
t = np.arange(500)
4111+
signal = np.sin(t)
4112+
4113+
plt.rcParams["image.origin"] = 'upper'
4114+
4115+
# Reference: First graph using default origin in imshow (upper),
4116+
fig_ref.subplots().specgram(signal)
4117+
4118+
# Try to overwrite the setting trying to flip the specgram
4119+
plt.rcParams["image.origin"] = 'lower'
4120+
4121+
# Test: origin='lower' should be ignored
4122+
fig_test.subplots().specgram(signal)
4123+
4124+
4125+
def test_specgram_origin_kwarg():
4126+
"""Ensure passing origin as a kwarg raises a TypeError."""
4127+
t = np.arange(500)
4128+
signal = np.sin(t)
4129+
4130+
with pytest.raises(TypeError):
4131+
plt.specgram(signal, origin='lower')
4132+
4133+
41074134
@image_comparison(
41084135
["psd_freqs.png", "csd_freqs.png", "psd_noise.png", "csd_noise.png"],
41094136
remove_text=True, tol=0.002)

0 commit comments

Comments
 (0)