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

Skip to content

Commit 15c489c

Browse files
committed
added new examples
svn path=/trunk/matplotlib/; revision=293
1 parent a2bdabb commit 15c489c

5 files changed

Lines changed: 155 additions & 0 deletions

File tree

examples/custom_ticker1.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
The new ticker code was designed to explicity support user customized
3+
ticking. The documentation
4+
http://matplotlib.sourceforge.net/matplotlib.ticker.html details this
5+
process. That code defines a lot of preset tickers but was primarily
6+
designed to be user extensible.
7+
8+
In this example a user defined function is used to format the ticks in
9+
millions of dollars on the y axis
10+
"""
11+
from matplotlib.ticker import FuncFormatter
12+
from matplotlib.matlab import *
13+
14+
x = arange(4)
15+
money = [1.5e5, 2.5e6, 5.5e6, 2.0e7]
16+
17+
def millions(x, pos):
18+
'The two args are the value and tick position'
19+
return '$%1.1fM' % (x*1e-6)
20+
21+
formatter = FuncFormatter(millions)
22+
23+
ax = subplot(111)
24+
ax.yaxis.set_major_formatter(formatter)
25+
bar(x, money)
26+
ax.set_xticks( x + 0.5)
27+
ax.set_xticklabels( ('Bill', 'Fred', 'Mary', 'Sue') )
28+
show()

examples/date_demo3.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python
2+
"""
3+
intdate is a class derived from int that is seconds since the epoch
4+
and also provides some helper functions for getting day, month, year,
5+
etc... You can use intdate with any matplotlib plotting function, not
6+
just plot_date
7+
"""
8+
from datetime import datetime
9+
import time
10+
from matplotlib.dates import intdate
11+
from matplotlib.ticker import MinuteLocator, DateFormatter
12+
from matplotlib.matlab import *
13+
14+
# simulate collecting data every minute starting at midnight
15+
t0 = time.mktime(datetime(2004,04,27).timetuple())
16+
t = t0+arange(0, 2*3600, 60) # 2 hours sampled every 2 minute
17+
s = rand(len(t))
18+
19+
ax = subplot(111)
20+
ax.xaxis.set_major_locator( MinuteLocator(20) )
21+
ax.xaxis.set_major_formatter( DateFormatter('%H:%M') )
22+
bar(t, s, width=60)
23+
show()
24+
25+
26+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
This is an example that shows you how to work directly with the agg
3+
figure canvas to create a figure using the pythonic API.
4+
5+
In this example, the contents of the agg canvas are extracted to a
6+
string, which can in turn be passed off to PIL or put in a numeric
7+
array
8+
9+
10+
"""
11+
#!/usr/bin/env python
12+
from matplotlib.backends.backend_agg import FigureCanvasAgg
13+
from matplotlib.figure import Figure
14+
from matplotlib.axes import Subplot
15+
from matplotlib.mlab import normpdf
16+
from matplotlib.numerix import randn
17+
18+
fig = Figure(figsize=(5,4), dpi=100)
19+
ax = fig.add_subplot(111)
20+
21+
canvas = FigureCanvasAgg(fig)
22+
23+
mu, sigma = 100, 15
24+
x = mu + sigma*randn(10000)
25+
26+
# the histogram of the data
27+
n, bins, patches = ax.hist(x, 50, normed=1)
28+
29+
# add a 'best fit' line
30+
y = normpdf( bins, mu, sigma)
31+
line, = ax.plot(bins, y, 'r--')
32+
line.set_linewidth(1)
33+
34+
ax.set_xlabel('Smarts')
35+
ax.set_ylabel('Probability')
36+
ax.set_title(r'$\rm{Histogram of IQ: }\mu=100, \sigma=15$')
37+
38+
ax.set_xlim( (40, 160))
39+
ax.set_ylim( (0, 0.03))
40+
41+
canvas.draw()
42+
43+
s = canvas.tostring_rgb() # save this and convert to bitmap as needed
44+
45+
# get the figure dimensions for creating bitmaps or numeric arrays,
46+
# etc.
47+
l,b,w,h = fig.bbox.get_bounds()
48+
w, h = int(w), int(h)
49+
50+
if 0:
51+
# convert to a Numeric array
52+
X = fromstring(s, UInt8)
53+
X.shape = h, w, 3
54+
55+
if 0:
56+
# pass off to PIL
57+
import Image
58+
im = Image.fromstring( "RGB", (w,h), s)
59+
im.show()
60+
61+
62+

examples/stem_plot.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env python
2+
from matplotlib.matlab import *
3+
4+
x = linspace(0.1, 2*pi, 10)
5+
markerline, stemlines, baseline = stem(x, cos(x), '-.')
6+
set(markerline, 'markerfacecolor', 'b')
7+
set(baseline, 'color','r', 'linewidth', 2)
8+
9+
show()

examples/to_numeric.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python
2+
"""
3+
Use backend agg to access the figure canvas as an RGB string and then
4+
convert it to a Numeric array and pass the string it to PIL for
5+
rendering
6+
"""
7+
8+
from matplotlib.matlab import *
9+
from matplotlib.backends.backend_agg import FigureCanvasAgg
10+
11+
plot([1,2,3])
12+
13+
canvas = get_current_fig_manager().canvas
14+
15+
agg = canvas.switch_backends(FigureCanvasAgg)
16+
agg.draw()
17+
s = agg.tostring_rgb()
18+
19+
# get the width and the height to resize the matrix
20+
l,b,w,h = agg.figure.bbox.get_bounds()
21+
w, h = int(w), int(h)
22+
23+
24+
X = fromstring(s, UInt8)
25+
X.shape = h, w, 3
26+
27+
import Image
28+
im = Image.fromstring( "RGB", (w,h), s)
29+
im.show()
30+

0 commit comments

Comments
 (0)