|
5 | 5 | from numerix import absolute, arange, array, asarray, ones, divide,\
|
6 | 6 | transpose, log, log10, Float, Float32, ravel, zeros,\
|
7 | 7 | Int16, Int32, Int, Float64, ceil, indices, \
|
8 |
| - shape, which, where, sqrt, asum, compress |
| 8 | + shape, which, where, sqrt, asum, compress, maximum, minimum |
9 | 9 |
|
10 | 10 | import matplotlib.mlab
|
11 | 11 | from artist import Artist
|
12 | 12 | from axis import XAxis, YAxis
|
13 | 13 | from cbook import iterable, is_string_like, flatten, enumerate, \
|
14 | 14 | allequal, dict_delall, strip_math, popd, popall, silent_list
|
15 | 15 | 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 |
17 | 17 | import cm
|
18 | 18 | #from cm import ColormapJet, Grayscale, ScalarMappable
|
19 | 19 | from cm import ScalarMappable
|
|
31 | 31 | from matplotlib.numerix.mlab import flipud, amin, amax
|
32 | 32 |
|
33 | 33 | 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 |
35 | 35 | from table import Table
|
36 | 36 | from text import Text, _process_text_args
|
37 | 37 | from transforms import Bbox, Point, Value, Affine, NonseparableTransformation
|
@@ -315,13 +315,14 @@ def __init__(self, fig, rect,
|
315 | 315 | frameon = True,
|
316 | 316 | sharex=None, # use Axes instance's xaxis info
|
317 | 317 | sharey=None, # use Axes instance's yaxis info
|
| 318 | + label='', |
318 | 319 | ):
|
319 | 320 | Artist.__init__(self)
|
320 | 321 | self._position = map(makeValue, rect)
|
321 | 322 | # must be set before set_figure
|
322 | 323 | self._sharex = sharex
|
323 | 324 | self._sharey = sharey
|
324 |
| - |
| 325 | + self.set_label(label) |
325 | 326 | self.set_figure(fig)
|
326 | 327 |
|
327 | 328 | # this call may differ for non-sep axes, eg polar
|
@@ -758,6 +759,131 @@ def autoscale_view(self):
|
758 | 759 | locator = self.yaxis.get_major_locator()
|
759 | 760 | self.set_ylim(locator.autoscale())
|
760 | 761 |
|
| 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 | + |
761 | 887 | def bar(self, left, height, width=0.8, bottom=0,
|
762 | 888 | color='b', yerr=None, xerr=None, ecolor='k', capsize=3
|
763 | 889 | ):
|
@@ -882,7 +1008,7 @@ def boxplot(self, x, notch=0, sym='b+', vert=1, whis=1.5):
|
882 | 1008 | # if we've got a vector, reshape it
|
883 | 1009 | rank = len(x.shape)
|
884 | 1010 | if 1 == rank:
|
885 |
| - x.shape(-1, 1) |
| 1011 | + x.shape = -1, 1 |
886 | 1012 |
|
887 | 1013 | row, col = x.shape
|
888 | 1014 |
|
|
0 commit comments