-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathJGnuplotDemo.java
299 lines (280 loc) · 8.96 KB
/
JGnuplotDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package org.leores.demo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.leores.plot.JGnuplot;
import org.leores.plot.JGnuplot.Plot;
import org.leores.util.U;
import org.leores.util.able.Processable2;
import org.leores.util.data.DataTable;
import org.leores.util.data.DataTableSet;
public class JGnuplotDemo extends Demo {
Plot plot1, plot2;
public JGnuplotDemo() {
prepPlot();
}
public void plot2d() {
JGnuplot jg = new JGnuplot() {
{
terminal = "pngcairo enhanced dashed";
output = "plot2d.png";
}
};
Plot plot = new Plot("") {
{
xlabel = "x";
ylabel = "y";
}
};
double[] x = { 1, 2, 3, 4, 5 }, y1 = { 2, 4, 6, 8, 10 }, y2 = { 3, 6, 9, 12, 15 };
DataTableSet dts = plot.addNewDataTableSet("2D Plot");
dts.addNewDataTable("y=2x", x, y1);
dts.addNewDataTable("y=3x", x, y2);
jg.execute(plot, jg.plot2d);
}
public void plot2dBar() {
JGnuplot jg = new JGnuplot() {
{
terminal = "pngcairo enhanced dashed";
output = "plot2dBar.png";
}
};
Plot plot = new Plot("") {
{
xlabel = "x";
ylabel = "y";
yrange = "[0:15]";
extra2 = "set key top left";
}
};
double[] x = { 1, 2, 3, 4, 5 }, y1 = { 2, 4, 6, 8, 10 }, y2 = { 3, 6, 9, 12, 15 };
DataTableSet dts = plot.addNewDataTableSet("2D Bar");
DataTable dt = dts.addNewDataTable("", x, y1, y2);
dt.insert(0, new String[] { "", "y1=2x", "y2=3x" });
jg.execute(plot, jg.plot2dBar);
}
public void plot3d() {
JGnuplot jg = new JGnuplot() {
{
terminal = "pngcairo enhanced dashed";
output = "plot3d.png";
}
};
Plot plot = new Plot("") {
{
xlabel = "x";
ylabel = "y";
zlabel = "z";
}
};
double[] x = { 1, 2, 3, 4, 5 }, y = { 2, 4, 6, 8, 10 }, z = { 3, 6, 9, 12, 15 }, z2 = { 2, 8, 18, 32, 50 };
DataTableSet dts = plot.addNewDataTableSet("3D Plot");
dts.addNewDataTable("z=x+y", x, y, z);
dts.addNewDataTable("z=x*y", x, y, z2);
jg.execute(plot, jg.plot3d);
}
public void plotDensity() {
JGnuplot jg = new JGnuplot() {
{
terminal = "pngcairo enhanced dashed";
output = "plotDensity.png";
}
};
Plot plot = new Plot("") {
{
xlabel = "x";
ylabel = "y";
zlabel = "z=x^2+y^2";
}
};
DataTableSet dts = plot.addNewDataTableSet("Density Plot");
//prepare data
List x = new ArrayList(), y = new ArrayList(), z = new ArrayList();
for (double i = -2; i <= 2; i += 0.5) {
for (double j = -2; j <= 2; j += 0.5) {
x.add(i);
y.add(j);
z.add(i * i + j * j);
}
}
dts.addNewDataTable("z=x^2+y^2", x, y, z);
jg.execute(plot, jg.plotDensity);
}
public void plotImage() {
JGnuplot jg = new JGnuplot() {
{
terminal = "pngcairo enhanced dashed";
output = "plotImage.png";
}
};
Plot plot = new Plot("") {
{
xlabel = "x";
ylabel = "y";
zlabel = "z=x^2+y^2";
}
};
DataTableSet dts = plot.addNewDataTableSet("Image Plot");
//prepare data
List x = new ArrayList(), y = new ArrayList(), z = new ArrayList();
for (double i = -2; i <= 2; i += 0.5) {
for (double j = -2; j <= 2; j += 0.5) {
x.add(i);
y.add(j);
z.add(i * i + j * j);
}
}
dts.addNewDataTable("z=x^2+y^2", x, y, z);
jg.execute(plot, jg.plotImage);
}
public void simple() {
plot2d();
plot2dBar();
plot3d();
plotDensity();
plotImage();
}
public void prepPlot() {
plot1 = new Plot("plot1") {
{
xlabel = "x axis";
ylabel = "y axis";
extra2 = "set key top left";
}
};
//DataTableSet 1 2d add data one by one
DataTableSet dts1 = plot1.addNewDataTableSet("DataTableSet 2d");//use null to avoid the output of figure title for this dataset.
DataTable dt1 = dts1.addNewDataTable("x", 2);
DataTable dt2 = dts1.addNewDataTable("2x", 2);
DataTable dt3 = dts1.addNewDataTable("3x", 2);
DataTable dt4 = dts1.addNewDataTable("4x", 2);
DataTable dt5 = dts1.addNewDataTable("5x", 2);
DataTable dt6 = dts1.addNewDataTable("6x", 2);
DataTable dt7 = dts1.addNewDataTable("7x", 2);
DataTable dt8 = dts1.addNewDataTable("8x", 2);
DataTable dt9 = dts1.addNewDataTable("9x", 2);
DataTable dt10 = dts1.addNewDataTable("10x", 2);
for (int i = 0; i < 5; i++) {
dt1.add(i, i);
dt2.add(i, 2 * i);
dt3.add(i, 3 * i);
dt4.add(i, 4 * i);
dt5.add(i, 5 * i);
dt6.add(i, 6 * i);
dt7.add(i, 7 * i);
dt8.add(i, 8 * i);
dt9.add(i, 9 * i);
dt10.add(i, 10 * i);
}
//DataTableSet 2 3d add data using prepared lists
DataTableSet dts2 = plot1.addNewDataTableSet("DataTableSet 3d");
List x = new ArrayList(), y = new ArrayList(), z1 = new ArrayList(), z2 = new ArrayList();
for (double i = -2; i <= 2; i += 0.5) {
for (double j = -2; j <= 2; j += 0.5) {
x.add(i);
y.add(j);
z1.add(i * i + j * j);
z2.add(4 + i * i + j * j);
}
}
Processable2 pa2 = new Processable2.SampleListByIndex(3);
dts2.addNewDataTable("x^2+y^2", pa2, x, y, z1);
dts2.addNewDataTable("4+x^2+y^2", x, y, z2);
DataTableSet dts3 = dts1;
plot1.add(dts3);
plot2 = new Plot("plot2");
plot2.load("xlabel=x axis;ylabel=y axis;zlabel=z axis");
plot2.add(dts2);
}
/**
* Output the generated gnuplot script file without executing it.
*/
public void compile() {
JGnuplot jg = new JGnuplot();
jg.compile(plot1, jg.plot2d);//if file name is omitted it will use plot.info+".plt" as the file name.
jg.compile(plot2, jg.plot3d);
jg.compile(plot2, jg.plotDensity, "plot2density.plt");
jg.compile(plot1, jg.multiplot, "jgnuplot3.plt");
}
/**
* Shows how to use both synchronized and asynchronized running of Gnuplot.
* (Synchronized: your java program will wait until you close the popped
* Gnuplot window; Asynchronized: you java program will not wait.)
*/
public void execute() {
JGnuplot jg = new JGnuplot();
jg.terminal = "wxt enhanced";
jg.execute(plot1, jg.plot2d, jg.JG_InNewThread | jg.JG_Pause);
jg.execute(plot2, jg.plot3d);
jg.beforeStyleVar = "ls1=10;ls10=1;ls2=9;ls9=2;";
jg.executeA(plot1, jg.plot2d);//Asynchronous plot in a new thread
jg.beforeStyleVar = null;
jg.extra = "set label \"Dynamically add extra code using the extra field.\" at graph 0.5,0.5 center";
//jg.extra = "set style line 1 lc rgbcolor 'greenyellow' lt 1 lw 2 pt 1";
jg.execute(plot1, jg.plot2d);
jg.execute(plot2, jg.plot3d);
jg.terminal = "wxt enhanced size 800,600;";
jg.execute(plot1, jg.multiplot);//show gnuplot warning in the java console. warning can be easily solved by extending the canvas size
}
/**
* Show different available terminals to plot. Please refer to the Gnuplot
* website for the complete list of terminals.
*/
public void terminals() {
JGnuplot jg = new JGnuplot();
jg.execute(plot1, jg.plot2d);//Using the default terminal
//windows terminal is only available to windows. You might get error output from gnuplot if you are not using windows.
jg.terminal = "windows enhanced dashed title 'id=100 hello there' size 600,600";
jg.beforeStyle = "linewidth=4";
jg.execute(plot1, jg.plot2d, ~jg.JG_DeleteTempFile);
jg.terminal = null;//Set the terminal to the default terminal
jg.execute(plot1, jg.plot2d);//wxt terminal default size 640,384
jg.terminal = "dumb";//ascii art terminal for anything that prints text
jg.execute(plot1, jg.plot2d);
jg.terminal = "jpeg enhanced size 600,600";
jg.output = "plot1.jpg";
jg.execute(plot1, jg.plot2d);
jg.terminal = "pngcairo enhanced dashed size 600,600";
jg.output = "plot1.png";
jg.execute(plot1, jg.plot2d);
jg.output = "plot2.png";
jg.execute(plot2, jg.plot3d);
//the size unit for pdf is Inch. It is different from other terminals. The default pdf size is 5,3.
jg.terminal = "pdfcairo enhanced dashed size 6,6";
jg.output = "plot1.pdf";
jg.execute(plot1, jg.plot2d);
jg.output = "plot2.pdf";
jg.execute(plot2, jg.plot3d);
jg.output = "plot3.pdf";
jg.execute(plot1, jg.multiplot);
}
/**
* Customised plot functions.
*/
public void plotx() {
//way1: through the JGunplot.plotx field.
JGnuplot jg = new JGnuplot();
jg.plotx = "$header$\n plot for [i=1:$size(1)$] '-' title info2(1,i).' plotx' w lp ls i\n $data(1,2d)$";
jg.execute(plot1, jg.plotx);
//way2: through a sub class extending JGunplot
class JGnuplot2 extends JGnuplot {
String myplot, rawcode;//No need to declare public/protected/private for these fields.
public void initialize() {
String sFLoad2 = "jgnuplot2.xml";
//U.copyFileFromClassPath(this, sFLoad2, sFLoad2, false);//this also works in a jar file
U.loadFromXML(this, sFLoad2, false);
}
}
JGnuplot2 jg2 = new JGnuplot2();
jg2.execute(plot1, jg2.myplot);
jg2.execute(new Plot(null), jg2.rawcode);
}
public static void demo() {
JGnuplotDemo jgd = new JGnuplotDemo();
jgd.simple();
//jgd.compile();
//jgd.execute();
//jgd.terminals();
//jgd.plotx();
}
}