Skip to content

ENH : add flag to box_plot and bxp to manage (or not) xticks #2922

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

Merged
merged 2 commits into from
Apr 27, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
2014-03-27 Added tests for pie ccw parameter. Removed pdf and svg images
from tests for pie linewidth parameter.

2014-03-24 Added bool kwarg (manage_xticks) to boxplot to enable/disable
the managemnet of the xlimits and ticks when making a boxplot.
Default in True which maintains current behavior by default.

2014-03-22 Added the keyword arguments wedgeprops and textprops to pie.
Users can control the wedge and text properties of the pie
in more detail, if they choose.
Expand Down
2 changes: 2 additions & 0 deletions doc/users/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ kwargs. See the examples:
:ref:`~examples/statistics/boxplot_demo.py` and
:ref:`~examples/statistics/bxp_demo.py`

Added a bool kwarg, `manage_xticks`, which if False disables the management
of the xtick and xlim by `boxplot`.

Support for datetime axes in 2d plots
`````````````````````````````````````
Expand Down
22 changes: 14 additions & 8 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2875,7 +2875,8 @@ def boxplot(self, x, notch=False, sym='b+', vert=True, whis=1.5,
bootstrap=None, usermedians=None, conf_intervals=None,
meanline=False, showmeans=False, showcaps=True,
showbox=True, showfliers=True, boxprops=None, labels=None,
flierprops=None, medianprops=None, meanprops=None):
flierprops=None, medianprops=None, meanprops=None,
manage_xticks=True):
"""
Make a box and whisker plot.

Expand Down Expand Up @@ -3060,14 +3061,15 @@ def boxplot(self, x, notch=False, sym='b+', vert=True, whis=1.5,
showcaps=showcaps, showbox=showbox,
boxprops=boxprops, flierprops=flierprops,
medianprops=medianprops, meanprops=meanprops,
meanline=meanline, showfliers=showfliers)
meanline=meanline, showfliers=showfliers,
manage_xticks=manage_xticks)
return artists

def bxp(self, bxpstats, positions=None, widths=None, vert=True,
patch_artist=False, shownotches=False, showmeans=False,
showcaps=True, showbox=True, showfliers=True,
boxprops=None, flierprops=None, medianprops=None,
meanprops=None, meanline=False):
meanprops=None, meanline=False, manage_xticks=True):
"""
Drawing function for box and whisker plots.

Expand All @@ -3077,7 +3079,7 @@ def bxp(self, bxpstats, positions=None, widths=None, vert=True,
patch_artist=False, shownotches=False, showmeans=False,
showcaps=True, showbox=True, showfliers=True,
boxprops=None, flierprops=None, medianprops=None,
meanprops=None, meanline=False)
meanprops=None, meanline=False, manage_xticks=True)

Make a box and whisker plot for each column of *x* or each
vector in sequence *x*. The box extends from the lower to
Expand Down Expand Up @@ -3156,6 +3158,9 @@ def bxp(self, bxpstats, positions=None, widths=None, vert=True,
*meanprops*. Not recommended if *shownotches* is also True.
Otherwise, means will be shown as points.

manage_xticks : bool, default = True
If the function should adjust the xlim and xtick locations.

Returns
-------

Expand Down Expand Up @@ -3390,10 +3395,11 @@ def dopatch(xs, ys, **kwargs):
setlim = self.set_ylim
setlabels = self.set_yticklabels

newlimits = min(positions) - 0.5, max(positions) + 0.5
setlim(newlimits)
setticks(positions)
setlabels(datalabels)
if manage_xticks:
newlimits = min(positions) - 0.5, max(positions) + 0.5
setlim(newlimits)
setticks(positions)
setlabels(datalabels)

# reset hold status
self.hold(holdStatus)
Expand Down
19 changes: 16 additions & 3 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import matplotlib
from matplotlib.testing.decorators import image_comparison, cleanup
import matplotlib.pyplot as plt

from numpy.testing import assert_array_equal

@image_comparison(baseline_images=['formatter_ticker_001',
'formatter_ticker_002',
Expand Down Expand Up @@ -1433,9 +1433,8 @@ def test_boxplot_bad_medians_1():
fig, ax = plt.subplots()
assert_raises(ValueError, ax.boxplot, x, usermedians=[1, 2])


@cleanup
def test_boxplot_bad_medians_1():
def test_boxplot_bad_medians_2():
x = np.linspace(-7, 7, 140)
x = np.hstack([-25, x, 25])
fig, ax = plt.subplots()
Expand All @@ -1460,6 +1459,20 @@ def test_boxplot_bad_ci_2():
conf_intervals=[[1, 2], [1]])


@cleanup
def test_manage_xticks():
_, ax = plt.subplots()
ax.set_xlim(0, 4)
old_xlim = ax.get_xlim()
np.random.seed(0)
y1 = np.random.normal(10, 3, 20)
y2 = np.random.normal(3, 1, 20)
ax.boxplot([y1, y2], positions = [1,2],
manage_xticks=False)
new_xlim = ax.get_xlim()
assert_array_equal(old_xlim, new_xlim)


@image_comparison(baseline_images=['errorbar_basic', 'errorbar_mixed'])
def test_errorbar():
x = np.arange(0.1, 4, 0.5)
Expand Down