Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ENH/REF: Overhauled boxplots #2643

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 44 commits into from
Jan 24, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
8bf8653
REF/TST: split up boxplot function into a calculator (cbook.boxplot_s…
phobson Nov 29, 2013
b429dcf
BUG: fixed cbook tests for Python 2.6
phobson Dec 2, 2013
04937b5
ENH: allow for custom patch artist and labels to the boxplots
phobson Dec 3, 2013
88435bc
WIP: more tests and add option to toggle box
phobson Dec 3, 2013
1002913
DOC/REF: added docstring for axes.bxp and refactored all references t…
phobson Dec 3, 2013
614736d
REF/TST: update boxplot_stats test with outliers changed to fliers
phobson Dec 3, 2013
ec12ef9
MNT: reran boilerplate.py
phobson Dec 3, 2013
ffdbd7d
DOC: added blurb in changelog
phobson Dec 3, 2013
987893c
MNT: got rid of build-breaking `u''` strings in pyplot
phobson Dec 3, 2013
2562b68
MNT: fixed changelog whitespace
phobson Dec 3, 2013
7f937f9
ENH: cbook.boxplot_stats can now set the whiskers to -range- (min/max…
phobson Dec 3, 2013
fc80458
TST: added tests and baseline images for new whisker behavior
phobson Dec 3, 2013
85d91df
ENH: added remaining bxp kwargs to boxplot
phobson Dec 4, 2013
d079ad2
DOC: expanded boxplot_stats docstring
phobson Dec 4, 2013
5a9f610
DOC: added examples
phobson Dec 4, 2013
13937e9
DOC: impoved example boxplot figure layout and confirmed that they bu…
phobson Dec 16, 2013
eed0ce0
REF/TST: split up boxplot function into a calculator (cbook.boxplot_s…
phobson Nov 29, 2013
91f88a2
BUG: fixed cbook tests for Python 2.6
phobson Dec 2, 2013
65a686c
ENH: allow for custom patch artist and labels to the boxplots
phobson Dec 3, 2013
e9c1c9b
WIP: more tests and add option to toggle box
phobson Dec 3, 2013
f8e7215
DOC/REF: added docstring for axes.bxp and refactored all references t…
phobson Dec 3, 2013
5cc858f
REF/TST: update boxplot_stats test with outliers changed to fliers
phobson Dec 3, 2013
e26b943
MNT: reran boilerplate.py
phobson Dec 3, 2013
38368ae
DOC: added blurb in changelog
phobson Dec 3, 2013
0ad290f
MNT: got rid of build-breaking `u''` strings in pyplot
phobson Dec 3, 2013
858964a
MNT: fixed changelog whitespace
phobson Dec 3, 2013
7cf8b35
ENH: cbook.boxplot_stats can now set the whiskers to -range- (min/max…
phobson Dec 3, 2013
d8da483
TST: added tests and baseline images for new whisker behavior
phobson Dec 3, 2013
d29bcd7
ENH: added remaining bxp kwargs to boxplot
phobson Dec 4, 2013
5ded206
DOC: expanded boxplot_stats docstring
phobson Dec 4, 2013
9b97ac4
DOC: added examples
phobson Dec 4, 2013
676f28e
DOC: impoved example boxplot figure layout and confirmed that they bu…
phobson Dec 16, 2013
954b212
DOC: add a blurb in whats_new about the changes to boxplot
phobson Jan 13, 2014
1532b99
Merge conflicts
phobson Jan 13, 2014
d5edad9
removed extra newline in changlog
phobson Jan 13, 2014
eb56c3b
boxplot default style dicts now updated with user input and better sh…
phobson Jan 17, 2014
d7a4a8c
fixed typo in mean style boxplot updating
phobson Jan 17, 2014
1a06081
added some comments to boxplot
phobson Jan 17, 2014
da97eab
cleanup up typos and minor tweaks based on dev team input
phobson Jan 19, 2014
437f5c2
cleaned up whats_new (thanks, efiring)
phobson Jan 19, 2014
f26a402
better shaping checking logic
phobson Jan 19, 2014
f2aea5b
boxplot and bxp docstrings to a more numpy-like format
phobson Jan 19, 2014
9b0118f
fixed usermedian shape logic (again)
phobson Jan 19, 2014
64b1196
reran boilerplate.py
phobson Jan 23, 2014
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
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
argument. A 30x30 grid is now used for both density=1 and
density=(1, 1).

2013-12-03 Added a pure boxplot-drawing method that allow a more complete
customization of boxplots. It takes a list of dicts contains stats.
Also created a function (`cbook.boxplot_stats`) that generates the
stats needed.

2013-11-28 Added qhull extension module to perform Delaunay triangulation more
robustly than before. It is used by tri.Triangulation (and hence
all pyplot.tri* methods) and mlab.griddata. Deprecated
Expand Down
25 changes: 25 additions & 0 deletions doc/users/whats_new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,31 @@ Phil Elson rewrote of the documentation and userguide for both Legend and PathEf
New plotting features
---------------------

Fully customizable boxplots
````````````````````````````
Paul Hobson overhauled the :func:`~matplotlib.pyplot.boxplot` method such
that it is now completely customizable in terms of the styles and positions
of the individual artists. Under the hood, :func:`~matplotlib.pyplot.boxplot`
relies on a new function (:func:`~matplotlib.cbook.boxplot_stats`), which
accepts any data structure currently compatible with
:func:`~matplotlib.pyplot.boxplot`, and returns a list of dictionaries
containing the positions for each element of the boxplots. Then
a second method, :func:`~matplotlib.Axes.bxp` is called to draw the boxplots
based on the stats.

The :func:~matplotlib.pyplot.boxplot function can be used as before to
generate boxplots from data in one step. But now the user has the
flexibility to generate the statistics independently, or to modify the
output of :func:~matplotlib.cbook.boxplot_stats prior to plotting
with :func:~matplotlib.Axes.bxp.

Lastly, each artist (e.g., the box, outliers, cap, notches) can now be
toggled on or off and their styles can be passed in through individual
kwargs. See the examples:
:ref:`~examples/statistics/boxplot_demo.py` and
:ref:`~examples/statistics/bxp_demo.py`


Support for datetime axes in 2d plots
`````````````````````````````````````
Andrew Dawson added support for datetime axes to
Expand Down
77 changes: 77 additions & 0 deletions examples/statistics/boxplot_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
"""
Demo of the new boxplot functionality
"""

import numpy as np
import matplotlib.pyplot as plt

# fake data
np.random.seed(937)
data = np.random.lognormal(size=(37, 4), mean=1.5, sigma=1.75)
labels = list('ABCD')
fs = 10 # fontsize

# demonstrate how to toggle the display of different elements:
fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(6,6))
axes[0, 0].boxplot(data, labels=labels)
axes[0, 0].set_title('Default', fontsize=fs)

axes[0, 1].boxplot(data, labels=labels, showmeans=True)
axes[0, 1].set_title('showmeans=True', fontsize=fs)

axes[0, 2].boxplot(data, labels=labels, showmeans=True, meanline=True)
axes[0, 2].set_title('showmeans=True,\nmeanline=True', fontsize=fs)

axes[1, 0].boxplot(data, labels=labels, showbox=False, showcaps=False)
axes[1, 0].set_title('Tufte Style \n(showbox=False,\nshowcaps=False)', fontsize=fs)

axes[1, 1].boxplot(data, labels=labels, notch=True, bootstrap=10000)
axes[1, 1].set_title('notch=True,\nbootstrap=10000', fontsize=fs)

axes[1, 2].boxplot(data, labels=labels, showfliers=False)
axes[1, 2].set_title('showfliers=False', fontsize=fs)

for ax in axes.flatten():
ax.set_yscale('log')
ax.set_yticklabels([])

fig.subplots_adjust(hspace=0.4)
plt.show()


# demonstrate how to customize the display different elements:
boxprops = dict(linestyle='--', linewidth=3, color='darkgoldenrod')
flierprops = dict(marker='o', markerfacecolor='green', markersize=12,
linestyle='none')
medianprops = dict(linestyle='-.', linewidth=2.5, color='firebrick')
meanpointprops = dict(marker='D', markeredgecolor='black',
markerfacecolor='firebrick')
meanlineprops = dict(linestyle='--', linewidth=2.5, color='purple')

fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(6,6))
axes[0, 0].boxplot(data, boxprops=boxprops)
axes[0, 0].set_title('Custom boxprops', fontsize=fs)

axes[0, 1].boxplot(data, flierprops=flierprops, medianprops=medianprops)
axes[0, 1].set_title('Custom medianprops\nand flierprops', fontsize=fs)

axes[0, 2].boxplot(data, whis='range')
axes[0, 2].set_title('whis="range"', fontsize=fs)

axes[1, 0].boxplot(data, meanprops=meanpointprops, meanline=False,
showmeans=True)
axes[1, 0].set_title('Custom mean\nas point', fontsize=fs)

axes[1, 1].boxplot(data, meanprops=meanlineprops, meanline=True, showmeans=True)
axes[1, 1].set_title('Custom mean\nas line', fontsize=fs)

axes[1, 2].boxplot(data, whis=[15, 85])
axes[1, 2].set_title('whis=[15, 85]\n#percentiles', fontsize=fs)

for ax in axes.flatten():
ax.set_yscale('log')
ax.set_yticklabels([])

fig.suptitle("I never said they'd be pretty")
fig.subplots_adjust(hspace=0.4)
plt.show()
83 changes: 83 additions & 0 deletions examples/statistics/bxp_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""
Demo of the new boxplot drawer function
"""

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook

# fake data
np.random.seed(937)
data = np.random.lognormal(size=(37, 4), mean=1.5, sigma=1.75)
labels = list('ABCD')

# compute the boxplot stats
stats = cbook.boxplot_stats(data, labels=labels, bootstrap=10000)
# After we've computed the stats, we can go through and change anything.
# Just to prove it, I'll set the median of each set to the median of all
# the data, and double the means
for n in range(len(stats)):
stats[n]['med'] = np.median(data)
stats[n]['mean'] *= 2

print(stats[0].keys())
fs = 10 # fontsize

# demonstrate how to toggle the display of different elements:
fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(6,6))
axes[0, 0].bxp(stats)
axes[0, 0].set_title('Default', fontsize=fs)

axes[0, 1].bxp(stats, showmeans=True)
axes[0, 1].set_title('showmeans=True', fontsize=fs)

axes[0, 2].bxp(stats, showmeans=True, meanline=True)
axes[0, 2].set_title('showmeans=True,\nmeanline=True', fontsize=fs)

axes[1, 0].bxp(stats, showbox=False, showcaps=False)
axes[1, 0].set_title('Tufte Style\n(showbox=False,\nshowcaps=False)', fontsize=fs)

axes[1, 1].bxp(stats, shownotches=True)
axes[1, 1].set_title('notch=True', fontsize=fs)

axes[1, 2].bxp(stats, showfliers=False)
axes[1, 2].set_title('showfliers=False', fontsize=fs)

for ax in axes.flatten():
ax.set_yscale('log')
ax.set_yticklabels([])

fig.subplots_adjust(hspace=0.4)
plt.show()


# demonstrate how to customize the display different elements:
boxprops = dict(linestyle='--', linewidth=3, color='darkgoldenrod')
flierprops = dict(marker='o', markerfacecolor='green', markersize=12,
linestyle='none')
medianprops = dict(linestyle='-.', linewidth=2.5, color='firebrick')
meanpointprops = dict(marker='D', markeredgecolor='black',
markerfacecolor='firebrick')
meanlineprops = dict(linestyle='--', linewidth=2.5, color='purple')

fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(6,6))
axes[0, 0].bxp(stats, boxprops=boxprops)
axes[0, 0].set_title('Custom boxprops', fontsize=fs)

axes[0, 1].bxp(stats, flierprops=flierprops, medianprops=medianprops)
axes[0, 1].set_title('Custom medianprops\nand flierprops', fontsize=fs)

axes[1, 0].bxp(stats, meanprops=meanpointprops, meanline=False,
showmeans=True)
axes[1, 0].set_title('Custom mean\nas point', fontsize=fs)

axes[1, 1].bxp(stats, meanprops=meanlineprops, meanline=True, showmeans=True)
axes[1, 1].set_title('Custom mean\nas line', fontsize=fs)

for ax in axes.flatten():
ax.set_yscale('log')
ax.set_yticklabels([])

fig.suptitle("I never said they'd be pretty")
fig.subplots_adjust(hspace=0.4)
plt.show()
Loading