Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 1242 lines (1007 sloc) 38.473 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
from __future__ import division, print_function
import re
import warnings
import matplotlib
import matplotlib.cbook as cbook
from matplotlib import docstring, rcParams
from transforms import Bbox, IdentityTransform, TransformedBbox, \
                       TransformedPath, Transform
from path import Path

## Note, matplotlib artists use the doc strings for set and get
# methods to enable the introspection methods of setp and getp. Every
# set_* method should have a docstring containing the line
#
# ACCEPTS: [ legal | values ]
#
# and aliases for setters and getters should have a docstring that
# starts with 'alias for ', as in 'alias for set_somemethod'
#
# You may wonder why we use so much boiler-plate manually defining the
# set_alias and get_alias functions, rather than using some clever
# python trick. The answer is that I need to be able to manipulate
# the docstring, and there is no clever way to do that in python 2.2,
# as far as I can see - see
# http://groups.google.com/groups?hl=en&lr=&threadm=mailman.5090.1098044946.5135.python-list%40python.org&rnum=1&prev=/groups%3Fq%3D__doc__%2Bauthor%253Ajdhunter%2540ace.bsd.uchicago.edu%26hl%3Den%26btnG%3DGoogle%2BSearch


def allow_rasterization(draw):
    """
Decorator for Artist.draw method. Provides routines
that run before and after the draw call. The before and after functions
are useful for changing artist-dependant renderer attributes or making
other setup function calls, such as starting and flushing a mixed-mode
renderer.
"""
    def before(artist, renderer):
        if artist.get_rasterized():
            renderer.start_rasterizing()

        if artist.get_agg_filter() is not None:
            renderer.start_filter()

    def after(artist, renderer):

        if artist.get_agg_filter() is not None:
            renderer.stop_filter(artist.get_agg_filter())

        if artist.get_rasterized():
            renderer.stop_rasterizing()

    # the axes class has a second argument inframe for its draw method.
    def draw_wrapper(artist, renderer, *args, **kwargs):
        before(artist, renderer)
        draw(artist, renderer, *args, **kwargs)
        after(artist, renderer)

    # "safe wrapping" to exactly replicate anything we haven't overridden above
    draw_wrapper.__name__ = draw.__name__
    draw_wrapper.__dict__ = draw.__dict__
    draw_wrapper.__doc__ = draw.__doc__
    draw_wrapper._supports_rasterization = True
    return draw_wrapper


class Artist(object):
    """
Abstract base class for someone who renders into a
:class:`FigureCanvas`.
"""

    aname = 'Artist'
    zorder = 0

    def __init__(self):
        self.figure = None

        self._transform = None
        self._transformSet = False
        self._visible = True
        self._animated = False
        self._alpha = None
        self.clipbox = None
        self._clippath = None
        self._clipon = True
        self._lod = False
        self._label = ''
        self._picker = None
        self._contains = None
        self._rasterized = None
        self._agg_filter = None

        self.eventson = False # fire events only if eventson
        self._oid = 0 # an observer id
        self._propobservers = {} # a dict from oids to funcs
        try:
            self.axes = None
        except AttributeError:
            # Handle self.axes as a read-only property, as in Figure.
            pass
        self._remove_method = None
        self._url = None
        self._gid = None
        self._snap = None

    def __getstate__(self):
        d = self.__dict__.copy()
        # remove the unpicklable remove method, this will get re-added on load
        # (by the axes) if the artist lives on an axes.
        d['_remove_method'] = None
        return d

    def remove(self):
        """
Remove the artist from the figure if possible. The effect
will not be visible until the figure is redrawn, e.g., with
:meth:`matplotlib.axes.Axes.draw_idle`. Call
:meth:`matplotlib.axes.Axes.relim` to update the axes limits
if desired.

Note: :meth:`~matplotlib.axes.Axes.relim` will not see
collections even if the collection was added to axes with
*autolim* = True.

Note: there is no support for removing the artist's legend entry.
"""

        # There is no method to set the callback. Instead the parent should
        # set the _remove_method attribute directly. This would be a
        # protected attribute if Python supported that sort of thing. The
        # callback has one parameter, which is the child to be removed.
        if self._remove_method is not None:
            self._remove_method(self)
        else:
            raise NotImplementedError('cannot remove artist')
        # TODO: the fix for the collections relim problem is to move the
        # limits calculation into the artist itself, including the property of
        # whether or not the artist should affect the limits. Then there will
        # be no distinction between axes.add_line, axes.add_patch, etc.
        # TODO: add legend support

    def have_units(self):
        'Return *True* if units are set on the *x* or *y* axes'
        ax = self.axes
        if ax is None or ax.xaxis is None:
            return False
        return ax.xaxis.have_units() or ax.yaxis.have_units()

    def convert_xunits(self, x):
        """For artists in an axes, if the xaxis has units support,
convert *x* using xaxis unit type
"""
        ax = getattr(self, 'axes', None)
        if ax is None or ax.xaxis is None:
            #print 'artist.convert_xunits no conversion: ax=%s'%ax
            return x
        return ax.xaxis.convert_units(x)

    def convert_yunits(self, y):
        """For artists in an axes, if the yaxis has units support,
convert *y* using yaxis unit type
"""
        ax = getattr(self, 'axes', None)
        if ax is None or ax.yaxis is None:
            return y
        return ax.yaxis.convert_units(y)

    def set_axes(self, axes):
        """
Set the :class:`~matplotlib.axes.Axes` instance in which the
artist resides, if any.

ACCEPTS: an :class:`~matplotlib.axes.Axes` instance
"""
        self.axes = axes

    def get_axes(self):
        """
Return the :class:`~matplotlib.axes.Axes` instance the artist
resides in, or *None*
"""
        return self.axes

    def add_callback(self, func):
        """
Adds a callback function that will be called whenever one of
the :class:`Artist`'s properties changes.

Returns an *id* that is useful for removing the callback with
:meth:`remove_callback` later.
"""
        oid = self._oid
        self._propobservers[oid] = func
        self._oid += 1
        return oid

    def remove_callback(self, oid):
        """
Remove a callback based on its *id*.

.. seealso::

:meth:`add_callback`
For adding callbacks

"""
        try:
            del self._propobservers[oid]
        except KeyError:
            pass

    def pchanged(self):
        """
Fire an event when property changed, calling all of the
registered callbacks.
"""
        for oid, func in self._propobservers.iteritems():
            func(self)

    def is_transform_set(self):
        """
Returns *True* if :class:`Artist` has a transform explicitly
set.
"""
        return self._transformSet

    def set_transform(self, t):
        """
Set the :class:`~matplotlib.transforms.Transform` instance
used by this artist.

ACCEPTS: :class:`~matplotlib.transforms.Transform` instance
"""
        self._transform = t
        self._transformSet = t is not None
        self.pchanged()

    def get_transform(self):
        """
Return the :class:`~matplotlib.transforms.Transform`
instance used by this artist.
"""
        if self._transform is None:
            self._transform = IdentityTransform()
        elif (not isinstance(self._transform, Transform)
              and hasattr(self._transform, '_as_mpl_transform')):
            self._transform = self._transform._as_mpl_transform(self.axes)
        return self._transform

    def hitlist(self, event):
        """
List the children of the artist which contain the mouse event *event*.
"""
        L = []
        try:
            hascursor, info = self.contains(event)
            if hascursor:
                L.append(self)
        except:
            import traceback
            traceback.print_exc()
            print("while checking", self.__class__)

        for a in self.get_children():
            L.extend(a.hitlist(event))
        return L

    def get_children(self):
        """
Return a list of the child :class:`Artist`s this
:class:`Artist` contains.
"""
        return []

    def contains(self, mouseevent):
        """Test whether the artist contains the mouse event.

Returns the truth value and a dictionary of artist specific details of
selection, such as which points are contained in the pick radius. See
individual artists for details.
"""
        if callable(self._contains):
            return self._contains(self, mouseevent)
        warnings.warn("'%s' needs 'contains' method" % self.__class__.__name__)
        return False, {}

    def set_contains(self, picker):
        """
Replace the contains test used by this artist. The new picker
should be a callable function which determines whether the
artist is hit by the mouse event::

hit, props = picker(artist, mouseevent)

If the mouse event is over the artist, return *hit* = *True*
and *props* is a dictionary of properties you want returned
with the contains test.

ACCEPTS: a callable function
"""
        self._contains = picker

    def get_contains(self):
        """
Return the _contains test used by the artist, or *None* for default.
"""
        return self._contains

    def pickable(self):
        'Return *True* if :class:`Artist` is pickable.'
        return (self.figure is not None and
                self.figure.canvas is not None and
                self._picker is not None)

    def pick(self, mouseevent):
        """
call signature::

pick(mouseevent)

each child artist will fire a pick event if *mouseevent* is over
the artist and the artist has picker set
"""
        # Pick self
        if self.pickable():
            picker = self.get_picker()
            if callable(picker):
                inside, prop = picker(self, mouseevent)
            else:
                inside, prop = self.contains(mouseevent)
            if inside:
                self.figure.canvas.pick_event(mouseevent, self, **prop)

        # Pick children
        for a in self.get_children():
            # make sure the event happened in the same axes
            ax = getattr(a, 'axes', None)
            if mouseevent.inaxes is None or mouseevent.inaxes == ax:
                # we need to check if mouseevent.inaxes is None
                # because some objects associated with an axes (eg a
                # tick label) can be outside the bounding box of the
                # axes and inaxes will be None
                a.pick(mouseevent)

    def set_picker(self, picker):
        """
Set the epsilon for picking used by this artist

*picker* can be one of the following:

* *None*: picking is disabled for this artist (default)

* A boolean: if *True* then picking will be enabled and the
artist will fire a pick event if the mouse event is over
the artist

* A float: if picker is a number it is interpreted as an
epsilon tolerance in points and the artist will fire
off an event if it's data is within epsilon of the mouse
event. For some artists like lines and patch collections,
the artist may provide additional data to the pick event
that is generated, e.g. the indices of the data within
epsilon of the pick event

* A function: if picker is callable, it is a user supplied
function which determines whether the artist is hit by the
mouse event::

hit, props = picker(artist, mouseevent)

to determine the hit test. if the mouse event is over the
artist, return *hit=True* and props is a dictionary of
properties you want added to the PickEvent attributes.

ACCEPTS: [None|float|boolean|callable]
"""
        self._picker = picker

    def get_picker(self):
        'Return the picker object used by this artist'
        return self._picker

    def is_figure_set(self):
        """
Returns True if the artist is assigned to a
:class:`~matplotlib.figure.Figure`.
"""
        return self.figure is not None

    def get_url(self):
        """
Returns the url
"""
        return self._url

    def set_url(self, url):
        """
Sets the url for the artist

ACCEPTS: a url string
"""
        self._url = url

    def get_gid(self):
        """
Returns the group id
"""
        return self._gid

    def set_gid(self, gid):
        """
Sets the (group) id for the artist

ACCEPTS: an id string
"""
        self._gid = gid

    def get_snap(self):
        """
Returns the snap setting which may be:

* True: snap vertices to the nearest pixel center

* False: leave vertices as-is

* None: (auto) If the path contains only rectilinear line
segments, round to the nearest pixel center

Only supported by the Agg and MacOSX backends.
"""
        if rcParams['path.snap']:
            return self._snap
        else:
            return False

    def set_snap(self, snap):
        """
Sets the snap setting which may be:

* True: snap vertices to the nearest pixel center

* False: leave vertices as-is

* None: (auto) If the path contains only rectilinear line
segments, round to the nearest pixel center

Only supported by the Agg and MacOSX backends.
"""
        self._snap = snap

    def get_figure(self):
        """
Return the :class:`~matplotlib.figure.Figure` instance the
artist belongs to.
"""
        return self.figure

    def set_figure(self, fig):
        """
Set the :class:`~matplotlib.figure.Figure` instance the artist
belongs to.

ACCEPTS: a :class:`matplotlib.figure.Figure` instance
"""
        self.figure = fig
        self.pchanged()

    def set_clip_box(self, clipbox):
        """
Set the artist's clip :class:`~matplotlib.transforms.Bbox`.

ACCEPTS: a :class:`matplotlib.transforms.Bbox` instance
"""
        self.clipbox = clipbox
        self.pchanged()

    def set_clip_path(self, path, transform=None):
        """
Set the artist's clip path, which may be:

* a :class:`~matplotlib.patches.Patch` (or subclass) instance

* a :class:`~matplotlib.path.Path` instance, in which case
an optional :class:`~matplotlib.transforms.Transform`
instance may be provided, which will be applied to the
path before using it for clipping.

* *None*, to remove the clipping path

For efficiency, if the path happens to be an axis-aligned
rectangle, this method will set the clipping box to the
corresponding rectangle and set the clipping path to *None*.

ACCEPTS: [ (:class:`~matplotlib.path.Path`,
:class:`~matplotlib.transforms.Transform`) |
:class:`~matplotlib.patches.Patch` | None ]
"""
        from matplotlib.patches import Patch, Rectangle

        success = False
        if transform is None:
            if isinstance(path, Rectangle):
                self.clipbox = TransformedBbox(Bbox.unit(),
                                              path.get_transform())
                self._clippath = None
                success = True
            elif isinstance(path, Patch):
                self._clippath = TransformedPath(
                    path.get_path(),
                    path.get_transform())
                success = True
            elif isinstance(path, tuple):
                path, transform = path

        if path is None:
            self._clippath = None
            success = True
        elif isinstance(path, Path):
            self._clippath = TransformedPath(path, transform)
            success = True
        elif isinstance(path, TransformedPath):
            self._clippath = path
            success = True

        if not success:
            print(type(path), type(transform))
            raise TypeError("Invalid arguments to set_clip_path")

        self.pchanged()

    def get_alpha(self):
        """
Return the alpha value used for blending - not supported on all
backends
"""
        return self._alpha

    def get_visible(self):
        "Return the artist's visiblity"
        return self._visible

    def get_animated(self):
        "Return the artist's animated state"
        return self._animated

    def get_clip_on(self):
        'Return whether artist uses clipping'
        return self._clipon

    def get_clip_box(self):
        'Return artist clipbox'
        return self.clipbox

    def get_clip_path(self):
        'Return artist clip path'
        return self._clippath

    def get_transformed_clip_path_and_affine(self):
        '''
Return the clip path with the non-affine part of its
transformation applied, and the remaining affine part of its
transformation.
'''
        if self._clippath is not None:
            return self._clippath.get_transformed_path_and_affine()
        return None, None

    def set_clip_on(self, b):
        """
Set whether artist uses clipping.

ACCEPTS: [True | False]
"""
        self._clipon = b
        self.pchanged()

    def _set_gc_clip(self, gc):
        'Set the clip properly for the gc'
        if self._clipon:
            if self.clipbox is not None:
                gc.set_clip_rectangle(self.clipbox)
            gc.set_clip_path(self._clippath)
        else:
            gc.set_clip_rectangle(None)
            gc.set_clip_path(None)

    def get_rasterized(self):
        "return True if the artist is to be rasterized"
        return self._rasterized

    def set_rasterized(self, rasterized):
        """
Force rasterized (bitmap) drawing in vector backend output.

Defaults to None, which implies the backend's default behavior

ACCEPTS: [True | False | None]
"""
        if rasterized and not hasattr(self.draw, "_supports_rasterization"):
            warnings.warn("Rasterization of '%s' will be ignored" % self)

        self._rasterized = rasterized

    def get_agg_filter(self):
        "return filter function to be used for agg filter"
        return self._agg_filter

    def set_agg_filter(self, filter_func):
        """
set agg_filter fuction.

"""
        self._agg_filter = filter_func

    def draw(self, renderer, *args, **kwargs):
        'Derived classes drawing method'
        if not self.get_visible():
            return

    def set_alpha(self, alpha):
        """
Set the alpha value used for blending - not supported on
all backends.

ACCEPTS: float (0.0 transparent through 1.0 opaque)
"""
        self._alpha = alpha
        self.pchanged()

    def set_lod(self, on):
        """
Set Level of Detail on or off. If on, the artists may examine
things like the pixel width of the axes and draw a subset of
their contents accordingly

ACCEPTS: [True | False]
"""
        self._lod = on
        self.pchanged()

    def set_visible(self, b):
        """
Set the artist's visiblity.

ACCEPTS: [True | False]
"""
        self._visible = b
        self.pchanged()

    def set_animated(self, b):
        """
Set the artist's animation state.

ACCEPTS: [True | False]
"""
        self._animated = b
        self.pchanged()

    def update(self, props):
        """
Update the properties of this :class:`Artist` from the
dictionary *prop*.
"""
        store = self.eventson
        self.eventson = False
        changed = False
        for k, v in props.iteritems():
            func = getattr(self, 'set_' + k, None)
            if func is None or not callable(func):
                raise AttributeError('Unknown property %s' % k)
            func(v)
            changed = True
        self.eventson = store
        if changed:
            self.pchanged()

    def get_label(self):
        """
Get the label used for this artist in the legend.
"""
        return self._label

    def set_label(self, s):
        """
Set the label to *s* for auto legend.

ACCEPTS: string or anything printable with '%s' conversion.
"""
        self._label = '%s' % (s, )
        self.pchanged()

    def get_zorder(self):
        """
Return the :class:`Artist`'s zorder.
"""
        return self.zorder

    def set_zorder(self, level):
        """
Set the zorder for the artist. Artists with lower zorder
values are drawn first.

ACCEPTS: any number
"""
        self.zorder = level
        self.pchanged()

    def update_from(self, other):
        'Copy properties from *other* to *self*.'
        self._transform = other._transform
        self._transformSet = other._transformSet
        self._visible = other._visible
        self._alpha = other._alpha
        self.clipbox = other.clipbox
        self._clipon = other._clipon
        self._clippath = other._clippath
        self._lod = other._lod
        self._label = other._label
        self.pchanged()

    def properties(self):
        """
return a dictionary mapping property name -> value for all Artist props
"""
        return ArtistInspector(self).properties()

    def set(self, **kwargs):
        """
A tkstyle set command, pass *kwargs* to set properties
"""
        ret = []
        for k, v in kwargs.iteritems():
            k = k.lower()
            funcName = "set_%s" % k
            func = getattr(self, funcName)
            ret.extend([func(v)])
        return ret

    def findobj(self, match=None, include_self=True):
        """
Find artist objects.

pyplot signature:
findobj(o=gcf(), match=None, include_self=True)

Recursively find all :class:matplotlib.artist.Artist instances
contained in self.

*match* can be

- None: return all objects contained in artist.

- function with signature ``boolean = match(artist)``
used to filter matches

- class instance: eg Line2D. Only return artists of class type.

If *include_self* is True (default), include self in the list to be
checked for a match.

.. plot:: mpl_examples/pylab_examples/findobj_demo.py
"""

        if match is None: # always return True
            def matchfunc(x):
                return True
        elif cbook.issubclass_safe(match, Artist):
            def matchfunc(x):
                return isinstance(x, match)
        elif callable(match):
            matchfunc = match
        else:
            raise ValueError('match must be None, a matplotlib.artist.Artist '
                             'subclass, or a callable')

        artists = []

        for c in self.get_children():
            if matchfunc(c):
                artists.append(c)
            artists.extend([thisc for thisc in
                                c.findobj(matchfunc, include_self=False)
                                                     if matchfunc(thisc)])

        if include_self and matchfunc(self):
            artists.append(self)
        return artists


class ArtistInspector:
    """
A helper class to inspect an :class:`~matplotlib.artist.Artist`
and return information about it's settable properties and their
current values.
"""
    def __init__(self, o):
        """
Initialize the artist inspector with an
:class:`~matplotlib.artist.Artist` or sequence of :class:`Artists`.
If a sequence is used, we assume it is a homogeneous sequence (all
:class:`Artists` are of the same type) and it is your responsibility
to make sure this is so.
"""
        if cbook.iterable(o) and len(o):
            o = o[0]

        self.oorig = o
        if not isinstance(o, type):
            o = type(o)
        self.o = o

        self.aliasd = self.get_aliases()

    def get_aliases(self):
        """
Get a dict mapping *fullname* -> *alias* for each *alias* in
the :class:`~matplotlib.artist.ArtistInspector`.

Eg., for lines::

{'markerfacecolor': 'mfc',
'linewidth' : 'lw',
}

"""
        names = [name for name in dir(self.o) if
                 (name.startswith('set_') or name.startswith('get_'))
                 and callable(getattr(self.o, name))]
        aliases = {}
        for name in names:
            func = getattr(self.o, name)
            if not self.is_alias(func):
                continue
            docstring = func.__doc__
            fullname = docstring[10:]
            aliases.setdefault(fullname[4:], {})[name[4:]] = None
        return aliases

    _get_valid_values_regex = re.compile(
                r"\n\s*ACCEPTS:\s*((?:.|\n)*?)(?:$|(?:\n\n))")

    def get_valid_values(self, attr):
        """
Get the legal arguments for the setter associated with *attr*.

This is done by querying the docstring of the function *set_attr*
for a line that begins with ACCEPTS:

Eg., for a line linestyle, return
"[ ``'-'`` | ``'--'`` | ``'-.'`` | ``':'`` | ``'steps'`` | ``'None'`` ]"
"""

        name = 'set_%s' % attr
        if not hasattr(self.o, name):
            raise AttributeError('%s has no function %s' % (self.o, name))
        func = getattr(self.o, name)

        docstring = func.__doc__
        if docstring is None:
            return 'unknown'

        if docstring.startswith('alias for '):
            return None

        match = self._get_valid_values_regex.search(docstring)
        if match is not None:
            return match.group(1).replace('\n', ' ')
        return 'unknown'

    def _get_setters_and_targets(self):
        """
Get the attribute strings and a full path to where the setter
is defined for all setters in an object.
"""

        setters = []
        for name in dir(self.o):
            if not name.startswith('set_'):
                continue
            o = getattr(self.o, name)
            if not callable(o):
                continue
            func = o
            if self.is_alias(func):
                continue
            source_class = self.o.__module__ + "." + self.o.__name__
            for cls in self.o.mro():
                if name in cls.__dict__:
                    source_class = cls.__module__ + "." + cls.__name__
                    break
            setters.append((name[4:], source_class + "." + name))
        return setters

    def get_setters(self):
        """
Get the attribute strings with setters for object. Eg., for a line,
return ``['markerfacecolor', 'linewidth', ....]``.
"""

        return [prop for prop, target in self._get_setters_and_targets()]

    def is_alias(self, o):
        """
Return *True* if method object *o* is an alias for another
function.
"""
        ds = o.__doc__
        if ds is None:
            return False
        return ds.startswith('alias for ')

    def aliased_name(self, s):
        """
return 'PROPNAME or alias' if *s* has an alias, else return
PROPNAME.

E.g. for the line markerfacecolor property, which has an
alias, return 'markerfacecolor or mfc' and for the transform
property, which does not, return 'transform'
"""

        if s in self.aliasd:
            return s + ''.join([' or %s' % x
                                for x
                                in self.aliasd[s].iterkeys()])
        else:
            return s

    def aliased_name_rest(self, s, target):
        """
return 'PROPNAME or alias' if *s* has an alias, else return
PROPNAME formatted for ReST

E.g. for the line markerfacecolor property, which has an
alias, return 'markerfacecolor or mfc' and for the transform
property, which does not, return 'transform'
"""

        if s in self.aliasd:
            aliases = ''.join([' or %s' % x
                               for x
                               in self.aliasd[s].iterkeys()])
        else:
            aliases = ''
        return ':meth:`%s <%s>`%s' % (s, target, aliases)

    def pprint_setters(self, prop=None, leadingspace=2):
        """
If *prop* is *None*, return a list of strings of all settable properies
and their valid values.

If *prop* is not *None*, it is a valid property name and that
property will be returned as a string of property : valid
values.
"""
        if leadingspace:
            pad = ' ' * leadingspace
        else:
            pad = ''
        if prop is not None:
            accepts = self.get_valid_values(prop)
            return '%s%s: %s' % (pad, prop, accepts)

        attrs = self._get_setters_and_targets()
        attrs.sort()
        lines = []

        for prop, path in attrs:
            accepts = self.get_valid_values(prop)
            name = self.aliased_name(prop)

            lines.append('%s%s: %s' % (pad, name, accepts))
        return lines

    def pprint_setters_rest(self, prop=None, leadingspace=2):
        """
If *prop* is *None*, return a list of strings of all settable properies
and their valid values. Format the output for ReST

If *prop* is not *None*, it is a valid property name and that
property will be returned as a string of property : valid
values.
"""
        if leadingspace:
            pad = ' ' * leadingspace
        else:
            pad = ''
        if prop is not None:
            accepts = self.get_valid_values(prop)
            return '%s%s: %s' % (pad, prop, accepts)

        attrs = self._get_setters_and_targets()
        attrs.sort()
        lines = []

        ########
        names = [self.aliased_name_rest(prop, target)
                 for prop, target
                 in attrs]
        accepts = [self.get_valid_values(prop) for prop, target in attrs]

        col0_len = max([len(n) for n in names])
        col1_len = max([len(a) for a in accepts])
        table_formatstr = pad + '=' * col0_len + ' ' + '=' * col1_len

        lines.append('')
        lines.append(table_formatstr)
        lines.append(pad + 'Property'.ljust(col0_len + 3) + \
                     'Description'.ljust(col1_len))
        lines.append(table_formatstr)

        lines.extend([pad + n.ljust(col0_len + 3) + a.ljust(col1_len)
                      for n, a in zip(names, accepts)])

        lines.append(table_formatstr)
        lines.append('')
        return lines
        ########

        for prop, path in attrs:
            accepts = self.get_valid_values(prop)
            name = self.aliased_name_rest(prop, path)

            lines.append('%s%s: %s' % (pad, name, accepts))
        return lines

    def properties(self):
        """
return a dictionary mapping property name -> value
"""
        o = self.oorig
        getters = [name for name in dir(o)
                   if name.startswith('get_')
                   and callable(getattr(o, name))]
        #print getters
        getters.sort()
        d = dict()
        for name in getters:
            func = getattr(o, name)
            if self.is_alias(func):
                continue

            try:
                val = func()
            except:
                continue
            else:
                d[name[4:]] = val

        return d

    def pprint_getters(self):
        """
Return the getters and actual values as list of strings.
"""

        d = self.properties()
        names = d.keys()
        names.sort()
        lines = []
        for name in names:
            val = d[name]
            if getattr(val, 'shape', ()) != () and len(val) > 6:
                s = str(val[:6]) + '...'
            else:
                s = str(val)
            s = s.replace('\n', ' ')
            if len(s) > 50:
                s = s[:50] + '...'
            name = self.aliased_name(name)
            lines.append(' %s = %s' % (name, s))
        return lines

    def findobj(self, match=None):
        """
Recursively find all :class:`matplotlib.artist.Artist`
instances contained in *self*.

If *match* is not None, it can be

- function with signature ``boolean = match(artist)``

- class instance: eg :class:`~matplotlib.lines.Line2D`

used to filter matches.
"""
        if match is None: # always return True
            def matchfunc(x):
                return True
        elif issubclass(match, Artist):
            def matchfunc(x):
                return isinstance(x, match)
        elif callable(match):
            matchfunc = func
        else:
            raise ValueError('match must be None, an '
                             'matplotlib.artist.Artist '
                             'subclass, or a callable')

        artists = []

        for c in self.get_children():
            if matchfunc(c):
                artists.append(c)
            artists.extend([thisc
                            for thisc
                            in c.findobj(matchfunc)
                            if matchfunc(thisc)])

        if matchfunc(self):
            artists.append(self)
        return artists


def getp(obj, property=None):
    """
Return the value of object's property. *property* is an optional string
for the property you want to return

Example usage::

getp(obj) # get all the object properties
getp(obj, 'linestyle') # get the linestyle property

*obj* is a :class:`Artist` instance, eg
:class:`~matplotllib.lines.Line2D` or an instance of a
:class:`~matplotlib.axes.Axes` or :class:`matplotlib.text.Text`.
If the *property* is 'somename', this function returns

obj.get_somename()

:func:`getp` can be used to query all the gettable properties with
``getp(obj)``. Many properties have aliases for shorter typing, e.g.
'lw' is an alias for 'linewidth'. In the output, aliases and full
property names will be listed as:

property or alias = value

e.g.:

linewidth or lw = 2
"""
    if property is None:
        insp = ArtistInspector(obj)
        ret = insp.pprint_getters()
        print('\n'.join(ret))
        return

    func = getattr(obj, 'get_' + property)
    return func()

# alias
get = getp


def setp(obj, *args, **kwargs):
    """
Set a property on an artist object.

matplotlib supports the use of :func:`setp` ("set property") and
:func:`getp` to set and get object properties, as well as to do
introspection on the object. For example, to set the linestyle of a
line to be dashed, you can do::

>>> line, = plot([1,2,3])
>>> setp(line, linestyle='--')

If you want to know the valid types of arguments, you can provide the
name of the property you want to set without a value::

>>> setp(line, 'linestyle')
linestyle: [ '-' | '--' | '-.' | ':' | 'steps' | 'None' ]

If you want to see all the properties that can be set, and their
possible values, you can do::

>>> setp(line)
... long output listing omitted

:func:`setp` operates on a single instance or a list of instances.
If you are in query mode introspecting the possible values, only
the first instance in the sequence is used. When actually setting
values, all the instances will be set. E.g., suppose you have a
list of two lines, the following will make both lines thicker and
red::

>>> x = arange(0,1.0,0.01)
>>> y1 = sin(2*pi*x)
>>> y2 = sin(4*pi*x)
>>> lines = plot(x, y1, x, y2)
>>> setp(lines, linewidth=2, color='r')

:func:`setp` works with the MATLAB style string/value pairs or
with python kwargs. For example, the following are equivalent::

>>> setp(lines, 'linewidth', 2, 'color', r') # MATLAB style

>>> setp(lines, linewidth=2, color='r') # python style
"""

    insp = ArtistInspector(obj)

    if len(kwargs) == 0 and len(args) == 0:
        print('\n'.join(insp.pprint_setters()))
        return

    if len(kwargs) == 0 and len(args) == 1:
        print(insp.pprint_setters(prop=args[0]))
        return

    if not cbook.iterable(obj):
        objs = [obj]
    else:
        objs = cbook.flatten(obj)

    if len(args) % 2:
        raise ValueError('The set args must be string, value pairs')

    funcvals = []
    for i in range(0, len(args) - 1, 2):
        funcvals.append((args[i], args[i + 1]))
    funcvals.extend(kwargs.iteritems())

    ret = []
    for o in objs:
        for s, val in funcvals:
            s = s.lower()
            funcName = "set_%s" % s
            func = getattr(o, funcName)
            ret.extend([func(val)])
    return [x for x in cbook.flatten(ret)]


def kwdoc(a):
    hardcopy = matplotlib.rcParams['docstring.hardcopy']
    if hardcopy:
        return '\n'.join(ArtistInspector(a).pprint_setters_rest(
                                            leadingspace=2))
    else:
        return '\n'.join(ArtistInspector(a).pprint_setters(leadingspace=2))

docstring.interpd.update(Artist=kwdoc(Artist))
Something went wrong with that request. Please try again.