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

Skip to content

Force origin='upper' in pyplot.specgram #17897

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 29 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
77a2b67
Link to style-file example page in docs
dstansby Jul 10, 2020
cc4a4db
Force origin='upper' in specgram function
kate-perkins Jul 11, 2020
b5ef28f
DOC: indicate that origin is force set to upper
kate-perkins Jul 12, 2020
3ee2131
Adding test for issue 17878
theodorju Jul 12, 2020
9aab320
DOC: Change rcParam from 'origin' to 'image.origin'
kate-perkins Jul 12, 2020
67df843
TST: try changing origin in specgram call
kate-perkins Jul 12, 2020
48288f8
Document behavior change for specgram
kate-perkins Jul 12, 2020
ef4c5dc
Document api behavior change
kate-perkins Jul 12, 2020
45ce308
Reduced line length in test_axes.py
theodorju Jul 12, 2020
ae95c37
Merge branch 'fix-for-issue-17878' of https://github.com/kate-perkins…
theodorju Jul 12, 2020
8d24ff7
Raise exception if origin kwarg passed to specgram
kate-perkins Jul 12, 2020
9cf10dd
TST: passing origin kwarg to specgram should fail
kate-perkins Jul 12, 2020
8856c7f
Merge by keeping most recent
kate-perkins Jul 12, 2020
054ed06
Shorten long line
kate-perkins Jul 12, 2020
326ce3e
Fix documentation build error
kate-perkins Jul 13, 2020
d5959b5
DOC: Remove note that origin='upper' in specgram
kate-perkins Jul 13, 2020
c9e1154
Shorten title and spell out keyword argument
kate-perkins Jul 14, 2020
d0968be
TST: change from pytest xfail to pytest raises
kate-perkins Jul 14, 2020
e4249e0
DOC: Add blank line after kwargs parameter
kate-perkins Jul 14, 2020
5633a45
Remove whitespace on blank line
kate-perkins Jul 14, 2020
2310ff5
Shorten long line
kate-perkins Jul 12, 2020
f8afa14
Fix documentation build error
kate-perkins Jul 13, 2020
007eaae
DOC: Remove note that origin='upper' in specgram
kate-perkins Jul 13, 2020
3f06646
Shorten title and spell out keyword argument
kate-perkins Jul 14, 2020
d79202f
TST: change from pytest xfail to pytest raises
kate-perkins Jul 14, 2020
3e6aa01
DOC: Add blank line after kwargs parameter
kate-perkins Jul 14, 2020
3aed2cf
Remove whitespace on blank line
kate-perkins Jul 14, 2020
f958e88
Wrap lines
kate-perkins Jul 15, 2020
0d53e81
Merge by keeping most recent
kate-perkins Jul 15, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions doc/api/next_api_changes/behavior/17897-KLP.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pyplot.specgram always uses origin='upper'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Previously if ``image.origin`` was set to something other than 'upper' or if the
``origin`` keyword argument was passed with a value other than 'upper', the spectrogram
itself would flip, but the axes would remain oriented for an origin value of 'upper', so
that the resulting plot was incorrectly labelled.

Now, the ``origin`` keyword argument is not supported and the ``image.origin`` rcParam is
ignored. The function matplotlib.pyplot.specgram is forced to use ``origin='upper'``, so
that the axes are correct for the plotted spectrogram.
10 changes: 8 additions & 2 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7464,7 +7464,8 @@ def specgram(self, x, NFFT=None, Fs=None, Fc=None, detrend=None,

**kwargs
Additional keyword arguments are passed on to `~.axes.Axes.imshow`
which makes the specgram image.
which makes the specgram image. The origin keyword argument
is not supported.

Returns
-------
Expand Down Expand Up @@ -7548,8 +7549,13 @@ def specgram(self, x, NFFT=None, Fs=None, Fc=None, detrend=None,
xmin, xmax = xextent
freqs += Fc
extent = xmin, xmax, freqs[0], freqs[-1]

if 'origin' in kwargs:
raise TypeError("specgram() got an unexpected keyword argument "
"'origin'")

im = self.imshow(Z, cmap, extent=extent, vmin=vmin, vmax=vmax,
**kwargs)
origin='upper', **kwargs)
self.axis('auto')

return spec, freqs, t, im
Expand Down
27 changes: 27 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4099,6 +4099,33 @@ def test_specgram_fs_none():
assert xmin == 32 and xmax == 96


@check_figures_equal(extensions=["png"])
def test_specgram_origin_rcparam(fig_test, fig_ref):
"""Test specgram ignores image.origin rcParam and uses origin 'upper'."""
t = np.arange(500)
signal = np.sin(t)

plt.rcParams["image.origin"] = 'upper'

# Reference: First graph using default origin in imshow (upper),
fig_ref.subplots().specgram(signal)

# Try to overwrite the setting trying to flip the specgram
plt.rcParams["image.origin"] = 'lower'

# Test: origin='lower' should be ignored
fig_test.subplots().specgram(signal)


def test_specgram_origin_kwarg():
"""Ensure passing origin as a kwarg raises a TypeError."""
t = np.arange(500)
signal = np.sin(t)

with pytest.raises(TypeError):
plt.specgram(signal, origin='lower')


@image_comparison(
["psd_freqs.png", "csd_freqs.png", "psd_noise.png", "csd_noise.png"],
remove_text=True, tol=0.002)
Expand Down
4 changes: 2 additions & 2 deletions tutorials/introductory/customizing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
<customizing-with-matplotlibrc-files>` file (which is read at startup to
configure Matplotlib).

There are a number of pre-defined styles `provided by Matplotlib`_. For
There are a number of pre-defined styles :doc:`provided by Matplotlib
</gallery/style_sheets/style_sheets_reference>`. For
example, there's a pre-defined style called "ggplot", which emulates the
aesthetics of ggplot_ (a popular plotting package for R_). To use this style,
just add:
Expand Down Expand Up @@ -202,4 +203,3 @@
#
# .. _ggplot: https://ggplot2.tidyverse.org/
# .. _R: https://www.r-project.org/
# .. _provided by Matplotlib: https://github.com/matplotlib/matplotlib/tree/master/lib/matplotlib/mpl-data/stylelib