1 | # ---------------------------------------------------------------------- |
---|
2 | # EXAMPLE: Rappture <curve> elements |
---|
3 | # ====================================================================== |
---|
4 | # AUTHOR: Martin Hunt, Purdue University |
---|
5 | # Copyright (c) 2015 HUBzero Foundation, LLC |
---|
6 | # |
---|
7 | # See the file "license.terms" for information on usage and |
---|
8 | # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
9 | # ====================================================================== |
---|
10 | import Rappture |
---|
11 | import numpy as np |
---|
12 | import sys |
---|
13 | |
---|
14 | # open the XML file containing the run parameters |
---|
15 | rx = Rappture.PyXml(sys.argv[1]) |
---|
16 | |
---|
17 | npts = int(rx['input.(points).current'].value) |
---|
18 | |
---|
19 | # generate a single curve |
---|
20 | single = rx['output.curve(single)'] |
---|
21 | single['about.label'] = "Single Curve" |
---|
22 | single['about.description'] = 'This is an example of a single curve.' |
---|
23 | single['xaxis.label'] = "Time" |
---|
24 | single['xaxis.description'] = "Time during the experiment." |
---|
25 | single['xaxis.units'] = "s" |
---|
26 | single['yaxis.label'] = "Voltage v(11)" |
---|
27 | single['yaxis.description'] = "Output from the amplifier." |
---|
28 | single['yaxis.units'] = "V" |
---|
29 | |
---|
30 | # generate curve |
---|
31 | xmin, xmax = 0.01, 10.0 |
---|
32 | x = np.linspace(xmin, xmax, npts) |
---|
33 | y = np.cos(x)/(1+x) |
---|
34 | |
---|
35 | # plot it |
---|
36 | single['component.xy'] = (x, y) |
---|
37 | |
---|
38 | # These are also acceptable. |
---|
39 | # single['component.xy'] = [x, y] |
---|
40 | # single['component.xy'] = np.row_stack((x, y)) |
---|
41 | |
---|
42 | # generate a single curve with x and y axis upper and lower limits |
---|
43 | xminl = xmin+((xmax-xmin)/4.0) |
---|
44 | xmaxl = xmax-((xmax-xmin)/4.0) |
---|
45 | yminl = np.cos(xminl)/(1+xminl) |
---|
46 | ymaxl = np.cos(xmaxl)/(1+xmaxl) |
---|
47 | limited = rx['output.curve(limited)'] |
---|
48 | limited['about.label'] = "Axis limits curve" |
---|
49 | limited['about.description'] = \ |
---|
50 | 'This is an example of a single curve with x and y axis limits applied.' |
---|
51 | limited['xaxis.label'] = "Time" |
---|
52 | limited['xaxis.description'] = "Time during the experiment." |
---|
53 | limited['xaxis.units'] = "s" |
---|
54 | limited['xaxis.min'] = xminl |
---|
55 | limited['xaxis.max'] = xmaxl |
---|
56 | limited['yaxis.label'] = "Voltage v(11)" |
---|
57 | limited['yaxis.description'] = "Output from the amplifier." |
---|
58 | limited['yaxis.units'] = "V" |
---|
59 | limited['yaxis.min'] = yminl |
---|
60 | limited['yaxis.max'] = ymaxl |
---|
61 | |
---|
62 | # plot it |
---|
63 | limited['component.xy'] = (x, y) |
---|
64 | |
---|
65 | |
---|
66 | # generate multiple curves on the same plot |
---|
67 | for factor in [1, 2]: |
---|
68 | curve = rx['output.curve(multi%s)' % factor] |
---|
69 | curve['about.group'] = "Multiple curve" |
---|
70 | curve['about.label'] = "Factor a=$factor" |
---|
71 | curve['about.description'] = \ |
---|
72 | "This is an example of a multiple curves on the same plot." |
---|
73 | curve['xaxis.label'] = "Frequency" |
---|
74 | curve['xaxis.description'] = \ |
---|
75 | "Frequency of the input source." |
---|
76 | curve['xaxis.units'] = "Hz" |
---|
77 | # curve['xaxis.scale'] = "log" |
---|
78 | curve['yaxis.label'] = "Current" |
---|
79 | curve['yaxis.description'] = \ |
---|
80 | "Current through the pull-down resistor." |
---|
81 | curve['yaxis.units'] = "uA" |
---|
82 | curve['yaxis.log'] = "log" |
---|
83 | y = np.power(2.0, factor*x)/x |
---|
84 | curve['component.xy'] = (x, y) |
---|
85 | |
---|
86 | # generate a scatter curve |
---|
87 | curve = rx['output.curve(scatter)'] |
---|
88 | curve['about.label'] = "Scatter curve" |
---|
89 | curve['about.description'] = \ |
---|
90 | "This is an example of a scatter curve." |
---|
91 | curve['about.type'] = "scatter" |
---|
92 | curve['xaxis.label'] = "Time" |
---|
93 | curve['xaxis.description'] = "Time during the experiment." |
---|
94 | curve['xaxis.units'] = "s" |
---|
95 | curve['yaxis.label'] = "Voltage v(11)" |
---|
96 | curve['yaxis.description'] = "Output from the amplifier." |
---|
97 | curve['yaxis.units'] = "V" |
---|
98 | |
---|
99 | y = np.cos(x)/(1+x) |
---|
100 | curve['component.xy'] = (x, y) |
---|
101 | |
---|
102 | # generate a bar curve |
---|
103 | curve = rx['output.curve(bars)'] |
---|
104 | curve['about.label'] = "Bar chart" |
---|
105 | curve['about.description'] = \ |
---|
106 | "This is an example of a scatter curve." |
---|
107 | curve['about.type'] = "bar" |
---|
108 | curve['xaxis.label'] = "Time" |
---|
109 | curve['xaxis.description'] = "Time during the experiment." |
---|
110 | curve['xaxis.units'] = "s" |
---|
111 | curve['yaxis.label'] = "Voltage v(11)" |
---|
112 | curve['yaxis.description'] = "Output from the amplifier." |
---|
113 | curve['yaxis.units'] = "V" |
---|
114 | |
---|
115 | x = np.arange(0, npts) |
---|
116 | y = np.sin(x)/(1+x) |
---|
117 | curve['component.xy'] = (x, y) |
---|
118 | |
---|
119 | # generate mixed curves on the same plot |
---|
120 | deg2rad = 0.017453292519943295 |
---|
121 | |
---|
122 | curve = rx['output.curve(line)'] |
---|
123 | curve['about.group'] = "Mixed element types" |
---|
124 | curve['about.label'] = "Sine" |
---|
125 | curve['about.description'] = \ |
---|
126 | "This is an example of a mixed curves on the same plot." |
---|
127 | curve['xaxis.label'] = "Degrees" |
---|
128 | # curve['yaxis.label'] = "Sine" |
---|
129 | curve['about.type'] = "line" |
---|
130 | |
---|
131 | x = np.arange(0, 361, 30) |
---|
132 | y = np.sin(x * deg2rad) |
---|
133 | curve['component.xy'] = (x, y) |
---|
134 | |
---|
135 | curve = rx['output.curve(bar)'] |
---|
136 | curve['about.group'] = "Mixed element types" |
---|
137 | curve['about.label'] = "Cosine" |
---|
138 | curve['about.description'] = \ |
---|
139 | "This is an example of a mixed curves on the same plot." |
---|
140 | curve['xaxis.label'] = "Degrees" |
---|
141 | # curve['yaxis.label'] = "Cosine" |
---|
142 | curve['about.type'] = "bar" |
---|
143 | curve['about.style'] = "-barwidth 24.0" |
---|
144 | |
---|
145 | y = np.cos(x * deg2rad) |
---|
146 | curve['component.xy'] = (x, y) |
---|
147 | |
---|
148 | curve = rx['output.curve(point)'] |
---|
149 | curve['about.group'] = "Mixed element types" |
---|
150 | curve['about.label'] = "Random" |
---|
151 | curve['about.description'] = \ |
---|
152 | "This is an example of a mixed curves on the same plot." |
---|
153 | curve['xaxis.label'] = "Degrees" |
---|
154 | # curve['yaxis.label'] = "Random" |
---|
155 | curve['about.type'] = "scatter" |
---|
156 | |
---|
157 | x = np.arange(0, 361, 10) |
---|
158 | y = np.random.rand(len(x)) * 2.0 - 1 |
---|
159 | curve['component.xy'] = (x, y) |
---|
160 | |
---|
161 | # save the updated XML describing the run... |
---|
162 | rx.close() |
---|