Allow setting frequency units for psd plots by cdholmes · Pull Request #32211 · matplotlib/matplotlib · GitHub
Skip to content

Allow setting frequency units for psd plots - #32211

Merged
QuLogic merged 4 commits into
matplotlib:mainfrom
cdholmes:psd_units
Aug 22, 2026
Merged

QuLogic merged 4 commits into
matplotlib:mainfrom
cdholmes:psd_units

Conversation

@cdholmes

@cdholmes cdholmes commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

PR summary

This PR allows users to specify the units for the sampling frequency in power spectral density (psd) plots. psd previously assumed units to be Hz and displayed Hz on the y-axis label. Users can now specify other units with the Funits keyword. If the keyword is omitted, Hz is assumed by default, so the change is backward compatible. This change has no effect on the psd calculation, only the resulting plot.

AI Disclosure

No AI was used

PR quality check

  • Use an expressive title, e.g. "Fix title font property precedence"
  • New and changed code is tested
  • Plotting related features are demonstrated in an example
  • New features and API changes have release notes
  • Documentation complies with general and docstring guidelines

@github-actions

Copy link
Copy Markdown

@cdholmes
cdholmes marked this pull request as draft August 15, 2026 00:11
@cdholmes
cdholmes marked this pull request as ready for review August 15, 2026 01:02

@scottshambaugh scottshambaugh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One comment to avoid an API break, but I think this looks pretty good and is a nice quality-of-life improvement.

Comment thread lib/matplotlib/axes/_axes.py Outdated
@scottshambaugh

Copy link
Copy Markdown
Contributor

TBH I didn't know that we had this function, looks like it's in here mainly for MATLAB compatibility. Normally we don't deal with units so I'd be tempted to deprecate it, but I think it's fine for a compatibility function. There are some references to Hz in the comments of lib/matplotlib/mlab.py which is reached by that scale_by_freq arg, could you please update those as well?

@story645

story645 commented Aug 16, 2026

Copy link
Copy Markdown
Member

Normally we don't deal with units so I'd be tempted to deprecate it, but I think it's fine for a compatibility function.

We support physical units through the units module, so I'm curious why that wouldn't work/can't be patched in here. Using unit data should put those units in the label b/c the unit's class handles labeling.

@jklymak

jklymak commented Aug 16, 2026

Copy link
Copy Markdown
Member

I think we should have deprecated these functions years ago. Folks should use scipy.signal and then plot as they need for their field. Some of the calculations are incorrect and no one has the time or means to maintain this properly and fix bugs. See #22920

@story645

Copy link
Copy Markdown
Member

Folks should use scipy.signal and then plot as they need for their field

If the problem is that this needs replacement , could we do something like requiring scipy only if you're using this part of the API? It would allow us to shim in the scipy piece w/o making it a hard requirement.

@jklymak

jklymak commented Aug 17, 2026

Copy link
Copy Markdown
Member

I don't think this needs replacement. It should just be removed. This was a Matlab leftover from before scipy existed. If someone wanted to write a downstream wrapper around Matplotlib and scipy I think that would be valuable, but I would not recommend these routines for general use.

@cdholmes

Copy link
Copy Markdown
Contributor Author

I seem to have unintentionally begun (or restarted) a larger discussion.

As suggested by @scottshambaugh, I edited the mlab comments so that Hz frequency units are used as an example, but not assumed. I have no plan to develop these functions further, apart from any minor edits to complete this PR. While I think this small change is useful now, I respect your decision about whether to merge this PR, given the long-term vision of replacing or deprecating/removing these functions.

Thanks for your consideration.

@rcomer rcomer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @cdholmes I have some comments on the documentation and a question about the code comment.

mlab is a bit of a strange corner of Matplotlib: I do not think we would add something like this now, but AFAIK there has been no definite decision to deprecate it. We have not even formally discouraged its use. So I do not think we would want to put a lot of effort into improving these functions, but there is no harm in taking a small change like this.

Comment thread doc/api/next_api_changes/development/32211_CDH.rst Outdated
Comment thread doc/release/next_whats_new/new_psd_feature.rst Outdated
Comment thread doc/release/next_whats_new/new_psd_feature.rst Outdated
Comment thread lib/matplotlib/axes/_axes.py
Comment thread lib/matplotlib/mlab.py
@cdholmes cdholmes closed this Aug 17, 2026
@cdholmes cdholmes reopened this Aug 17, 2026
In psd, allow user to specify the units for the sampling frequency of the analyzed array. psd previously assumed units to be Hz and displayed this on the y-axis label. Users can now specify other units. This change has no effect on the psd calculation.

Update psd documentation, example, type hinting, release notes

@rcomer rcomer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. We require two approving reviews for code changes, so I can't merge this yet.

Comment thread doc/release/next_whats_new/new_psd_feature.rst Outdated
Comment thread doc/release/next_whats_new/new_psd_feature.rst Outdated
Comment thread lib/matplotlib/axes/_axes.pyi Outdated
cdholmes and others added 3 commits August 21, 2026 12:48
Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
@QuLogic
QuLogic merged commit adecc56 into matplotlib:main Aug 22, 2026
40 of 41 checks passed
@QuLogic QuLogic added this to the v3.12.0 milestone Aug 22, 2026
@QuLogic

QuLogic commented Aug 22, 2026

Copy link
Copy Markdown
Member

clin1234 pushed a commit to clin1234/matplotlib that referenced this pull request Aug 23, 2026
In psd, allow user to specify the units for the sampling frequency of the analyzed array. psd previously assumed units to be Hz and displayed this on the y-axis label. Users can now specify other units. This change has no effect on the psd calculation.

Update psd documentation, example, type hinting, release notes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants