8000 retaining dtype of hist bins as int when data is int and no weighting is used by pharshalp · Pull Request #12802 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

retaining dtype of hist bins as int when data is int and no weighting is used #12802

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

Closed
wants to merge 2 commits into from
Closed

Conversation

pharshalp
Copy link
Contributor
@pharshalp pharshalp commented Nov 12, 2018

PR Summary

When calling the histogram plotting method/function, if the input data has dtype int and no weighting/nomralization is used then the returned histogram values should be returned with dtype int (as is the behavior of numpy.histogram). The current implementation tries to avoid incosistency in return type of numpy.histogram (int if no weighting, float if weighting used) by always casting the input data as float.

This PR is an attempt to address #12784 by postponing the casting step in the ax.hist code. The data is casted to float if stacked and density kwarg is True.

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
n, _, _ = ax.hist(np.arange(10))
print(n)
print(n.dtype)

Output (before this PR):

[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
float64

Output (after this PR):

[1 1 1 1 1 1 1 1 1 1]
int64

(alternative is to just document the behavior as attempted in #12800).

PR Checklist

  • [ ] Has Pytest style unit tests
  • Code is Flake 8 compliant
  • [ ] New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • [ ] Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
    -~~ [ ] Documented in doc/api/api_changes.rst if API changed in a backward-incompatible way~~

@jklymak
Copy link
Member
jklymak commented Nov 13, 2018

n is the number of points in each bin or its normalized value. I guess this can be an integer if we want, but...

@pharshalp
Copy link
Contributor Author

Closing this PR... I wasn't carefully thinking about the problem (and got excited by the prospect of making a quick contribution).

The following comment made it clear... thanks @jklymak
#12784 (comment)

n is either the number of counts in a bin, or some normalization of the number of counts. You can argue that it should be integers for counts, and floats for the normalized counts, but it should have nothing to do with the dtype of the data being binned.

@pharshalp pharshalp closed this Nov 14, 2018
@pharshalp pharshalp deleted the hist_int_fix branch November 17, 2018 19:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0