Skip to content

Commit 9276e22

Browse files
authored
Always use unit width for categorical axes (#3390)
1 parent b6737d5 commit 9276e22

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

seaborn/categorical.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,11 @@ def _configure_legend(self, ax, func, common_kws=None, semantic_kws=None):
375375
@property
376376
def _native_width(self):
377377
"""Return unit of width separating categories on native numeric scale."""
378+
# Categorical data always have a unit width
379+
if self.var_types[self.orient] == "categorical":
380+
return 1
381+
382+
# Otherwise, define the width as the smallest space between observations
378383
unique_values = np.unique(self.comp_data[self.orient])
379384
if len(unique_values) > 1:
380385
native_width = np.nanmin(np.diff(unique_values))

tests/test_categorical.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2198,9 +2198,16 @@ def test_width_native_scale(self):
21982198
width = .5
21992199
x, y = [4, 6, 10], [1, 2, 3]
22002200
ax = barplot(x=x, y=y, width=width, native_scale=True)
2201-
for i, bar in enumerate(ax.patches):
2201+
for bar in ax.patches:
22022202
assert bar.get_width() == (width * 2)
22032203

2204+
def test_width_spaced_categories(self):
2205+
2206+
ax = barplot(x=["a", "b", "c"], y=[4, 5, 6])
2207+
barplot(x=["a", "c"], y=[1, 3], ax=ax)
2208+
for bar in ax.patches:
2209+
assert bar.get_width() == pytest.approx(0.8)
2210+
22042211
def test_saturation_color(self):
22052212

22062213
color = (.1, .9, .2)

0 commit comments

Comments
 (0)