-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[Bug]: ax.hist density not auto-scaled when using histtype='step' #24097
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
Comments
I cannot see a difference between your left and right side plot so it's not clear what difference you are concerned about. |
Thanks for the quick reply. I updated my post with the expected and actual outputs, hope that makes it clearer now. |
In bar mode, the each bin is a Rectangle that is added to the data limit individually. In step mode, all bins are combined into a single outline For example using a 65-bin path from your example: import numpy as np
import matplotlib.path as mpath
vertices = np.array([
[-5.456910832616701, 0.000000000000000],
[-5.456910832616701, 0.000062546965532],
[-5.297030974099689, 0.000062546965532],
[-5.297030974099689, 0.000000000000000],
[-5.137151115582677, 0.000000000000000],
[-5.137151115582677, 0.000000000000000],
[-4.977271257065666, 0.000000000000000],
[-4.977271257065666, 0.000000000000000],
[-4.817391398548653, 0.000000000000000],
[-4.817391398548653, 0.000062546965532],
[-4.657511540031642, 0.000062546965532],
[-4.657511540031642, 0.000375281793195],
[-4.497631681514630, 0.000375281793195],
[-4.497631681514630, 0.000500375724260],
[-4.337751822997618, 0.000500375724260],
[-4.337751822997618, 0.000875657517455],
[-4.177871964480607, 0.000875657517455],
[-4.177871964480607, 0.000688016620857],
[-4.017992105963595, 0.000688016620857],
[-4.017992105963595, 0.001313486276182],
[-3.858112247446583, 0.001313486276182],
[-3.858112247446583, 0.002939707380026],
[-3.698232388929571, 0.002939707380026],
[-3.698232388929571, 0.004065552759611],
[-3.538352530412560, 0.004065552759611],
[-3.538352530412560, 0.005253945104728],
[-3.378472671895548, 0.005253945104728],
[-3.378472671895548, 0.008068558553689],
[-3.218592813378536, 0.008068558553689],
[-3.218592813378536, 0.010945718968183],
[-3.058712954861524, 0.010945718968183],
[-3.058712954861524, 0.014448349038001],
[-2.898833096344513, 0.014448349038001],
[-2.898833096344513, 0.019952482004858],
[-2.738953237827501, 0.019952482004858],
[-2.738953237827501, 0.027833399661950],
[-2.579073379310489, 0.027833399661950],
[-2.579073379310489, 0.040155151871847],
[-2.419193520793477, 0.040155151871847],
[-2.419193520793477, 0.049787384563848],
[-2.259313662276465, 0.049787384563848],
[-2.259313662276465, 0.062984794291199],
[-2.099433803759454, 0.062984794291199],
[-2.099433803759454, 0.081873977882006],
[-1.939553945242442, 0.081873977882006],
[-1.939553945242442, 0.100638067541747],
[-1.779674086725430, 0.100638067541747],
[-1.779674086725430, 0.121153472236398],
[-1.619794228208419, 0.121153472236398],
[-1.619794228208419, 0.143420191965958],
[-1.459914369691407, 0.143420191965958],
[-1.459914369691407, 0.173317641490480],
[-1.300034511174395, 0.173317641490480],
[-1.300034511174395, 0.196460018737493],
[-1.140154652657383, 0.196460018737493],
[-1.140154652657383, 0.222291915502405],
[-0.980274794140372, 0.222291915502405],
[-0.980274794140372, 0.250875878750744],
[-0.820394935623360, 0.250875878750744],
[-0.820394935623360, 0.275331742273941],
[-0.660515077106348, 0.275331742273941],
[-0.660515077106348, 0.295284224278798],
[-0.500635218589336, 0.295284224278798],
[-0.500635218589336, 0.308419087040619],
[-0.340755360072325, 0.308419087040619],
[-0.340755360072325, 0.321491402836906],
[-0.180875501555313, 0.321491402836906],
[-0.180875501555313, 0.334188436839996],
[-0.020995643038301, 0.334188436839996],
[-0.020995643038301, 0.329935243183789],
[0.138884215478710, 0.329935243183789],
[0.138884215478710, 0.330185431045918],
[0.298764073995723, 0.330185431045918],
[0.298764073995723, 0.316675286490905],
[0.458643932512734, 0.316675286490905],
[0.458643932512734, 0.300913451176721],
[0.618523791029746, 0.300913451176721],
[0.618523791029746, 0.284213411379552],
[0.778403649546758, 0.284213411379552],
[0.778403649546758, 0.256692746545263],
[0.938283508063770, 0.256692746545263],
[0.938283508063770, 0.229234628676510],
[1.098163366580781, 0.229234628676510],
[1.098163366580781, 0.194208327978325],
[1.258043225097793, 0.194208327978325],
[1.258043225097793, 0.179071962319466],
[1.417923083614805, 0.179071962319466],
[1.417923083614805, 0.156805242589907],
[1.577802942131816, 0.156805242589907],
[1.577802942131816, 0.127658356651775],
[1.737682800648829, 0.127658356651775],
[1.737682800648829, 0.108018609474579],
[1.897562659165840, 0.108018609474579],
[1.897562659165840, 0.087941033538655],
[2.057442517682852, 0.087941033538655],
[2.057442517682852, 0.071115899810421],
[2.217322376199863, 0.071115899810421],
[2.217322376199863, 0.056855191669017],
[2.377202234716875, 0.056855191669017],
[2.377202234716875, 0.042031560837821],
[2.537082093233887, 0.042031560837821],
[2.537082093233887, 0.029584714696859],
[2.696961951750899, 0.029584714696859],
[2.696961951750899, 0.022892189384885],
[2.856841810267910, 0.022892189384885],
[2.856841810267910, 0.017200415521430],
[3.016721668784922, 0.017200415521430],
[3.016721668784922, 0.012571940072027],
[3.176601527301934, 0.012571940072027],
[3.176601527301934, 0.007630729794962],
[3.336481385818947, 0.007630729794962],
[3.336481385818947, 0.007067807105169],
[3.496361244335957, 0.007067807105169],
[3.496361244335957, 0.003752817931948],
[3.656241102852969, 0.003752817931948],
[3.656241102852969, 0.002877160414494],
[3.816120961369982, 0.002877160414494],
[3.816120961369982, 0.001376033241714],
[3.976000819886992, 0.001376033241714],
[3.976000819886992, 0.001125845379584],
[4.135880678404004, 0.001125845379584],
[4.135880678404004, 0.000875657517455],
[4.295760536921017, 0.000875657517455],
[4.295760536921017, 0.000437828758727],
[4.455640395438029, 0.000437828758727],
[4.455640395438029, 0.000312734827662],
[4.615520253955039, 0.000312734827662],
[4.615520253955039, 0.000125093931065],
[4.775400112472052, 0.000125093931065],
[4.775400112472052, 0.000250187862130],
[4.935279970989065, 0.000250187862130],
[4.935279970989065, 0.000000000000000]])
print('vertices max', vertices.max(axis=0))
path = mpath.Path(vertices)
for segment, segment_code in path.iter_segments():
print(segment, segment_code) outputs:
and obviously that's completely wrong for calculating limits. This can be fixed a few ways, though I need to confirm exactly how is best. |
Path simplification is really scaled for pixel/display unit outputs, but paths in autoscaling are in data units. This sometimes causes autoscaling to pick the wrong limits, as the simplified paths may be smaller than the originals. Fixes matplotlib#24097
Path simplification is really scaled for pixel/display unit outputs, but paths in autoscaling are in data units. This sometimes causes autoscaling to pick the wrong limits, as the simplified paths may be smaller than the originals. Fixes matplotlib#24097
Path simplification is really scaled for pixel/display unit outputs, but paths in autoscaling are in data units. This sometimes causes autoscaling to pick the wrong limits, as the simplified paths may be smaller than the originals. Fixes matplotlib#24097
Path simplification is really scaled for pixel/display unit outputs, but paths in autoscaling are in data units. This sometimes causes autoscaling to pick the wrong limits, as the simplified paths may be smaller than the originals. Fixes matplotlib#24097
Bug summary
I need to plot a histogram of some data (generated by
numpy.save
in binary format) from my work using thematplotlib.axes.Axes.hist
function. I noted that the histogram's density axis (when settingdensity=True
) is not automatically adjusted to fit the whole histogram.I played with different combinations of parameters, and noted that the densities changes if you rescale the whole data array, which is counterintuitive as rescaling the data should only affect the x-axis values. I noted that if you set
histtype="step"
, the issue will occur, but is otherwise okay for otherhisttype
s.I started a github repo for testing this issue here. The
test.npy
file is the data generated from my program.Code for reproduction
Actual outcome
Here's the histograms generated using some simulated data. You can play with the

histtype
andscale
parameters in the code to see the differences. Whenscale=1.2
, I gotExpected outcome
When

scale=1
, sometimes the randomised array would lead to identical left and right panel ...Additional information
No response
Operating system
OS/X
Matplotlib Version
3.6.0
Matplotlib Backend
No response
Python version
3.10.4
Jupyter version
No response
Installation
pip
The text was updated successfully, but these errors were encountered: