Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e64f3d4

Browse files
committed
added a webapp demo
svn path=/trunk/matplotlib/; revision=589
1 parent 7fc9539 commit e64f3d4

4 files changed

Lines changed: 123 additions & 33 deletions

File tree

examples/pythonic_matplotlib.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,20 @@
1111
instances, managing the bounding boxes of the figure elements,
1212
creating and reaslizing GUI windows and embedding figures in them.
1313
14+
1415
If you are an application developer and want to embed matplotlib in
1516
your application, follow the lead of examples/embedding_in_wx.py,
1617
examples/embedding_in_gtk.py or examples/embedding_in_tk.py. In this
1718
case you will want to control the creation of all your figures,
1819
embedding them in application windows, etc.
1920
21+
If you are a web application developer, you may want to use the
22+
example in webapp_demo.py, which shows how to use the backend agg
23+
figure canvase directly, with none of the globals (current figure,
24+
current axes) that are present in the matlab interface. Note that
25+
there is no reason why the matlab interface won't work for web
26+
application developers, however.
27+
2028
If you see an example in the examples dir written in matlab interface,
2129
and you want to emulate that using the true python method calls, there
2230
is an easy mapping. Many of those examples use 'set' to control

examples/webapp_demo.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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()

lib/matplotlib/figure.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
class Figure(Artist):
1717

1818
def __init__(self,
19-
figsize = None, # defaults to rc figure.figsize
20-
dpi = None, # defaults to rc figure.dpi
21-
facecolor = None, # defaults to rc figure.facecolor
22-
edgecolor = None, # defaults to rc figure.edgecolor
19+
figsize = None, # defaults to rc figure.figsize
20+
dpi = None, # defaults to rc figure.dpi
21+
facecolor = None, # defaults to rc figure.facecolor
22+
edgecolor = None, # defaults to rc figure.edgecolor
2323
linewidth = 1.0, # the default linewidth of the frame
2424
frameon = True,
2525
):

lib/matplotlib/matlab.py

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -305,17 +305,20 @@ def raise_msg_to_str(msg):
305305
#----- Now we get started with the matlab commands ----#
306306

307307
def axis(*v):
308-
"""
309-
axis() returns the current axis as a length a length 4 vector
308+
"""\
309+
Set/Get the axis properties::
310+
311+
axis() returns the current axis as a length a length 4 vector
310312
311-
axis(v) where v= [xmin xmax ymin ymax] sets the min and max of the
312-
x and y axis limits
313+
axis(v) where v = [xmin, xmax, ymin, ymax] sets the min and max of the x
314+
and y axis limits
313315
314316
axis('off') turns off the axis lines and labels
315317
316318
axis('equal') sets the xlim width and ylim height to be to be
317-
identical. The longer of the two intervals is chosen
318-
"""
319+
identical. The longer of the two intervals is chosen
320+
321+
"""
319322

320323
if len(v)==1 and is_string_like(v[0]):
321324
s = v[0]
@@ -355,26 +358,18 @@ def axis(*v):
355358

356359
def axes(*args, **kwargs):
357360
"""
358-
Add an axis at positon rect specified by
361+
Add an axes at positon rect specified by::
359362
360-
axes() by itself creates a default full subplot(111) window axis
363+
axes() by itself creates a default full subplot(111) window axis
361364
362-
axes(rect, axisbg='w') where rect=[left, bottom, width, height]
363-
in normalized (0,1) units. axisbg is the background color for
364-
the axis, default white
365+
axes(rect, axisbg='w') where rect=[left, bottom, width, height] in
366+
normalized (0,1) units. axisbg is the background color for the
367+
axis, default white
365368
366-
axes(h) where h is an axes instance makes h the
367-
current axis An Axes instance is returned
369+
axes(h) where h is an axes instance makes h the
370+
current axis An Axes instance is returned
368371
369-
axisbg is a color format string which sets the background color of
370-
the axes
371372
372-
If axisbg is a length 1 string, assume it's a color format string
373-
(see plot for legal color strings). If it is a length 7 string,
374-
assume it's a hex color string, as used in html, eg, '#eeefff'.
375-
If it is a len(3) tuple, assume it's an rgb value where r,g,b in
376-
[0,1].
377-
378373
"""
379374

380375
nargs = len(args)
@@ -1701,16 +1696,16 @@ def axhline(y=0, xmin=0, xmax=1, **kwargs):
17011696
return value is the Line2D instance. kwargs are the same as kwargs to
17021697
plot, and can be used to control the line properties. Eg
17031698
1704-
# draw a thick red hline at y=0 that spans the xrange
1705-
l = axhline(linewidth=4, color='r')
1699+
# draw a thick red hline at y=0 that spans the xrange
1700+
l = axhline(linewidth=4, color='r')
17061701
1707-
# draw a default hline at y=1 that spans the xrange
1708-
l = axhline(y=1)
1702+
# draw a default hline at y=1 that spans the xrange
1703+
l = axhline(y=1)
17091704
1710-
# draw a default hline at y=.5 that spans the the middle half of
1711-
# the xrange
1712-
l = axhline(y=.5, xmin=0.25, xmax=0.75)
1713-
ylim(-1,2)
1705+
# draw a default hline at y=.5 that spans the the middle half of
1706+
# the xrange
1707+
l = axhline(y=.5, xmin=0.25, xmax=0.75)
1708+
ylim(-1,2)
17141709
"""
17151710
ax = gca()
17161711
trans = blend_xy_sep_transform( ax.transAxes, ax.transData)

0 commit comments

Comments
 (0)