Skip to content

Commit 1e48ed0

Browse files
committed
added some more example
svn path=/trunk/matplotlib/; revision=542
1 parent 43d6b5a commit 1e48ed0

File tree

4 files changed

+166
-0
lines changed

4 files changed

+166
-0
lines changed

examples/axhspan_demo.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from matplotlib.matlab import *
2+
figure(1)
3+
plot(10*rand(12), 'o')
4+
xlim(0,15)
5+
xticks([2, 4, 8, 12], ('John', 'Hunter', 'Was', 'Here'))
6+
7+
ylim(-1,10)
8+
yticks(range(8))
9+
10+
figure(2)
11+
t = arange(-1,2, .01)
12+
s = sin(2*pi*t)
13+
plot(t,s)
14+
# draw a thick red hline at y=0 that spans the xrange
15+
l = axhline(linewidth=4, color='r')
16+
17+
# draw a default hline at y=1 that spans the xrange
18+
l = axhline(y=1)
19+
20+
# draw a default vline at x=1 that spans the xrange
21+
l = axvline(x=1)
22+
23+
# draw a thick blue vline at x=0 that spans the the upper quadrant of
24+
# the yrange
25+
l = axvline(x=0, ymin=0.75, linewidth=4, color='b')
26+
27+
# draw a default hline at y=.5 that spans the the middle half of
28+
# the axes
29+
l = axhline(y=.5, xmin=0.25, xmax=0.75)
30+
31+
p = axhspan(0.25, 0.75, facecolor=0.5, alpha=0.5)
32+
33+
p = axvspan(1.25, 1.55, facecolor='g', alpha=0.5)
34+
35+
axis([-1,2,-1,2])
36+
37+
38+
39+
40+
41+
show()
42+

examples/date_demo_convert.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import matplotlib
2+
matplotlib.use('TkAgg')
3+
from matplotlib import matlab
4+
ML = matlab
5+
from matplotlib.dates import DayLocator, HourLocator, \
6+
drange, date2num, timezone
7+
import datetime
8+
9+
matplotlib.rcParams['timezone'] = 'US/Pacific'
10+
tz = timezone('US/Pacific')
11+
12+
13+
date1 = datetime.datetime( 2000, 3, 2, tzinfo=tz)
14+
date2 = datetime.datetime( 2000, 3, 6, tzinfo=tz)
15+
delta = datetime.timedelta(hours=6)
16+
dates = drange(date1, date2, delta)
17+
18+
y = ML.arrayrange( len(dates)*1.0)
19+
ysq = y*y
20+
21+
# note new constructor takes days or sequence of days you want to
22+
# tick, not the hour as before. Default is to tick every day
23+
majorTick = DayLocator(tz=tz)
24+
25+
# the hour locator takes the hour or sequence of hours you want to
26+
# tick, not the base multiple
27+
minorTick = HourLocator(range(0,25,6), tz=tz)
28+
ax = ML.subplot(111)
29+
ax.plot_date(dates, ysq, tz=tz)
30+
31+
# this is superfluous, since the autoscaler should get it right, but
32+
# use date2num and num2date to to convert between dates and floats if
33+
# you want; both date2num and num2date convert an instance or sequence
34+
ML.xlim( dates[0], dates[-1] )
35+
ax.xaxis.set_major_locator(majorTick)
36+
ax.xaxis.set_minor_locator(minorTick)
37+
labels = ax.get_xticklabels()
38+
ML.set(labels,'rotation', 90)
39+
ML.show()

examples/image_interp.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""
2+
The same (small) array, interpolated with three different
3+
interpolation methods.
4+
5+
The center of the pixel at A[i,j] is plotted at i+0.5, i+0.5. If you
6+
are using interpolation='nearest', the region bounded by (i,j) and
7+
(i+1,j+1) will have the same color. If you are using interpolation,
8+
the pixel center will have the same color as it does with nearest, but
9+
other pixels will be interpolated between the neighboring pixels.
10+
11+
Earlier versions of matplotlib (<0.63) tried to hide the edge effects
12+
from you by setting the view limits so that they would not be visible.
13+
A recent bugfix in antigrain, and a new implementation in the
14+
matplotlib._image module which takes advantage of this fix, no longer
15+
makes this necessary. To prevent edge effects, when doing
16+
interpolation, the matplotlib._image module now pads the input array
17+
with identical pixels around the edge. Eg, if you have a 5x5 array
18+
with colors a-y as below
19+
20+
21+
a b c d e
22+
f g h i j
23+
k l m n o
24+
p q r s t
25+
u v w x y
26+
27+
the _image module creates the padded array,
28+
29+
a a b c d e e
30+
a a b c d e e
31+
f f g h i j j
32+
k k l m n o o
33+
p p q r s t t
34+
o u v w x y y
35+
o u v w x y y
36+
37+
does the interpolation/resizing, and then extracts the central region.
38+
This allows you to plot the full range of your array w/o edge effects,
39+
and for example to layer multiple images of different sizes over one
40+
another with different interpolation methods - see
41+
examples/layer_images.py. It also implies a performance hit, as this
42+
new temporary, padded array must be created. Sophisticated
43+
interpolation also implies a performance hit, so if you need maximal
44+
performance or have very large images, interpolation='nearest' is
45+
suggested.
46+
47+
"""
48+
from matplotlib.matlab import *
49+
A = rand(5,5)
50+
figure(1)
51+
imshow(A, interpolation='nearest')
52+
savefig('agg_nearest')
53+
grid(True)
54+
55+
figure(2)
56+
imshow(A, interpolation='bilinear')
57+
savefig('agg_bilinear')
58+
grid(True)
59+
60+
figure(3)
61+
imshow(A, interpolation='bicubic')
62+
savefig('agg_bicubic')
63+
grid(True)
64+
65+
show()

examples/movie_demo.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import os, sys
2+
from matplotlib.matlab import *
3+
4+
files = []
5+
figure(figsize=(5,5))
6+
ax = subplot(111)
7+
for i in range(50): # 50 frames
8+
cla()
9+
imshow(rand(5,5), interpolation='nearest')
10+
fname = '_tmp%03d.png'%i
11+
print 'Saving frame', fname
12+
savefig(fname)
13+
files.append(fname)
14+
15+
print 'Making movie animation.mpg - this make take a while'
16+
#os.system("mencoder 'mf://_tmp*.png' -mf type=png:fps=10 -ovc lavc -lavcopts vcodec=wmv2 -oac copy -o animation.mpg")
17+
os.system("convert _tmp*.png animation.mpg")
18+
19+
# cleanup
20+
for fname in files: os.remove(fname)

0 commit comments

Comments
 (0)