Skip to content

Commit 509dc1f

Browse files
authored
Don't force axes limits in hist2d. (matplotlib#30634)
Standard autoscaling works just fine there.
1 parent f31a12b commit 509dc1f

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
hist2d no longer forces axes limits
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
Previously, `.Axes.hist2d` would force the axes x and y limits to the extents
4+
of the histogrammed data, ignoring any other artists. `.Axes.hist2d` now
5+
behaves similarly to `.Axes.imshow`: axes limits are updated to fit the data,
6+
but autoscaling is not otherwise disabled.

lib/matplotlib/axes/_axes.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7772,13 +7772,15 @@ def hist2d(self, x, y, bins=10, range=None, density=False, weights=None,
77727772
77737773
Notes
77747774
-----
7775-
- Currently ``hist2d`` calculates its own axis limits, and any limits
7776-
previously set are ignored.
7777-
- Rendering the histogram with a logarithmic color scale is
7778-
accomplished by passing a `.colors.LogNorm` instance to the *norm*
7779-
keyword argument. Likewise, power-law normalization (similar
7780-
in effect to gamma correction) can be accomplished with
7781-
`.colors.PowerNorm`.
7775+
Rendering the histogram with a logarithmic color scale is accomplished
7776+
by passing a `.colors.LogNorm` instance to the *norm* keyword
7777+
argument. Likewise, power-law normalization (similar in effect to gamma
7778+
correction) can be accomplished with `.colors.PowerNorm`.
7779+
7780+
.. versionchanged:: 3.11
7781+
Previously, `~.Axes.hist2d` would force the axes limits to match the
7782+
extents of the histogram; now, autoscaling also takes other plot
7783+
elements into account.
77827784
"""
77837785

77847786
h, xedges, yedges = np.histogram2d(x, y, bins=bins, range=range,
@@ -7790,8 +7792,6 @@ def hist2d(self, x, y, bins=10, range=None, density=False, weights=None,
77907792
h[h > cmax] = None
77917793

77927794
pc = self.pcolormesh(xedges, yedges, h.T, **kwargs)
7793-
self.set_xlim(xedges[0], xedges[-1])
7794-
self.set_ylim(yedges[0], yedges[-1])
77957795

77967796
return h, xedges, yedges, pc
77977797

lib/matplotlib/tests/test_axes.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2837,6 +2837,16 @@ def test_hist2d_density():
28372837
obj.hist2d(x, y, density=True)
28382838

28392839

2840+
@mpl.style.context("mpl20")
2841+
def test_hist2d_autolimits():
2842+
x, y = np.random.random((2, 100))
2843+
ax = plt.figure().add_subplot()
2844+
ax.hist2d(x, y)
2845+
assert ax.get_xlim() == (x.min(), x.max())
2846+
assert ax.get_ylim() == (y.min(), y.max())
2847+
assert ax.get_autoscale_on() # Autolimits have not been disabled.
2848+
2849+
28402850
class TestScatter:
28412851
@image_comparison(['scatter'], style='mpl20', remove_text=True)
28422852
def test_scatter_plot(self):

0 commit comments

Comments
 (0)