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

Skip to content

Commit 7b6dc66

Browse files
committed
added pyplots examples which I forgot to svn add in prev commit
svn path=/trunk/matplotlib/; revision=5690
1 parent 7444976 commit 7b6dc66

18 files changed

Lines changed: 1138 additions & 0 deletions

doc/pyplots/arrow_demo.py

Lines changed: 313 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,313 @@
1+
#!/usr/bin/env python
2+
"""Arrow drawing example for the new fancy_arrow facilities.
3+
4+
Code contributed by: Rob Knight <[email protected]>
5+
6+
usage:
7+
8+
python arrow_demo.py realistic|full|sample|extreme
9+
10+
11+
"""
12+
from pylab import *
13+
14+
rates_to_bases={'r1':'AT', 'r2':'TA', 'r3':'GA','r4':'AG','r5':'CA','r6':'AC', \
15+
'r7':'GT', 'r8':'TG', 'r9':'CT','r10':'TC','r11':'GC','r12':'CG'}
16+
numbered_bases_to_rates = dict([(v,k) for k, v in rates_to_bases.items()])
17+
lettered_bases_to_rates = dict([(v, 'r'+v) for k, v in rates_to_bases.items()])
18+
def add_dicts(d1, d2):
19+
"""Adds two dicts and returns the result."""
20+
result = d1.copy()
21+
result.update(d2)
22+
return result
23+
24+
def make_arrow_plot(data, size=4, display='length', shape='right', \
25+
max_arrow_width=0.03, arrow_sep = 0.02, alpha=0.5, \
26+
normalize_data=False, ec=None, labelcolor=None, \
27+
head_starts_at_zero=True, rate_labels=lettered_bases_to_rates,\
28+
**kwargs):
29+
"""Makes an arrow plot.
30+
31+
Parameters:
32+
33+
data: dict with probabilities for the bases and pair transitions.
34+
size: size of the graph in inches.
35+
display: 'length', 'width', or 'alpha' for arrow property to change.
36+
shape: 'full', 'left', or 'right' for full or half arrows.
37+
max_arrow_width: maximum width of an arrow, data coordinates.
38+
arrow_sep: separation between arrows in a pair, data coordinates.
39+
alpha: maximum opacity of arrows, default 0.8.
40+
41+
**kwargs can be anything allowed by a Arrow object, e.g.
42+
linewidth and edgecolor.
43+
"""
44+
45+
xlim(-0.5,1.5)
46+
ylim(-0.5,1.5)
47+
gcf().set_size_inches(size,size)
48+
xticks([])
49+
yticks([])
50+
max_text_size = size*12
51+
min_text_size = size
52+
label_text_size = size*2.5
53+
text_params={'ha':'center', 'va':'center', 'family':'sans-serif',\
54+
'fontweight':'bold'}
55+
r2 = sqrt(2)
56+
57+
deltas = {\
58+
'AT':(1,0),
59+
'TA':(-1,0),
60+
'GA':(0,1),
61+
'AG':(0,-1),
62+
'CA':(-1/r2, 1/r2),
63+
'AC':(1/r2, -1/r2),
64+
'GT':(1/r2, 1/r2),
65+
'TG':(-1/r2,-1/r2),
66+
'CT':(0,1),
67+
'TC':(0,-1),
68+
'GC':(1,0),
69+
'CG':(-1,0)
70+
}
71+
72+
colors = {\
73+
'AT':'r',
74+
'TA':'k',
75+
'GA':'g',
76+
'AG':'r',
77+
'CA':'b',
78+
'AC':'r',
79+
'GT':'g',
80+
'TG':'k',
81+
'CT':'b',
82+
'TC':'k',
83+
'GC':'g',
84+
'CG':'b'
85+
}
86+
87+
label_positions = {\
88+
'AT':'center',
89+
'TA':'center',
90+
'GA':'center',
91+
'AG':'center',
92+
'CA':'left',
93+
'AC':'left',
94+
'GT':'left',
95+
'TG':'left',
96+
'CT':'center',
97+
'TC':'center',
98+
'GC':'center',
99+
'CG':'center'
100+
}
101+
102+
103+
def do_fontsize(k):
104+
return float(clip(max_text_size*sqrt(data[k]),\
105+
min_text_size,max_text_size))
106+
107+
A = text(0,1, '$A_3$', color='r', size=do_fontsize('A'), **text_params)
108+
T = text(1,1, '$T_3$', color='k', size=do_fontsize('T'), **text_params)
109+
G = text(0,0, '$G_3$', color='g', size=do_fontsize('G'), **text_params)
110+
C = text(1,0, '$C_3$', color='b', size=do_fontsize('C'), **text_params)
111+
112+
arrow_h_offset = 0.25 #data coordinates, empirically determined
113+
max_arrow_length = 1 - 2*arrow_h_offset
114+
115+
max_arrow_width = max_arrow_width
116+
max_head_width = 2.5*max_arrow_width
117+
max_head_length = 2*max_arrow_width
118+
arrow_params={'length_includes_head':True, 'shape':shape, \
119+
'head_starts_at_zero':head_starts_at_zero}
120+
ax = gca()
121+
sf = 0.6 #max arrow size represents this in data coords
122+
123+
d = (r2/2 + arrow_h_offset - 0.5)/r2 #distance for diags
124+
r2v = arrow_sep/r2 #offset for diags
125+
126+
#tuple of x, y for start position
127+
positions = {\
128+
'AT': (arrow_h_offset, 1+arrow_sep),
129+
'TA': (1-arrow_h_offset, 1-arrow_sep),
130+
'GA': (-arrow_sep, arrow_h_offset),
131+
'AG': (arrow_sep, 1-arrow_h_offset),
132+
'CA': (1-d-r2v, d-r2v),
133+
'AC': (d+r2v, 1-d+r2v),
134+
'GT': (d-r2v, d+r2v),
135+
'TG': (1-d+r2v, 1-d-r2v),
136+
'CT': (1-arrow_sep, arrow_h_offset),
137+
'TC': (1+arrow_sep, 1-arrow_h_offset),
138+
'GC': (arrow_h_offset, arrow_sep),
139+
'CG': (1-arrow_h_offset, -arrow_sep),
140+
}
141+
142+
if normalize_data:
143+
#find maximum value for rates, i.e. where keys are 2 chars long
144+
max_val = 0
145+
for k, v in data.items():
146+
if len(k) == 2:
147+
max_val = max(max_val, v)
148+
#divide rates by max val, multiply by arrow scale factor
149+
for k, v in data.items():
150+
data[k] = v/max_val*sf
151+
152+
def draw_arrow(pair, alpha=alpha, ec=ec, labelcolor=labelcolor):
153+
#set the length of the arrow
154+
if display == 'length':
155+
length = max_head_length+(max_arrow_length-max_head_length)*\
156+
data[pair]/sf
157+
else:
158+
length = max_arrow_length
159+
#set the transparency of the arrow
160+
if display == 'alph':
161+
alpha = min(data[pair]/sf, alpha)
162+
else:
163+
alpha=alpha
164+
#set the width of the arrow
165+
if display == 'width':
166+
scale = data[pair]/sf
167+
width = max_arrow_width*scale
168+
head_width = max_head_width*scale
169+
head_length = max_head_length*scale
170+
else:
171+
width = max_arrow_width
172+
head_width = max_head_width
173+
head_length = max_head_length
174+
175+
fc = colors[pair]
176+
ec = ec or fc
177+
178+
x_scale, y_scale = deltas[pair]
179+
x_pos, y_pos = positions[pair]
180+
arrow(x_pos, y_pos, x_scale*length, y_scale*length, \
181+
fc=fc, ec=ec, alpha=alpha, width=width, head_width=head_width, \
182+
head_length=head_length, **arrow_params)
183+
184+
#figure out coordinates for text
185+
#if drawing relative to base: x and y are same as for arrow
186+
#dx and dy are one arrow width left and up
187+
#need to rotate based on direction of arrow, use x_scale and y_scale
188+
#as sin x and cos x?
189+
sx, cx = y_scale, x_scale
190+
191+
where = label_positions[pair]
192+
if where == 'left':
193+
orig_position = 3*array([[max_arrow_width, max_arrow_width]])
194+
elif where == 'absolute':
195+
orig_position = array([[max_arrow_length/2.0, 3*max_arrow_width]])
196+
elif where == 'right':
197+
orig_position = array([[length-3*max_arrow_width,\
198+
3*max_arrow_width]])
199+
elif where == 'center':
200+
orig_position = array([[length/2.0, 3*max_arrow_width]])
201+
else:
202+
raise ValueError, "Got unknown position parameter %s" % where
203+
204+
205+
206+
M = array([[cx, sx],[-sx,cx]])
207+
coords = dot(orig_position, M) + [[x_pos, y_pos]]
208+
x, y = ravel(coords)
209+
orig_label = rate_labels[pair]
210+
label = '$%s_{_{\mathrm{%s}}}$' % (orig_label[0], orig_label[1:])
211+
212+
text(x, y, label, size=label_text_size, ha='center', va='center', \
213+
color=labelcolor or fc)
214+
215+
for p in positions.keys():
216+
draw_arrow(p)
217+
218+
#test data
219+
all_on_max = dict([(i, 1) for i in 'TCAG'] + \
220+
[(i+j, 0.6) for i in 'TCAG' for j in 'TCAG'])
221+
222+
realistic_data = {
223+
'A':0.4,
224+
'T':0.3,
225+
'G':0.5,
226+
'C':0.2,
227+
'AT':0.4,
228+
'AC':0.3,
229+
'AG':0.2,
230+
'TA':0.2,
231+
'TC':0.3,
232+
'TG':0.4,
233+
'CT':0.2,
234+
'CG':0.3,
235+
'CA':0.2,
236+
'GA':0.1,
237+
'GT':0.4,
238+
'GC':0.1,
239+
}
240+
241+
extreme_data = {
242+
'A':0.75,
243+
'T':0.10,
244+
'G':0.10,
245+
'C':0.05,
246+
'AT':0.6,
247+
'AC':0.3,
248+
'AG':0.1,
249+
'TA':0.02,
250+
'TC':0.3,
251+
'TG':0.01,
252+
'CT':0.2,
253+
'CG':0.5,
254+
'CA':0.2,
255+
'GA':0.1,
256+
'GT':0.4,
257+
'GC':0.2,
258+
}
259+
260+
sample_data = {
261+
'A':0.2137,
262+
'T':0.3541,
263+
'G':0.1946,
264+
'C':0.2376,
265+
'AT':0.0228,
266+
'AC':0.0684,
267+
'AG':0.2056,
268+
'TA':0.0315,
269+
'TC':0.0629,
270+
'TG':0.0315,
271+
'CT':0.1355,
272+
'CG':0.0401,
273+
'CA':0.0703,
274+
'GA':0.1824,
275+
'GT':0.0387,
276+
'GC':0.1106,
277+
}
278+
279+
280+
if __name__ == '__main__':
281+
from sys import argv
282+
d = None
283+
if len(argv) > 1:
284+
if argv[1] == 'full':
285+
d = all_on_max
286+
scaled = False
287+
elif argv[1] == 'extreme':
288+
d = extreme_data
289+
scaled = False
290+
elif argv[1] == 'realistic':
291+
d = realistic_data
292+
scaled = False
293+
elif argv[1] == 'sample':
294+
d = sample_data
295+
scaled = True
296+
if d is None:
297+
d = all_on_max
298+
scaled=False
299+
if len(argv) > 2:
300+
display = argv[2]
301+
else:
302+
display = 'length'
303+
304+
size = 4
305+
figure(figsize=(size,size))
306+
307+
make_arrow_plot(d, display=display, linewidth=0.001, edgecolor=None,
308+
normalize_data=scaled, head_starts_at_zero=True, size=size)
309+
310+
draw()
311+
#savefig('arrows.png')
312+
#print 'Example saved to file "arrows.png"'
313+
show()

doc/pyplots/axhspan_demo.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
t = np.arange(-1,2, .01)
5+
s = np.sin(2*np.pi*t)
6+
7+
plt.plot(t,s)
8+
# draw a thick red hline at y=0 that spans the xrange
9+
l = plt.axhline(linewidth=4, color='r')
10+
11+
# draw a default hline at y=1 that spans the xrange
12+
l = plt.axhline(y=1)
13+
14+
# draw a default vline at x=1 that spans the xrange
15+
l = plt.axvline(x=1)
16+
17+
# draw a thick blue vline at x=0 that spans the the upper quadrant of
18+
# the yrange
19+
l = plt.axvline(x=0, ymin=0.75, linewidth=4, color='b')
20+
21+
# draw a default hline at y=.5 that spans the the middle half of
22+
# the axes
23+
l = plt.axhline(y=.5, xmin=0.25, xmax=0.75)
24+
25+
p = plt.axhspan(0.25, 0.75, facecolor='0.5', alpha=0.5)
26+
27+
p = plt.axvspan(1.25, 1.55, facecolor='g', alpha=0.5)
28+
29+
plt.axis([-1,2,-1,2])
30+
31+
32+
plt.show()

doc/pyplots/bar_stacked.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python
2+
# a stacked bar plot with errorbars
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
6+
7+
N = 5
8+
menMeans = (20, 35, 30, 35, 27)
9+
womenMeans = (25, 32, 34, 20, 25)
10+
menStd = (2, 3, 4, 1, 2)
11+
womenStd = (3, 5, 2, 3, 3)
12+
ind = np.arange(N) # the x locations for the groups
13+
width = 0.35 # the width of the bars: can also be len(x) sequence
14+
15+
p1 = plt.bar(ind, menMeans, width, color='r', yerr=womenStd)
16+
p2 = plt.bar(ind, womenMeans, width, color='y',
17+
bottom=menMeans, yerr=menStd)
18+
19+
plt.ylabel('Scores')
20+
plt.title('Scores by group and gender')
21+
plt.xticks(ind+width/2., ('G1', 'G2', 'G3', 'G4', 'G5') )
22+
plt.yticks(np.arange(0,81,10))
23+
plt.legend( (p1[0], p2[0]), ('Men', 'Women') )
24+
25+
plt.show()

0 commit comments

Comments
 (0)