-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
BUG: Fixes #36918 boxplots with matplotlib #37107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -246,6 +246,7 @@ def boxplot( | |
): | ||
|
||
import matplotlib.pyplot as plt | ||
from matplotlib.ticker import FixedFormatter | ||
|
||
# validate return_type: | ||
if return_type not in BoxPlot._valid_return_types: | ||
|
@@ -302,15 +303,13 @@ def plot_group(keys, values, ax: "Axes"): | |
bp = ax.boxplot(values, **kwds) | ||
if fontsize is not None: | ||
ax.tick_params(axis="both", labelsize=fontsize) | ||
|
||
if kwds.get("vert", 1): | ||
ticks = ax.get_xticks() | ||
if len(ticks) != len(keys): | ||
i, remainder = divmod(len(ticks), len(keys)) | ||
assert remainder == 0, remainder | ||
keys *= i | ||
ax.set_xticklabels(keys, rotation=rot) | ||
axis = ax.xaxis | ||
else: | ||
ax.set_yticklabels(keys, rotation=rot) | ||
axis = ax.yaxis | ||
axis.set_major_formatter(FixedFormatter(keys)) | ||
ax.tick_params(axis=axis.axis_name, which="major", rotation=rot) | ||
Comment on lines
+313
to
+314
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice clean-up! |
||
maybe_color_bp(bp, **kwds) | ||
|
||
# Return axes in multiplot case, maybe revisit later # 985 | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -218,6 +218,40 @@ def test_specified_props_kwd(self, props, expected): | |||||
|
||||||
assert result[expected][0].get_color() == "C1" | ||||||
|
||||||
@pytest.mark.parametrize("vert", [(True), (False)]) | ||||||
def test_boxplot_shared_axis(self, vert): | ||||||
# GH 37107 | ||||||
df1 = DataFrame(np.random.random((100, 5)), columns=["A", "B", "C", "D", "E"]) | ||||||
df2 = DataFrame(np.random.random((100, 5)), columns=["A", "B", "C", "D", "E"]) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
ahh, if it's to generate two dfs with different random numbers, ignore, but still, probably one df is enough |
||||||
|
||||||
# Two rows if shared axis is y, two rows if shared axis is y. | ||||||
# This is done so that the shared axes are actually separated | ||||||
# and get_ticklabels returns a non empty list on each ax object | ||||||
nrows, ncols, sharex, sharey = ( | ||||||
(1, 2, True, False) if vert else (2, 1, False, True) | ||||||
) | ||||||
fig, axes = self.plt.subplots( | ||||||
nrows=nrows, ncols=ncols, sharex=sharex, sharey=sharey | ||||||
) | ||||||
df1.boxplot(ax=axes[0], vert=vert, fontsize=10, rot=10) | ||||||
df2.boxplot(ax=axes[1], vert=vert, fontsize=10, rot=10) | ||||||
|
||||||
# In order for the ticklabels to be placed, the plot has to be drawn | ||||||
fig.canvas.draw() | ||||||
|
||||||
for ax in axes: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i might not fully understand, why needs two dfs and two plots (in each of |
||||||
axis = ax.xaxis if vert else ax.yaxis | ||||||
labels = [x.get_text() for x in axis.get_ticklabels(which="major")] | ||||||
if vert: | ||||||
self._check_ticks_props(ax, xlabelsize=10, xrot=10) | ||||||
else: | ||||||
self._check_ticks_props(ax, ylabelsize=10, yrot=10) | ||||||
|
||||||
# Matplotlib returns 10 ticklabels, 5 of which are empty | ||||||
assert len(labels) % 5 == 0 | ||||||
assert len(labels) // (len(labels) // 5) == 5 | ||||||
assert labels[:5] == ["A", "B", "C", "D", "E"] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you maybe simply it a bit? seems we want to test there are 10 labels while the first 5 is those column names, and last 5 are empty? |
||||||
|
||||||
|
||||||
@td.skip_if_no_mpl | ||||||
class TestDataFrameGroupByPlots(TestPlotBase): | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ehh, isn't the original issue about erroring when
vert=False
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, sorry my bad I will correct it