Skip to content

Commit 9bd1665

Browse files
authored
Don't use offset / scientific notation in numeric legends (#3187)
* Don't show offset / scientific notation in numeric legends * Update release notes
1 parent 22cdfb0 commit 9bd1665

File tree

5 files changed

+29
-0
lines changed

5 files changed

+29
-0
lines changed

doc/whatsnew/v0.12.2.rst

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

77
- |Enhancement| Automatic mark widths are now calculated separately for unshared facet axes (:pr:`3119`).
88

9+
- |Fix| Fixed a bug where legends for numeric variables with large values with be incorrectly shown (i.e. with a missing offset or exponent; :pr:`3187`).
10+
911
- |Fix| Fixed a regression in v0.12.0 where manually-added labels could have duplicate legend entries (:pr:`3116`).
1012

1113
- |Fix| Fixed a bug in :func:`histplot` with `kde=True` and `log_scale=True` where the curve was not scaled properly (:pr:`3173`).

seaborn/_core/scales.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,14 @@ def spacer(x):
378378
axis.set_view_interval(vmin, vmax)
379379
locs = axis.major.locator()
380380
locs = locs[(vmin <= locs) & (locs <= vmax)]
381+
# Avoid having an offset / scientific notation in a legend
382+
# as we don't represent that anywhere so it ends up incorrect.
383+
# This could become an option (e.g. Continuous.label(offset=True))
384+
# in which case we would need to figure out how to show it.
385+
if hasattr(axis.major.formatter, "set_useOffset"):
386+
axis.major.formatter.set_useOffset(False)
387+
if hasattr(axis.major.formatter, "set_scientific"):
388+
axis.major.formatter.set_scientific(False)
381389
labels = axis.major.formatter.format_ticks(locs)
382390
new._legend = list(locs), list(labels)
383391

seaborn/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,10 @@ def get_view_interval(self):
699699
formatter = mpl.ticker.LogFormatter()
700700
else:
701701
formatter = mpl.ticker.ScalarFormatter()
702+
# Avoid having an offset/scientific notation which we don't currently
703+
# have any way of representing in the legend
704+
formatter.set_useOffset(False)
705+
formatter.set_scientific(False)
702706
formatter.axis = dummy_axis()
703707

704708
# TODO: The following two lines should be replaced

tests/_core/test_plot.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,6 +2051,15 @@ def _legend_artist(self, variables, value, scales):
20512051
p = Plot(**xy, color=["a", "b", "c", "d"]).add(NoLegendMark()).plot()
20522052
assert not p._figure.legends
20532053

2054+
def test_legend_has_no_offset(self, xy):
2055+
2056+
color = np.add(xy["x"], 1e8)
2057+
p = Plot(**xy, color=color).add(MockMark()).plot()
2058+
legend = p._figure.legends[0]
2059+
assert legend.texts
2060+
for text in legend.texts:
2061+
assert float(text.get_text()) > 1e7
2062+
20542063

20552064
class TestDefaultObject:
20562065

tests/test_relational.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,12 @@ def test_ax_kwarg_removal(self, long_df):
675675
assert len(ax.collections) == 0
676676
assert len(g.ax.collections) > 0
677677

678+
def test_legend_has_no_offset(self, long_df):
679+
680+
g = relplot(data=long_df, x="x", y="y", hue=long_df["z"] + 1e8)
681+
for text in g.legend.texts:
682+
assert float(text.get_text()) > 1e7
683+
678684

679685
class TestLinePlotter(SharedAxesLevelTests, Helpers):
680686

0 commit comments

Comments
 (0)