Menu

[r4113]: / branches / transforms / lib / matplotlib / figure.py  Maximize  Restore  History

Download this file

890 lines (700 with data), 29.5 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
"""
Figure class -- add docstring here!
"""
import numpy as npy
import artist
from artist import Artist
from axes import Axes, SubplotBase, subplot_class_factory
from cbook import flatten, allequal, Stack, iterable, dedent
import _image
import colorbar as cbar
from image import FigureImage
from matplotlib import rcParams
from patches import Rectangle
from text import Text, _process_text_args
from legend import Legend
from transforms import Affine2D, Bbox, BboxTransformTo, TransformedBbox
from projections import projection_factory, get_projection_names, \
get_projection_class
class SubplotParams:
"""
A class to hold the parameters for a subplot
"""
def __init__(self, left=None, bottom=None, right=None, top=None,
wspace=None, hspace=None):
"""
All dimensions are fraction of the figure width or height.
All values default to their rc params
The following attributes are available:
left : the left side of the subplots of the figure
right : the right side of the subplots of the figure
bottom : the bottom of the subplots of the figure
top : the top of the subplots of the figure
wspace : the amount of width reserved for blank space between subplots
hspace : the amount of height reserved for white space between subplots
validate : make sure the params are in a legal state
(left<right, etc)
"""
self.validate = True
self.update(left, bottom, right, top, wspace, hspace)
def update(self,left=None, bottom=None, right=None, top=None,
wspace=None, hspace=None):
"""
Update the current values. If any kwarg is None, default to
the current value, if set, otherwise to rc
"""
thisleft = getattr(self, 'left', None)
thisright = getattr(self, 'right', None)
thistop = getattr(self, 'top', None)
thisbottom = getattr(self, 'bottom', None)
thiswspace = getattr(self, 'wspace', None)
thishspace = getattr(self, 'hspace', None)
self._update_this('left', left)
self._update_this('right', right)
self._update_this('bottom', bottom)
self._update_this('top', top)
self._update_this('wspace', wspace)
self._update_this('hspace', hspace)
def reset():
self.left = thisleft
self.right = thisright
self.top = thistop
self.bottom = thisbottom
self.wspace = thiswspace
self.hspace = thishspace
if self.validate:
if self.left>=self.right:
reset()
raise ValueError('left cannot be >= right')
if self.bottom>=self.top:
reset()
raise ValueError('bottom cannot be >= top')
def _update_this(self, s, val):
if val is None:
val = getattr(self, s, None)
if val is None:
key = 'figure.subplot.' + s
val = rcParams[key]
setattr(self, s, val)
class Figure(Artist):
def __str__(self):
return "Figure(%gx%g)" % tuple(self.bbox.size)
def __init__(self,
figsize = None, # defaults to rc figure.figsize
dpi = None, # defaults to rc figure.dpi
facecolor = None, # defaults to rc figure.facecolor
edgecolor = None, # defaults to rc figure.edgecolor
linewidth = 1.0, # the default linewidth of the frame
frameon = True, # whether or not to draw the figure frame
subplotpars = None, # default to rc
):
"""
figsize is a w,h tuple in inches
dpi is dots per inch
subplotpars is a SubplotParams instance, defaults to rc
"""
Artist.__init__(self)
if figsize is None : figsize = rcParams['figure.figsize']
if dpi is None : dpi = rcParams['figure.dpi']
if facecolor is None: facecolor = rcParams['figure.facecolor']
if edgecolor is None: edgecolor = rcParams['figure.edgecolor']
self._dpi_scale_trans = Affine2D()
self.dpi = dpi
self.bbox_inches = Bbox.from_bounds(0, 0, *figsize)
self.bbox = TransformedBbox(self.bbox_inches, self._dpi_scale_trans)
self.frameon = frameon
self.transFigure = BboxTransformTo(self.bbox)
self.figurePatch = Rectangle(
xy=(0,0), width=1, height=1,
facecolor=facecolor, edgecolor=edgecolor,
linewidth=linewidth,
)
self._set_artist_props(self.figurePatch)
self._hold = rcParams['axes.hold']
self.canvas = None
if subplotpars is None:
subplotpars = SubplotParams()
self.subplotpars = subplotpars
self._axstack = Stack() # maintain the current axes
self.axes = []
self.clf()
self._cachedRenderer = None
def _get_dpi(self):
return self._dpi
def _set_dpi(self, dpi):
self._dpi = dpi
self._dpi_scale_trans.clear().scale(dpi, dpi)
dpi = property(_get_dpi, _set_dpi)
def autofmt_xdate(self, bottom=0.2, rotation=30, ha='right'):
"""
A common use case is a number of subplots with shared xaxes
where the x-axis is date data. The ticklabels are often
long,and it helps to rotate them on the bottom subplot and
turn them off on other subplots. This function will raise a
RuntimeError if any of the Axes are not Subplots.
bottom : the bottom of the subplots for subplots_adjust
rotation: the rotation of the xtick labels
ha : the horizontal alignment of the xticklabels
"""
for ax in self.get_axes():
if not hasattr(ax, 'is_last_row'):
raise RuntimeError('Axes must be subplot instances; found %s'%type(ax))
if ax.is_last_row():
for label in ax.get_xticklabels():
label.set_ha(ha)
label.set_rotation(rotation)
else:
for label in ax.get_xticklabels():
label.set_visible(False)
self.subplots_adjust(bottom=bottom)
def get_children(self):
'get a list of artists contained in the figure'
children = [self.figurePatch]
children.extend(self.axes)
children.extend(self.lines)
children.extend(self.patches)
children.extend(self.texts)
children.extend(self.images)
children.extend(self.legends)
return children
def contains(self, mouseevent):
"""Test whether the mouse event occurred on the figure.
Returns True,{}
"""
if callable(self._contains): return self._contains(self,mouseevent)
#inside = mouseevent.x >= 0 and mouseevent.y >= 0
inside = self.bbox.contains(mouseevent.x,mouseevent.y)
return inside,{}
def get_window_extent(self, *args, **kwargs):
'get the figure bounding box in display space; kwargs are void'
return self.bbox
def set_canvas(self, canvas):
"""
Set the canvas the contains the figure
ACCEPTS: a FigureCanvas instance
"""
self.canvas = canvas
def hold(self, b=None):
"""
Set the hold state. If hold is None (default), toggle the
hold state. Else set the hold state to boolean value b.
Eg
hold() # toggle hold
hold(True) # hold is on
hold(False) # hold is off
"""
if b is None: self._hold = not self._hold
else: self._hold = b
def figimage(self, X,
xo=0,
yo=0,
alpha=1.0,
norm=None,
cmap=None,
vmin=None,
vmax=None,
origin=None):
"""
FIGIMAGE(X) # add non-resampled array to figure
FIGIMAGE(X, xo, yo) # with pixel offsets
FIGIMAGE(X, **kwargs) # control interpolation ,scaling, etc
Add a nonresampled figure to the figure from array X. xo and yo are
offsets in pixels
X must be a float array
If X is MxN, assume luminance (grayscale)
If X is MxNx3, assume RGB
If X is MxNx4, assume RGBA
The following kwargs are allowed:
* cmap is a cm colormap instance, eg cm.jet. If None, default to
the rc image.cmap valuex
* norm is a matplotlib.colors.Normalize instance; default is
normalization(). This scales luminance -> 0-1
* vmin and vmax are used to scale a luminance image to 0-1. If
either is None, the min and max of the luminance values will be
used. Note if you pass a norm instance, the settings for vmin and
vmax will be ignored.
* alpha = 1.0 : the alpha blending value
* origin is either 'upper' or 'lower', which indicates where the [0,0]
index of the array is in the upper left or lower left corner of
the axes. Defaults to the rc image.origin value
This complements the axes image (Axes.imshow) which will be resampled
to fit the current axes. If you want a resampled image to fill the
entire figure, you can define an Axes with size [0,1,0,1].
A image.FigureImage instance is returned.
"""
if not self._hold: self.clf()
im = FigureImage(self, cmap, norm, xo, yo, origin)
im.set_array(X)
im.set_alpha(alpha)
if norm is None:
im.set_clim(vmin, vmax)
self.images.append(im)
return im
def set_figsize_inches(self, *args, **kwargs):
import warnings
warnings.warn('Use set_size_inches instead!', DeprecationWarning)
self.set_size_inches(*args, **kwargs)
def set_size_inches(self, *args, **kwargs):
"""
set_size_inches(w,h, forward=False)
Set the figure size in inches
Usage: set_size_inches(self, w,h) OR
set_size_inches(self, (w,h) )
optional kwarg forward=True will cause the canvas size to be
automatically updated; eg you can resize the figure window
from the shell
WARNING: forward=True is broken on all backends except GTK*
ACCEPTS: a w,h tuple with w,h in inches
"""
forward = kwargs.get('forward', False)
if len(args)==1:
w,h = args[0]
else:
w,h = args
dpival = self.dpi
self.bbox_inches.p1 = w, h
if forward:
dpival = self.dpi
canvasw = w*dpival
canvash = h*dpival
manager = getattr(self.canvas, 'manager', None)
if manager is not None:
manager.resize(int(canvasw), int(canvash))
def get_size_inches(self):
return self.bbox_inches.p1
def get_edgecolor(self):
'Get the edge color of the Figure rectangle'
return self.figurePatch.get_edgecolor()
def get_facecolor(self):
'Get the face color of the Figure rectangle'
return self.figurePatch.get_facecolor()
def get_figwidth(self):
'Return the figwidth as a float'
return self.bbox_inches.width
def get_figheight(self):
'Return the figheight as a float'
return self.bbox_inches.height
def get_dpi(self):
'Return the dpi as a float'
return self.dpi
def get_frameon(self):
'get the boolean indicating frameon'
return self.frameon
def set_edgecolor(self, color):
"""
Set the edge color of the Figure rectangle
ACCEPTS: any matplotlib color - see help(colors)
"""
self.figurePatch.set_edgecolor(color)
def set_facecolor(self, color):
"""
Set the face color of the Figure rectangle
ACCEPTS: any matplotlib color - see help(colors)
"""
self.figurePatch.set_facecolor(color)
def set_dpi(self, val):
"""
Set the dots-per-inch of the figure
ACCEPTS: float
"""
self.dpi = val
def set_figwidth(self, val):
"""
Set the width of the figure in inches
ACCEPTS: float
"""
self.bbox_inches.x1 = val
def set_figheight(self, val):
"""
Set the height of the figure in inches
ACCEPTS: float
"""
self.bbox_inches.y1 = val
def set_frameon(self, b):
"""
Set whether the figure frame (background) is displayed or invisible
ACCEPTS: boolean
"""
self.frameon = b
def delaxes(self, a):
'remove a from the figure and update the current axes'
self.axes.remove(a)
self._axstack.remove(a)
keys = []
for key, thisax in self._seen.items():
if a==thisax: del self._seen[key]
for func in self._axobservers: func(self)
def _make_key(self, *args, **kwargs):
'make a hashable key out of args and kwargs'
def fixitems(items):
#items may have arrays and lists in them, so convert them
# to tuples for the key
ret = []
for k, v in items:
if iterable(v): v = tuple(v)
ret.append((k,v))
return tuple(ret)
def fixlist(args):
ret = []
for a in args:
if iterable(a): a = tuple(a)
ret.append(a)
return tuple(ret)
key = fixlist(args), fixitems(kwargs.items())
return key
def add_axes(self, *args, **kwargs):
"""
Add an a axes with axes rect [left, bottom, width, height] where all
quantities are in fractions of figure width and height. kwargs are
legal Axes kwargs plus "projection" which sets the projection type
of the axes. (For backward compatibility, polar=True may also be
provided, which is equivalent to projection='polar').
Valid values for "projection" are: %s. Some of these projections
support additional kwargs, which may be provided to add_axes.
rect = l,b,w,h
add_axes(rect)
add_axes(rect, frameon=False, axisbg='g')
add_axes(rect, polar=True)
add_axes(rect, projection='polar')
add_axes(ax) # add an Axes instance
If the figure already has an axes with key *args, *kwargs then it will
simply make that axes current and return it. If you do not want this
behavior, eg you want to force the creation of a new axes, you must
use a unique set of args and kwargs. The artist "label" attribute has
been exposed for this purpose. Eg, if you want two axes that are
otherwise identical to be added to the figure, make sure you give them
unique labels:
add_axes(rect, label='axes1')
add_axes(rect, label='axes2')
The Axes instance will be returned
The following kwargs are supported:
%s
""" % (", ".join(get_projection_names()), '%(Axes)s')
key = self._make_key(*args, **kwargs)
if self._seen.has_key(key):
ax = self._seen[key]
self.sca(ax)
return ax
if not len(args): return
if isinstance(args[0], Axes):
a = args[0]
assert(a.get_figure() is self)
else:
rect = args[0]
ispolar = kwargs.pop('polar', False)
projection = kwargs.pop('projection', None)
if ispolar:
if projection is not None and projection != 'polar':
raise ValueError(
"polar=True, yet projection='%s'. " +
"Only one of these arguments should be supplied." %
projection)
projection = 'polar'
a = projection_factory(projection, self, rect, **kwargs)
self.axes.append(a)
self._axstack.push(a)
self.sca(a)
self._seen[key] = a
return a
add_axes.__doc__ = dedent(add_axes.__doc__) % artist.kwdocd
def add_subplot(self, *args, **kwargs):
"""
Add a subplot. Examples
add_subplot(111)
add_subplot(212, axisbg='r') # add subplot with red background
add_subplot(111, polar=True) # add a polar subplot
add_subplot(sub) # add Subplot instance sub
kwargs are legal Axes kwargs plus "projection", which chooses
a projection type for the axes. (For backward compatibility,
polar=True may also be provided, which is equivalent to
projection='polar'). Valid values for "projection" are: %s.
Some of these projections support additional kwargs, which may
be provided to add_axes.
The Axes instance will be returned.
If the figure already has a subplot with key *args, *kwargs then it will
simply make that subplot current and return it
The following kwargs are supported:
%s
""" % (", ".join(get_projection_names()), "%(Axes)s")
key = self._make_key(*args, **kwargs)
if self._seen.has_key(key):
ax = self._seen[key]
self.sca(ax)
return ax
if not len(args): return
if isinstance(args[0], SubplotBase):
a = args[0]
assert(a.get_figure() is self)
else:
ispolar = kwargs.pop('polar', False)
projection = kwargs.pop('projection', None)
if ispolar:
if projection is not None and projection != 'polar':
raise ValueError(
"polar=True, yet projection='%s'. " +
"Only one of these arguments should be supplied." %
projection)
projection = 'polar'
projection_class = get_projection_class(projection)
a = subplot_class_factory(projection_class)(self, *args, **kwargs)
self.axes.append(a)
self._axstack.push(a)
self.sca(a)
self._seen[key] = a
return a
add_subplot.__doc__ = dedent(add_subplot.__doc__) % artist.kwdocd
def clf(self):
"""
Clear the figure
"""
for ax in tuple(self.axes): # Iterate over the copy.
ax.cla()
self.delaxes(ax) # removes ax from self.axes
toolbar = getattr(self.canvas, 'toolbar', None)
if toolbar is not None:
toolbar.update()
self._axstack.clear()
self._seen = {}
self.lines = []
self.patches = []
self.texts=[]
self.images = []
self.legends = []
self._axobservers = []
def clear(self):
"""
Clear the figure
"""
self.clf()
def draw(self, renderer):
"""
Render the figure using Renderer instance renderer
"""
# draw the figure bounding box, perhaps none for white figure
#print 'figure draw'
if not self.get_visible(): return
renderer.open_group('figure')
if self.frameon: self.figurePatch.draw(renderer)
for p in self.patches: p.draw(renderer)
for l in self.lines: l.draw(renderer)
if len(self.images)<=1 or renderer.option_image_nocomposite() or not allequal([im.origin for im in self.images]):
for im in self.images:
im.draw(renderer)
else:
# make a composite image blending alpha
# list of (_image.Image, ox, oy)
mag = renderer.get_image_magnification()
ims = [(im.make_image(mag), im.ox*mag, im.oy*mag)
for im in self.images]
im = _image.from_images(self.bbox.height * mag,
self.bbox.width * mag,
ims)
im.is_grayscale = False
l, b, w, h = self.bbox.bounds
renderer.draw_image(l, b, im, self.bbox,
*self.get_transformed_clip_path_and_affine())
# render the axes
for a in self.axes: a.draw(renderer)
# render the figure text
for t in self.texts: t.draw(renderer)
for legend in self.legends:
legend.draw(renderer)
renderer.close_group('figure')
self._cachedRenderer = renderer
self.canvas.draw_event(renderer)
def draw_artist(self, a):
'draw artist only -- this is available only after the figure is drawn'
assert self._cachedRenderer is not None
a.draw(self._cachedRenderer)
def get_axes(self):
return self.axes
def legend(self, handles, labels, *args, **kwargs):
"""
Place a legend in the figure. Labels are a sequence of
strings, handles is a sequence of line or patch instances, and
loc can be a string or an integer specifying the legend
location
USAGE:
legend( (line1, line2, line3),
('label1', 'label2', 'label3'),
'upper right')
The LOC location codes are
'best' : 0, (currently not supported for figure legends)
'upper right' : 1,
'upper left' : 2,
'lower left' : 3,
'lower right' : 4,
'right' : 5,
'center left' : 6,
'center right' : 7,
'lower center' : 8,
'upper center' : 9,
'center' : 10,
loc can also be an (x,y) tuple in figure coords, which
specifies the lower left of the legend box. figure coords are
(0,0) is the left, bottom of the figure and 1,1 is the right,
top.
The legend instance is returned. The following kwargs are supported:
loc = "upper right" #
numpoints = 4 # the number of points in the legend line
prop = FontProperties(size='smaller') # the font property
pad = 0.2 # the fractional whitespace inside the legend border
markerscale = 0.6 # the relative size of legend markers vs. original
shadow # if True, draw a shadow behind legend
labelsep = 0.005 # the vertical space between the legend entries
handlelen = 0.05 # the length of the legend lines
handletextsep = 0.02 # the space between the legend line and legend text
axespad = 0.02 # the border between the axes and legend edge
"""
handles = flatten(handles)
l = Legend(self, handles, labels, *args, **kwargs)
self.legends.append(l)
return l
def text(self, x, y, s, *args, **kwargs):
"""
Add text to figure at location x,y (relative 0-1 coords) See
the help for Axis text for the meaning of the other arguments
kwargs control the Text properties:
%(Text)s
"""
override = _process_text_args({}, *args, **kwargs)
t = Text(
x=x, y=y, text=s,
)
t.update(override)
self._set_artist_props(t)
self.texts.append(t)
return t
text.__doc__ = dedent(text.__doc__) % artist.kwdocd
def _set_artist_props(self, a):
if a!= self:
a.set_figure(self)
a.set_transform(self.transFigure)
def gca(self, **kwargs):
"""
Return the current axes, creating one if necessary
The following kwargs are supported
%(Axes)s
"""
ax = self._axstack()
if ax is not None: return ax
return self.add_subplot(111, **kwargs)
gca.__doc__ = dedent(gca.__doc__) % artist.kwdocd
def sca(self, a):
'Set the current axes to be a and return a'
self._axstack.bubble(a)
for func in self._axobservers: func(self)
return a
def add_axobserver(self, func):
'whenever the axes state change, func(self) will be called'
self._axobservers.append(func)
def savefig(self, *args, **kwargs):
"""
SAVEFIG(fname, dpi=None, facecolor='w', edgecolor='w',
orientation='portrait', papertype=None, format=None):
Save the current figure.
fname - the filename to save the current figure to. The
output formats supported depend on the backend being
used. and are deduced by the extension to fname.
Possibilities are eps, jpeg, pdf, png, ps, svg. fname
can also be a file or file-like object - cairo backend
only.
dpi - is the resolution in dots per inch. If
None it will default to the value savefig.dpi in the
matplotlibrc file
facecolor and edgecolor are the colors of the figure rectangle
orientation is either 'landscape' or 'portrait' - not supported on
all backends; currently only on postscript output
papertype is is one of 'letter', 'legal', 'executive', 'ledger', 'a0'
through 'a10', or 'b0' through 'b10' - only supported for postscript
output
format - one of the file extensions supported by the active backend.
"""
for key in ('dpi', 'facecolor', 'edgecolor'):
if not kwargs.has_key(key):
kwargs[key] = rcParams['savefig.%s'%key]
self.canvas.print_figure(*args, **kwargs)
def colorbar(self, mappable, cax=None, ax=None, **kw):
if ax is None:
ax = self.gca()
if cax is None:
cax, kw = cbar.make_axes(ax, **kw)
cb = cbar.Colorbar(cax, mappable, **kw)
mappable.add_observer(cb)
mappable.set_colorbar(cb, cax)
self.sca(ax)
return cb
colorbar.__doc__ = '''
Create a colorbar for a ScalarMappable instance.
Documentation for the pylab thin wrapper: %s
'''% cbar.colorbar_doc
def subplots_adjust(self, *args, **kwargs):
"""
subplots_adjust(self, left=None, bottom=None, right=None, top=None,
wspace=None, hspace=None)
fig.subplots_adjust(left=None, bottom=None, right=None, wspace=None, hspace=None):
Update the SubplotParams with kwargs (defaulting to rc where
None) and update the subplot locations
"""
self.subplotpars.update(*args, **kwargs)
import matplotlib.axes
for ax in self.axes:
if not isinstance(ax, matplotlib.axes.SubplotBase):
# Check if sharing a subplots axis
if ax._sharex is not None and isinstance(ax._sharex, matplotlib.axes.SubplotBase):
ax._sharex.update_params()
ax.set_position(ax._sharex.figbox)
elif ax._sharey is not None and isinstance(ax._sharey, matplotlib.axes.SubplotBase):
ax._sharey.update_params()
ax.set_position(ax._sharey.figbox)
else:
ax.update_params()
ax.set_position(ax.figbox)
def figaspect(arg):
"""
Create a figure with specified aspect ratio. If arg is a number,
use that aspect ratio. If arg is an array, figaspect will
determine the width and height for a figure that would fit array
preserving aspect ratio. The figure width, height in inches are
returned. Be sure to create an axes with equal with and height,
eg
Example usage:
# make a figure twice as tall as it is wide
w, h = figaspect(2.)
fig = Figure(figsize=(w,h))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
ax.imshow(A, **kwargs)
# make a figure with the proper aspect for an array
A = rand(5,3)
w, h = figaspect(A)
fig = Figure(figsize=(w,h))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
ax.imshow(A, **kwargs)
Thanks to Fernando Perez for this function
"""
isarray = hasattr(arg, 'shape')
# min/max sizes to respect when autoscaling. If John likes the idea, they
# could become rc parameters, for now they're hardwired.
figsize_min = npy.array((4.0,2.0)) # min length for width/height
figsize_max = npy.array((16.0,16.0)) # max length for width/height
#figsize_min = rcParams['figure.figsize_min']
#figsize_max = rcParams['figure.figsize_max']
# Extract the aspect ratio of the array
if isarray:
nr,nc = arg.shape[:2]
arr_ratio = float(nr)/nc
else:
arr_ratio = float(arg)
# Height of user figure defaults
fig_height = rcParams['figure.figsize'][1]
# New size for the figure, keeping the aspect ratio of the caller
newsize = npy.array((fig_height/arr_ratio,fig_height))
# Sanity checks, don't drop either dimension below figsize_min
newsize /= min(1.0,*(newsize/figsize_min))
# Avoid humongous windows as well
newsize /= max(1.0,*(newsize/figsize_max))
# Finally, if we have a really funky aspect ratio, break it but respect
# the min/max dimensions (we don't want figures 10 feet tall!)
newsize = npy.clip(newsize,figsize_min,figsize_max)
return newsize
artist.kwdocd['Figure'] = artist.kwdoc(Figure)
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.