Skip to content

Commit

Permalink
ENH : add flag to box_plot and bxp to manage (or not) xticks
Browse files Browse the repository at this point in the history
Closes matplotlib#2921
 - added test for managa_xtick option to boxplot
 - added CHANGELOG and whats_new.rst entry for manage_xticks
  • Loading branch information
tacaswell committed Apr 4, 2014
1 parent f93ab9b commit 8c9f4d4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
@@ -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
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
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
16 changes: 15 additions & 1 deletion lib/matplotlib/tests/test_axes.py
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 @@ -1460,6 +1460,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

0 comments on commit 8c9f4d4

Please sign in to comment.