-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContourPlot.java
More file actions
242 lines (207 loc) · 5.68 KB
/
Copy pathContourPlot.java
File metadata and controls
242 lines (207 loc) · 5.68 KB
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
package pyplot4j.contour;
import static java.lang.String.format;
import java.io.File;
import java.util.ArrayList;
import pyplot4j.util.FileOutput;
import pyplot4j.style.LegendLocation;
import pyplot4j.util.TerminalExecutor;
/**
* matplotlib.pyplot.contour(*args, data=None, **kwargs)
* contour([X, Y,] Z, [levels], **kwargs)
*
* @author Meisam
*
*/
public class ContourPlot {
String title = null ;
String xlabel = null ;
String ylabel = null ;
boolean grid = false ;
boolean cla = false ;
String gridWhich = null ;
String gridAxis = null ;
String xlim = null ;
String ylim = null ;
// legend
boolean legend = false ;
String legendLocation ;
// other properties
boolean tightLayout = false ;
String style ;
// contour series
ArrayList<ContourSeries> contourSeriesCollection ;
int count = 1 ;
public boolean isSubplot = false ;
static int id = 0 ;
public ContourPlot(String title) {
this.title = title ;
contourSeriesCollection = new ArrayList<>() ;
}
public ContourPlot() {
this.title = null ;
contourSeriesCollection = new ArrayList<>() ;
}
public ContourSeries contour(double[] x, double[] y, MeshGrid func) {
ContourSeries series = new ContourSeries(x, y, func) ;
series.setXvar("x"+count).setYvar("y"+count).setZvar("z"+count) ;
series.clabel().name("cs"+count) ;
contourSeriesCollection.add(series) ;
count++ ;
return series ;
}
public ContourSeries contour(double[] x, double[] y, double[][] z) {
ContourSeries series = new ContourSeries(x, y, z) ;
series.setXvar("x"+count).setYvar("y"+count).setZvar("z"+count) ;
series.clabel().name("cs"+count) ;
contourSeriesCollection.add(series) ;
count++ ;
return series ;
}
// clabel of the last contour
public ContourLabel clabel() {
return contourSeriesCollection.get(count-2).clabel() ;
}
public ContourPlot xlabel(String xlabel) {
this.xlabel = xlabel ;
return this ;
}
public ContourPlot ylabel(String ylabel) {
this.ylabel = ylabel ;
return this ;
}
public ContourPlot title(String title) {
this.title = title ;
return this ;
}
public ContourPlot xlim(double xmin, double xmax) {
this.xlim = format("[%f, %f]", xmin, xmax) ;
return this ;
}
public ContourPlot ylim(double ymin, double ymax) {
this.ylim = format("[%f, %f]", ymin, ymax) ;
return this ;
}
public ContourPlot grid(boolean on, String which, String axis) {
this.grid = on ;
this.gridWhich = which ;
this.gridAxis = axis ;
return this ;
}
public ContourPlot grid(boolean on) {
this.grid = on ;
this.gridWhich = "both" ;
this.gridAxis = "both" ;
return this ;
}
public ContourPlot legend(boolean on) {
this.legend = on ;
return this ;
}
public ContourPlot legend(boolean on, String loc) {
this.legend = on ;
this.legendLocation = (loc!=null) ? loc.trim() : null ;
return this ;
}
public ContourPlot legend(boolean on, LegendLocation loc) {
this.legend = on ;
this.legendLocation = (loc!=null) ? loc.toString().trim() : null ;
return this ;
}
public ContourPlot tightLayout() {
this.tightLayout = true ;
return this ;
}
public ContourPlot cla() {
this.cla = true ;
return this ;
}
public void savefig(String fileName) {
if(contourSeriesCollection.isEmpty())
throw new IllegalStateException("ContourPlot data is empty") ;
// open the output stream
int index = fileName.lastIndexOf('.') ;
String pyName = fileName.substring(0, index) ;
FileOutput fo = new FileOutput(pyName+".py") ;
pythonCode(fo);
// show the plot
if(cla) {
fo.println("plt.cla()");
}
fo.println(format("plt.savefig('%s')", fileName));
// close the output stream
fo.close();
// run the python code
Thread thread = new Thread(() -> {
TerminalExecutor.execute("python", fo.getFilename());
}) ;
thread.start();
}
public void show() {
// open the output stream
File file = new File("contour_plot_"+(id++)) ;
file.deleteOnExit();
FileOutput fo = new FileOutput(file) ;
pythonCode(fo);
// show the plot
if(cla) {
fo.println("plt.cla()");
}
fo.println("plt.show()");
// close the output stream
fo.close();
// run the python code
Thread thread = new Thread(() -> {
TerminalExecutor.execute("python", fo.getFilename());
}) ;
thread.start();
}
void pythonCode(FileOutput fo) {
if(!isSubplot) {
fo.println("from sys import platform as sys_pf") ;
fo.println("if sys_pf == 'darwin':") ;
fo.println("\timport matplotlib") ;
fo.println("\tmatplotlib.use('TkAgg')") ;
fo.println("import numpy as np") ;
fo.println("import matplotlib.pyplot as plt");
fo.println();
}
// print x and y values
for(ContourSeries contourSeries : contourSeriesCollection) {
// print x
fo.print(contourSeries.xvar + " = [");
fo.printcomma(contourSeries.x);
fo.print("];") ;
fo.println();
// print y
fo.print(contourSeries.yvar + " = [");
fo.printcomma(contourSeries.y);
fo.print("];") ;
fo.println();
// contour plot
fo.println(contourSeries.toString());
}
// configure the plot
if(title != null)
fo.println(format("plt.title('%s')", title)) ;
if(xlabel != null)
fo.println(format("plt.xlabel('%s')", xlabel)) ;
if(ylabel != null)
fo.println(format("plt.ylabel('%s')", ylabel)) ;
if(xlim != null)
fo.println(format("plt.xlim(%s)", xlim));
if(ylim != null)
fo.println(format("plt.ylim(%s)", ylim));
if(grid)
fo.println(format("plt.grid(%s, which='%s', axis='%s')", "True", gridWhich, gridAxis)) ;
else
fo.println(format("plt.grid(%s, which='%s', axis='%s')", "False", gridWhich, gridAxis)) ;
if(legend) {
if(legendLocation != null)
fo.println(format("plt.legend(loc='%s')", legendLocation)) ;
else
fo.println("plt.legend()");
}
if(tightLayout)
fo.println("plt.tight_layout()");
}
}