Skip to content

Commit 4ea21a7

Browse files
committed
Fix histplot auto linewidth on log scaled axis
Fixes #2513
1 parent 6d84f82 commit 4ea21a7

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

doc/releases/v0.12.0.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ v0.12.0 (Unreleased)
3030

3131
- |Fix| In :func:`histplot`, fixed a bug where using `shrink` with non-discrete bins shifted bar positions inaccurately (:pr:`2477`).
3232

33+
- |Fix| In :func:`histplot`, fixed a bug where automatically computed linewidths were too thick for a log-scaled histogram (:pr:2522`).
34+
3335
- |Fix| In :func:`displot`, fixed a bug where `common_norm` was ignored when `kind="hist"` and faceting was used without assigning `hue` (:pr:`2468`).
3436

3537
- |Defaults| In :func:`displot`, the default alpha value now adjusts to a provided `multiple` parameter even when `hue` is not assigned (:pr:`2462`).

seaborn/distributions.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -644,15 +644,18 @@ def plot_univariate_histogram(
644644
ax.autoscale_view()
645645

646646
# We will base everything on the minimum bin width
647-
hist_metadata = [h.index.to_frame() for _, h in histograms.items()]
648-
binwidth = min([
649-
h["widths"].min() for h in hist_metadata
650-
])
647+
hist_metadata = pd.concat([
648+
# Use .items for generality over dict or df
649+
h.index.to_frame() for _, h in histograms.items()
650+
]).reset_index(drop=True)
651+
thin_bar_idx = hist_metadata["widths"].idxmin()
652+
binwidth = hist_metadata.loc[thin_bar_idx, "widths"]
653+
left_edge = hist_metadata.loc[thin_bar_idx, "edges"]
651654

652655
# Convert binwidth from data coordinates to pixels
653656
pts_x, pts_y = 72 / ax.figure.dpi * (
654-
ax.transData.transform([binwidth, binwidth])
655-
- ax.transData.transform([0, 0])
657+
ax.transData.transform([left_edge + binwidth] * 2)
658+
- ax.transData.transform([left_edge] * 2)
656659
)
657660
if self.data_variable == "x":
658661
binwidth_points = pts_x

seaborn/tests/test_distributions.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -940,7 +940,7 @@ def test_log_scale(self, rng):
940940
for c1, c2 in zip(ax1.collections, ax2.collections):
941941
assert_array_equal(c1.get_segments(), c2.get_segments())
942942

943-
def test_bandwiddth(self, rng):
943+
def test_bandwidth(self, rng):
944944

945945
n = 100
946946
x, y = rng.multivariate_normal([0, 0], [(.2, .5), (.5, 2)], n).T
@@ -1675,6 +1675,12 @@ def test_auto_linewidth(self, flat_series, fill):
16751675
histplot(flat_series, **kws, bins=30, ax=ax2)
16761676
assert get_lw(ax1) > get_lw(ax2)
16771677

1678+
f, ax1 = plt.subplots(figsize=(4, 5))
1679+
f, ax2 = plt.subplots(figsize=(4, 5))
1680+
histplot(flat_series, **kws, bins=30, ax=ax1)
1681+
histplot(10 ** flat_series, **kws, bins=30, log_scale=True, ax=ax2)
1682+
assert get_lw(ax1) == pytest.approx(get_lw(ax2))
1683+
16781684
def test_bar_kwargs(self, flat_series):
16791685

16801686
lw = 2

0 commit comments

Comments
 (0)