8000 The bottom parameter of plt.hist() shifts the data as well, not just the baseline · Issue #16478 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
The bottom parameter of plt.hist() shifts the data as well, not just the baseline #16478
Closed
@torokati44

Description

@torokati44

When passing a bottom parameter to plt.hist(), the entire histogram is shifted by that amount, as a whole, instead of just the baseline, as the documentation would suggest.

See this simple example:

import numpy as np
import matplotlib.pyplot as plt
np.random.seed(42)
x = np.random.randn(10000)
b, e = np.histogram(x)
plt.hist(e[:-1], e, weights=b)
plt.show()

This output of this is:
unshifted

This is all fine and good.

When adding the bottom=1500 parameter:

import numpy as np
import matplotlib.pyplot as plt
np.random.seed(42)
x = np.random.randn(10000)
b, e = np.histogram(x)
plt.hist(e[:-1], e, weights=b, bottom=1500)
plt.show()

This is what I get:

shifted

The "indicated values" of the histogram have shifted up by 1500 as well, not just the baseline. By baseline, I mean the "straight horizontal" outline of the filled region.

What I was expecting instead, can also be achieved by "unshifting" the actual data as well, by subtracting the value of the bottom parameter from it:

import numpy as np
import matplotlib.pyplot as plt
np.random.seed(42)
x = np.random.randn(10000)
b, e = np.histogram(x)
plt.hist(e[:-1], e, weights=b-1500, bottom=1500)
plt.show()

See:
corrected

It might be the case that this is entirely what the original intent was behind the functionality, but at the very least, the documentation is misleading in my opinion.
Also, this might apply to plt.bar() as well.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0