Skip to content

Commit 41cfa2b

Browse files
committed
added boxplot demos
svn path=/trunk/matplotlib/; revision=1099
1 parent 204216e commit 41cfa2b

File tree

10 files changed

+357
-23
lines changed

10 files changed

+357
-23
lines changed

CHANGELOG

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
New entries should be added at the top
22

3+
2005-03-28 Added Jarrod's svg patch for text - JDH
4+
5+
2005-03-28 Added ludal arrow and quiver patch - JDH
6+
7+
2005-03-28 Added label kwarg to Axes to facilitate forcing the
8+
creation of new Axes with otherwise identical attributes
9+
310
2005-03-28 Applied boxplot and OSX font search patches
411

512
2005-03-27 Added ft2font NULL check to fix Japanase font bug - JDH
613

7-
2005-03-27 Added sprint legend patch plus John Gill's tests and fix
8-
- JDH
14+
2005-03-27 Added sprint legend patch plus John Gill's tests and fix --
15+
see examples/legend_auto.py - JDH
916

1017
==========================================================================
1118

TODO

+2-2
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ ZeroDivisionError: SeparableTransformation::eval_scalars yin interval is zero; c
683683

684684
-- direction fields
685685

686-
-- add a force option to the add_axes code.
686+
-- DONE add a force option to the add_axes code.
687687

688688
-- DONE move object inspector into Artist method
689689

@@ -693,4 +693,4 @@ ZeroDivisionError: SeparableTransformation::eval_scalars yin interval is zero; c
693693

694694
-- add a get description to axes which returns the args and kwargs to construct it
695695

696-
-- restore __all__ to pylab and add ArtistInspector\
696+
-- restore __all__ to pylab and add ArtistInspector

boilerplate.py

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ def %(func)s(*args, **kwargs):
7575
'spy2',
7676
'stem',
7777
'vlines',
78+
'quiver',
7879
)
7980

8081
_misccommands = (

examples/boxplot_demo.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/python
2+
3+
#
4+
# Example boxplot code
5+
#
6+
7+
from pylab import *
8+
9+
# fake up some data
10+
spread= rand(50) * 100
11+
center = ones(25) * 50
12+
flier_high = rand(10) * 100 + 100
13+
flier_low = rand(10) * -100
14+
data =concatenate((spread, center, flier_high, flier_low), 0)
15+
16+
# basic plot
17+
boxplot(data)
18+
#savefig('box1')
19+
20+
# notched plot
21+
figure()
22+
boxplot(data,1)
23+
#savefig('box2')
24+
25+
# change outlier point symbols
26+
figure()
27+
boxplot(data,0,'gD')
28+
#savefig('box3')
29+
30+
# don't show outlier points
31+
figure()
32+
boxplot(data,0,'')
33+
#savefig('box4')
34+
35+
# horizontal boxes
36+
figure()
37+
boxplot(data,0,'rs',0)
38+
#savefig('box5')
39+
40+
# change whisker length
41+
figure()
42+
boxplot(data,0,'rs',0,0.75)
43+
#savefig('box6')
44+
45+
# fake up some more data
46+
spread= rand(50) * 100
47+
center = ones(25) * 40
48+
flier_high = rand(10) * 100 + 100
49+
flier_low = rand(10) * -100
50+
d2 = concatenate( (spread, center, flier_high, flier_low), 0 )
51+
data.shape = (-1, 1)
52+
d2.shape = (-1, 1)
53+
data = concatenate( (data, d2), 1 )
54+
55+
# multiple box plots on one figure
56+
figure()
57+
boxplot(data)
58+
#savefig('box7')
59+
60+
show()
61+

examples/legend_auto.py

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
"""
2+
This file was written to test matplotlib's autolegend placement
3+
algorithm, but shows lots of different ways to create legends so is
4+
useful as a general examples
5+
6+
Thanks to John Gill and Phil ?? for help at the matplotlib sprint at
7+
pycon 2005 where the auto-legend support was written.
8+
"""
9+
from pylab import *
10+
import sys
11+
12+
13+
N = 100
14+
x = arange(N)
15+
16+
def fig_1():
17+
figure(1)
18+
t = arange(0, 40.0 * pi, 0.1)
19+
l, = plot(t, 100*sin(t), 'r', label='sine')
20+
legend()
21+
22+
def fig_2():
23+
figure(2)
24+
plot(x, 'o', label='x=y')
25+
legend()
26+
27+
def fig_3():
28+
figure(3)
29+
plot(x, -x, 'o', label='x= -y')
30+
legend()
31+
32+
def fig_4():
33+
figure(4)
34+
plot(x, ones(len(x)), 'o', label='y=1')
35+
plot(x, -ones(len(x)), 'o', label='y=-1')
36+
legend()
37+
38+
def fig_5():
39+
figure(5)
40+
n, bins, patches = hist(randn(1000), 40, normed=1)
41+
l, = plot(bins, normpdf(bins, 0.0, 1.0), 'r--', label='fit', linewidth=3)
42+
legend([l, patches[0]], ['fit', 'hist'])
43+
44+
def fig_6():
45+
figure(6)
46+
plot(x, 50-x, 'o', label='y=1')
47+
plot(x, x-50, 'o', label='y=-1')
48+
legend()
49+
50+
def fig_7():
51+
figure(7)
52+
xx = x - (N/2.0)
53+
plot(xx, (xx*xx)-1225, 'bo', label='$y=x^2$')
54+
plot(xx, 25*xx, 'go', label='$y=25x$')
55+
plot(xx, -25*xx, 'mo', label='$y=-25x$')
56+
legend()
57+
58+
def fig_8():
59+
figure(8)
60+
b1 = bar(x, x, color='m')
61+
b2 = bar(x, x[::-1], color='g')
62+
legend([b1[0], b2[0]], ['up', 'down'])
63+
64+
def fig_9():
65+
figure(9)
66+
b1 = bar(x, -x)
67+
b2 = bar(x, -x[::-1], color='r')
68+
legend([b1[0], b2[0]], ['down', 'up'])
69+
70+
def fig_10():
71+
figure(10)
72+
b1 = bar(x, x, bottom=-100, color='m')
73+
b2 = bar(x, x[::-1], bottom=-100, color='g')
74+
b3 = bar(x, -x, bottom=100)
75+
b4 = bar(x, -x[::-1], bottom=100, color='r')
76+
legend([b1[0], b2[0], b3[0], b4[0]], ['bottom right', 'bottom left',
77+
'top left', 'top right'])
78+
79+
if __name__ == '__main__':
80+
nfigs = 10
81+
figures = [int(f) for f in sys.argv[1:]]
82+
if len(figures) == 0:
83+
figures = range(1, nfigs+1)
84+
85+
for fig in figures:
86+
fn_name = "fig_%d" % fig
87+
fn = globals()[fn_name]
88+
fn()
89+
90+
show()

lib/matplotlib/axes.py

+131-5
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
from numerix import absolute, arange, array, asarray, ones, divide,\
66
transpose, log, log10, Float, Float32, ravel, zeros,\
77
Int16, Int32, Int, Float64, ceil, indices, \
8-
shape, which, where, sqrt, asum, compress
8+
shape, which, where, sqrt, asum, compress, maximum, minimum
99

1010
import matplotlib.mlab
1111
from artist import Artist
1212
from axis import XAxis, YAxis
1313
from cbook import iterable, is_string_like, flatten, enumerate, \
1414
allequal, dict_delall, strip_math, popd, popall, silent_list
1515
from collections import RegularPolyCollection, PolyCollection, LineCollection
16-
from colors import colorConverter, normalize, Colormap, LinearSegmentedColormap
16+
from colors import colorConverter, normalize, Colormap, LinearSegmentedColormap, looks_like_color
1717
import cm
1818
#from cm import ColormapJet, Grayscale, ScalarMappable
1919
from cm import ScalarMappable
@@ -31,7 +31,7 @@
3131
from matplotlib.numerix.mlab import flipud, amin, amax
3232

3333
from matplotlib import rcParams
34-
from patches import Patch, Rectangle, Circle, Polygon, Wedge, Shadow, bbox_artist
34+
from patches import Patch, Rectangle, Circle, Polygon, Arrow, Wedge, Shadow, bbox_artist
3535
from table import Table
3636
from text import Text, _process_text_args
3737
from transforms import Bbox, Point, Value, Affine, NonseparableTransformation
@@ -315,13 +315,14 @@ def __init__(self, fig, rect,
315315
frameon = True,
316316
sharex=None, # use Axes instance's xaxis info
317317
sharey=None, # use Axes instance's yaxis info
318+
label='',
318319
):
319320
Artist.__init__(self)
320321
self._position = map(makeValue, rect)
321322
# must be set before set_figure
322323
self._sharex = sharex
323324
self._sharey = sharey
324-
325+
self.set_label(label)
325326
self.set_figure(fig)
326327

327328
# this call may differ for non-sep axes, eg polar
@@ -758,6 +759,131 @@ def autoscale_view(self):
758759
locator = self.yaxis.get_major_locator()
759760
self.set_ylim(locator.autoscale())
760761

762+
763+
def quiver(self, U, V, *args, **kwargs ):
764+
"""
765+
QUIVER( X, Y, U, V )
766+
QUIVER( U, V )
767+
QUIVER( X, Y, U, V, S)
768+
QUIVER( U, V, S )
769+
QUIVER( ..., color=None, width=1.0, cmap=None,norm=None )
770+
771+
Make a vector plot (U, V) with arrows on a grid (X, Y)
772+
773+
The optional arguments color and width are used to specify the color and width
774+
of the arrow. color can be an array of colors in which case the arrows can be
775+
colored according to another dataset.
776+
777+
If cm is specied and color is None, the colormap is used to give a color
778+
according to the vector's length.
779+
780+
If color is a scalar field, the colormap is used to map the scalar to a color
781+
If a colormap is specified and color is an array of color triplets, then the
782+
colormap is ignored
783+
784+
width is a scalar that controls the width of the arrows
785+
786+
if S is specified it is used to scale the vectors. Use S=0 to disable automatic
787+
scaling.
788+
If S!=0, vectors are scaled to fit within the grid and then are multiplied by S.
789+
790+
791+
"""
792+
if not self._hold: self.cla()
793+
do_scale = True
794+
S = 1.0
795+
if len(args)==0:
796+
# ( U, V )
797+
U = asarray(U)
798+
V = asarray(V)
799+
X,Y = meshgrid( arange(U.shape[0]), arange(U.shape[1]) )
800+
elif len(args)==1:
801+
# ( U, V, S )
802+
U = asarray(U)
803+
V = asarray(V)
804+
X,Y = meshgrid( arange(U.shape[0]), arange(U.shape[1]) )
805+
S = float(args[0])
806+
do_scale = ( S != 0.0 )
807+
elif len(args)==2:
808+
# ( X, Y, U, V )
809+
X = asarray(U)
810+
Y = asarray(V)
811+
U = asarray(args[0])
812+
V = asarray(args[1])
813+
elif len(args)==3:
814+
# ( X, Y, U, V )
815+
X = asarray(U)
816+
Y = asarray(V)
817+
U = asarray(args[0])
818+
V = asarray(args[1])
819+
S = float(args[2])
820+
do_scale = ( S != 0.0 )
821+
822+
assert U.shape == V.shape
823+
assert X.shape == Y.shape
824+
assert U.shape == X.shape
825+
826+
arrows = []
827+
N = sqrt( U**2+V**2 )
828+
if do_scale:
829+
Nmax = maximum.reduce(maximum.reduce(N))
830+
U *= (S/Nmax)
831+
V *= (S/Nmax)
832+
N /= Nmax
833+
834+
alpha = kwargs.get('alpha', 1.0)
835+
width = kwargs.get('width', 0.25)
836+
norm = kwargs.get('norm', None)
837+
cmap = kwargs.get('cmap', None)
838+
vmin = kwargs.get('vmin', None)
839+
vmax = kwargs.get('vmax', None)
840+
color = kwargs.get('color', None)
841+
shading = kwargs.get('shading', 'faceted')
842+
843+
C = None
844+
I,J = U.shape
845+
if color is not None and not looks_like_color(color):
846+
clr = asarray(color)
847+
if clr.shape==U.shape:
848+
C = array([ clr[i,j] for i in xrange(I) for j in xrange(J)])
849+
elif clr.shape == () and color:
850+
# a scalar (1, True,...)
851+
C = array([ N[i,j] for i in xrange(I) for j in xrange(J)])
852+
else:
853+
color = (0.,0.,0.,1.)
854+
elif color is None:
855+
color = (0.,0.,0.,1.)
856+
else:
857+
color = colorConverter.to_rgba( color, alpha )
858+
859+
860+
arrows = [ Arrow(X[i,j],Y[i,j],U[i,j],V[i,j],0.1*S ).get_verts()
861+
for i in xrange(I) for j in xrange(J) ]
862+
collection = PolyCollection(
863+
arrows,
864+
edgecolors = 'None',
865+
facecolors = (color,),
866+
antialiaseds = (0,),
867+
linewidths = (width,),
868+
)
869+
if C:
870+
collection.set_array( C )
871+
else:
872+
collection.set_facecolor( (color,) )
873+
collection.set_cmap(cmap)
874+
collection.set_norm(norm)
875+
if norm is not None:
876+
collection.set_clim( vmin, vmax )
877+
self.add_collection( collection )
878+
lims = asarray(arrows)
879+
_max = maximum.reduce( maximum.reduce( lims ))
880+
_min = minimum.reduce( minimum.reduce( lims ))
881+
self.update_datalim( [ tuple(_min), tuple(_max) ] )
882+
self.autoscale_view()
883+
return arrows
884+
885+
886+
761887
def bar(self, left, height, width=0.8, bottom=0,
762888
color='b', yerr=None, xerr=None, ecolor='k', capsize=3
763889
):
@@ -882,7 +1008,7 @@ def boxplot(self, x, notch=0, sym='b+', vert=1, whis=1.5):
8821008
# if we've got a vector, reshape it
8831009
rank = len(x.shape)
8841010
if 1 == rank:
885-
x.shape(-1, 1)
1011+
x.shape = -1, 1
8861012

8871013
row, col = x.shape
8881014

0 commit comments

Comments
 (0)