|
| 1 | +#!/usr/bin/env python |
| 2 | +# This example shows how to use the agg backend directly to create |
| 3 | +# images, which may be of use to web application developers who want |
| 4 | +# full control over their code without using the matlab interface to |
| 5 | +# manage figures, figure closing etc. |
| 6 | +# |
| 7 | +# The rc command is used to create per-script default figure |
| 8 | +# customizations of the rc parameters; see |
| 9 | +# http://matplotlib.sf.net/.matplotlibrc . You may prefer to set the |
| 10 | +# rc parameters in the rc file itself. Note that you can keep |
| 11 | +# directory level default configurations by placing different rc files |
| 12 | +# in the directory that the script runs in. |
| 13 | +# |
| 14 | +# I am making no effort here to make a figure that looks good -- |
| 15 | +# rather I am just trying to show the various ways to use matplotlib |
| 16 | +# to customize your figure using the matplotlib API |
| 17 | + |
| 18 | +import matplotlib |
| 19 | +matplotlib.use('Agg') # force the antigrain backend |
| 20 | +from matplotlib import rc |
| 21 | +from matplotlib.backends.backend_agg import FigureCanvasAgg |
| 22 | +from matplotlib.figure import Figure |
| 23 | +from matplotlib.cbook import iterable |
| 24 | +import matplotlib.numerix as nx |
| 25 | + |
| 26 | +# set some global properties that affect the defaults of all figures |
| 27 | +rc('lines', linewidth=2) # thicker plot lines |
| 28 | +rc('grid', color=0.75, linestyle='-') # solid gray grid lines |
| 29 | +rc('axes', hold=True, # hold state is on |
| 30 | + grid=True, facecolor='y') # yellow background, grid on |
| 31 | +rc('tick', color='r', labelsize=20) # big red ticks |
| 32 | + |
| 33 | +def setapi(o, **kwargs): |
| 34 | + """ |
| 35 | + for all key, value pairs in kwargs, and all objects in (possibly) |
| 36 | + iterable o, look for a method o.set_key and try to call |
| 37 | + o.set_key(value) if it exists. This is basically a refinition of |
| 38 | + the matlab interface set command |
| 39 | + """ |
| 40 | + if not iterable(o): o = [o] |
| 41 | + for thiso in o: # iterate over the objects |
| 42 | + for k,v in kwargs.items(): |
| 43 | + func = getattr(thiso, 'set_'+k) |
| 44 | + if func is None: continue |
| 45 | + func(v) |
| 46 | + |
| 47 | +def make_fig(): |
| 48 | + """ |
| 49 | + make a figure |
| 50 | +
|
| 51 | + No need to close figures or clean up since the objects will be |
| 52 | + destroyed when they go out of scope |
| 53 | + """ |
| 54 | + fig = Figure() |
| 55 | + #ax = fig.add_subplot(111) # add a standard subplot |
| 56 | + |
| 57 | + # add an axes at left, bottom, width, height; by making the bottom |
| 58 | + # at 0.3, we save some extra room for tick labels |
| 59 | + ax = fig.add_axes([0.2, 0.3, 0.7, 0.6]) |
| 60 | + |
| 61 | + line, = ax.plot([1,2,3], 'ro--', markersize=12, markerfacecolor='g') |
| 62 | + |
| 63 | + # make a translucent scatter collection |
| 64 | + x = nx.rand(100) |
| 65 | + y = nx.rand(100) |
| 66 | + area = nx.pi*(10 * nx.rand(100))**2 # 0 to 10 point radiuses |
| 67 | + c = ax.scatter(x,y,area) |
| 68 | + c.set_alpha(0.5) |
| 69 | + |
| 70 | + # add some text decoration |
| 71 | + ax.set_title('My first image') |
| 72 | + ax.set_ylabel('Some numbers') |
| 73 | + ax.set_xticks( (.2,.4,.6,.8) ) |
| 74 | + labels = ax.set_xticklabels(('Bill', 'Fred', 'Ted', 'Ed')) |
| 75 | + |
| 76 | + # To set object properties, you can either iterate over the |
| 77 | + # objects manually, or define you own set command, as in setapi |
| 78 | + # above. |
| 79 | + #setapi(labels, rotation=45, fontsize=12) |
| 80 | + for l in labels: |
| 81 | + l.set_rotation(45) |
| 82 | + l.set_fontsize(12) |
| 83 | + |
| 84 | + canvas = FigureCanvasAgg(fig) |
| 85 | + canvas.print_figure('webapp.png', dpi=150) |
| 86 | + |
| 87 | +make_fig() |
0 commit comments