11#!/usr/bin/env python
22# -*- noplot -*-
3- # This example shows how to use the agg backend directly to create
4- # images, which may be of use to web application developers who want
5- # full control over their code without using the pylab interface to
6- # manage figures, figure closing etc.
7- #
8- # The rc command is used to create per-script default figure
9- # customizations of the rc parameters; see
10- # http://matplotlib.sf.net/matplotlibrc . You may prefer to set the
11- # rc parameters in the rc file itself. Note that you can keep
12- # directory level default configurations by placing different rc files
13- # in the directory that the script runs in.
14- #
15- # I am making no effort here to make a figure that looks good --
16- # rather I am just trying to show the various ways to use matplotlib
17- # to customize your figure using the matplotlib API
3+ """
4+ This example shows how to use the agg backend directly to create
5+ images, which may be of use to web application developers who want
6+ full control over their code without using the pyplot interface to
7+ manage figures, figure closing etc.
8+
9+ .. note::
10+
11+ It is not necessary to avoid using the pyplot interface in order to
12+ create figures without a graphical front-end - simply setting
13+ the backend to "Agg" would be sufficient.
14+
15+
16+ It is also worth noting that, because matplotlib can save figures to file-like
17+ object, matplotlib can also be used inside a cgi-script *without* needing to
18+ write a figure to disk.
19+
20+ """
1821
19- import matplotlib
20- matplotlib .use ('Agg' ) # force the antigrain backend
21- from matplotlib import rc
2222from matplotlib .backends .backend_agg import FigureCanvasAgg
2323from matplotlib .figure import Figure
24- from matplotlib .cbook import iterable
2524import numpy as np
2625
26+
2727def make_fig ():
2828 """
29- make a figure
29+ Make a figure and save it to "webagg.png".
3030
31- No need to close figures or clean up since the objects will be
32- destroyed when they go out of scope
3331 """
3432 fig = Figure ()
35- #ax = fig.add_subplot(111) # add a standard subplot
36-
37- # add an axes at left, bottom, width, height; by making the bottom
38- # at 0.3, we save some extra room for tick labels
39- ax = fig .add_axes ([0.2 , 0.3 , 0.7 , 0.6 ])
33+ ax = fig .add_subplot (1 , 1 , 1 )
4034
41- line , = ax .plot ([1 ,2 , 3 ], 'ro--' , markersize = 12 , markerfacecolor = 'g' )
35+ ax .plot ([1 , 2 , 3 ], 'ro--' , markersize = 12 , markerfacecolor = 'g' )
4236
4337 # make a translucent scatter collection
4438 x = np .random .rand (100 )
4539 y = np .random .rand (100 )
46- area = np .pi * (10 * np .random .rand (100 ))** 2 # 0 to 10 point radiuses
47- c = ax .scatter (x ,y , area )
40+ area = np .pi * (10 * np .random .rand (100 )) ** 2 # 0 to 10 point radiuses
41+ c = ax .scatter (x , y , area )
4842 c .set_alpha (0.5 )
4943
5044 # add some text decoration
5145 ax .set_title ('My first image' )
5246 ax .set_ylabel ('Some numbers' )
53- ax .set_xticks ( (.2 ,.4 ,.6 ,.8 ) )
47+ ax .set_xticks ((.2 , .4 , .6 , .8 ))
5448 labels = ax .set_xticklabels (('Bill' , 'Fred' , 'Ted' , 'Ed' ))
5549
5650 # To set object properties, you can either iterate over the
5751 # objects manually, or define you own set command, as in setapi
5852 # above.
59- for l in labels :
60- l .set_rotation (45 )
61- l .set_fontsize (12 )
53+ for label in labels :
54+ label .set_rotation (45 )
55+ label .set_fontsize (12 )
6256
63- canvas = FigureCanvasAgg (fig )
64- canvas .print_figure ('webapp' , dpi = 150 )
57+ FigureCanvasAgg (fig ).print_png ('webapp.png' , dpi = 150 )
6558
66- make_fig ()
59+ make_fig ()
0 commit comments