File tree Expand file tree Collapse file tree 9 files changed +106
-10
lines changed
src/main/java/org/ruivieira/plotlib Expand file tree Collapse file tree 9 files changed +106
-10
lines changed Original file line number Diff line number Diff line change 6
6
7
7
<groupId >org.ruivieira</groupId >
8
8
<artifactId >java-plotlib</artifactId >
9
- <version >0.0.10 </version >
9
+ <version >0.0.11 </version >
10
10
11
11
<build >
12
12
<plugins >
43
43
</plugins >
44
44
</build >
45
45
46
+ <properties >
47
+ <commons .math.version>3.6.1</commons .math.version>
48
+ </properties >
49
+
46
50
<dependencies >
47
51
<dependency >
48
52
<groupId >commons-io</groupId >
54
58
<artifactId >tinylog</artifactId >
55
59
<version >1.3.6</version >
56
60
</dependency >
61
+ <dependency >
62
+ <groupId >org.apache.commons</groupId >
63
+ <artifactId >commons-math3</artifactId >
64
+ <version >${commons.math.version} </version >
65
+ </dependency >
57
66
</dependencies >
58
67
59
68
<profiles >
Original file line number Diff line number Diff line change 1
1
package org .ruivieira .plotlib ;
2
2
3
+ import org .ruivieira .plotlib .converters .ListConverter ;
4
+
3
5
import java .util .Collection ;
4
6
import java .util .Optional ;
5
7
6
8
public abstract class BinaryAbstractPlot <T , U > extends UnaryAbstractPlot <T > implements Plot {
7
9
8
- protected final Converter <U > ys ;
10
+ protected final ListConverter <U > ys ;
9
11
10
12
protected Optional <String > colour = Optional .empty ();
11
13
protected Optional <Double > alpha = Optional .empty ();
12
14
protected Optional <Coordinate > ylim = Optional .empty ();
13
15
14
16
public BinaryAbstractPlot (Collection <T > x , Collection <U > y ) {
15
17
super (x );
16
- this .ys = new Converter <>(y );
18
+ this .ys = new ListConverter <>(y );
17
19
}
18
20
19
21
public BinaryAbstractPlot setYLim (Coordinate c ) {
Original file line number Diff line number Diff line change @@ -22,7 +22,7 @@ public class Figure {
22
22
public StringBuilder script = new StringBuilder ();
23
23
24
24
public Figure () {
25
- script .append ("import matplotlib\n import matplotlib.pyplot as plt\n \n " );
25
+ script .append ("import matplotlib\n import matplotlib.pyplot as plt\n import numpy as np \ n\n " );
26
26
}
27
27
28
28
public void setTitle (String title ) {
Original file line number Diff line number Diff line change
1
+ package org .ruivieira .plotlib ;
2
+
3
+ import org .apache .commons .math3 .linear .AbstractRealMatrix ;
4
+ import org .apache .commons .math3 .linear .RealMatrix ;
5
+ import org .ruivieira .plotlib .converters .MatrixConverter ;
6
+
7
+ public abstract class MatrixAbstractPlot implements Plot {
8
+
9
+ protected final MatrixConverter matrix ;
10
+
11
+ protected final StringBuilder script = new StringBuilder ();
12
+
13
+ public MatrixAbstractPlot (RealMatrix x ) {
14
+ this .matrix = new MatrixConverter (x );
15
+ }
16
+ }
Original file line number Diff line number Diff line change 1
1
package org .ruivieira .plotlib ;
2
2
3
+ import org .apache .commons .math3 .linear .RealMatrix ;
3
4
import org .pmw .tinylog .Configurator ;
4
5
import org .pmw .tinylog .Level ;
5
6
import org .pmw .tinylog .writers .ConsoleWriter ;
6
7
import org .ruivieira .plotlib .plots .Histogram ;
7
8
import org .ruivieira .plotlib .plots .HorizontalLine ;
9
+ import org .ruivieira .plotlib .plots .MatShow ;
8
10
import org .ruivieira .plotlib .plots .ScatterPlot ;
9
11
10
12
import java .io .IOException ;
11
13
import java .util .Arrays ;
12
14
15
+ import static org .apache .commons .math3 .linear .MatrixUtils .createRealIdentityMatrix ;
16
+
13
17
14
18
public class Test {
15
19
@@ -29,6 +33,11 @@ public static void main(String[] args) throws IOException {
29
33
Figure figure = new Figure ();
30
34
figure .add (new ScatterPlot <>(Arrays .asList (x ), Arrays .asList (x )));
31
35
figure .add (new HorizontalLine <>(10.0 ).setColour ("black" ).setLineStyle ("--" ));
32
- figure .save ("/tmp/did.png" );
36
+ figure .show ();
37
+
38
+ RealMatrix m = createRealIdentityMatrix (10 );
39
+ Figure f = new Figure ();
40
+ f .add (new MatShow (m ));
41
+ f .show ();
33
42
}
34
43
}
Original file line number Diff line number Diff line change 1
1
package org .ruivieira .plotlib ;
2
2
3
+ import org .ruivieira .plotlib .converters .ListConverter ;
4
+
3
5
import java .util .Collection ;
4
6
5
7
public abstract class UnaryAbstractPlot <A > implements Plot {
6
8
7
- protected final Converter <A > xs ;
9
+ protected final ListConverter <A > xs ;
8
10
9
11
protected final StringBuilder script = new StringBuilder ();
10
12
11
13
public UnaryAbstractPlot (Collection <A > x ) {
12
- this .xs = new Converter <>(x );
14
+ this .xs = new ListConverter <>(x );
13
15
}
14
16
}
Original file line number Diff line number Diff line change 1
- package org .ruivieira .plotlib ;
1
+ package org .ruivieira .plotlib . converters ;
2
2
3
3
4
4
import java .util .Collection ;
5
5
import java .util .List ;
6
6
import java .util .Objects ;
7
7
import java .util .stream .Collectors ;
8
8
9
- public class Converter <T > {
9
+ public class ListConverter <T > {
10
10
11
11
public List <String > getConverted () {
12
12
return converted ;
@@ -15,7 +15,7 @@ public List<String> getConverted() {
15
15
private final List <String > converted ;
16
16
17
17
18
- public Converter (Collection <T > data ) {
18
+ public ListConverter (Collection <T > data ) {
19
19
this .converted = data .stream ().map (Objects ::toString ).collect (Collectors .toList ());
20
20
}
21
21
Original file line number Diff line number Diff line change
1
+ package org .ruivieira .plotlib .converters ;
2
+
3
+
4
+ import org .apache .commons .math3 .linear .AbstractRealMatrix ;
5
+ import org .apache .commons .math3 .linear .RealMatrix ;
6
+
7
+ import java .util .ArrayList ;
8
+ import java .util .Collection ;
9
+ import java .util .List ;
10
+ import java .util .Objects ;
11
+ import java .util .stream .Collectors ;
12
+
13
+ public class MatrixConverter <T > {
14
+
15
+ public List <String > getConverted () {
16
+ return converted ;
17
+ }
18
+
19
+ private final List <String > converted ;
20
+
21
+
22
+ public MatrixConverter (RealMatrix data ) {
23
+ final double [][] entries = data .getData ();
24
+ this .converted = new ArrayList <>();
25
+ for (int rows = 0 ; rows < data .getRowDimension () ; rows ++) {
26
+ List <String > rowList = new ArrayList <>();
27
+ final double [] rowEntries = data .getRow (rows );
28
+ for (int i = 0 ; i < rowEntries .length ; i ++) {
29
+ rowList .add (String .valueOf (rowEntries [i ]));
30
+ }
31
+ this .converted .add ("[" + String .join ("," , rowList ) + "]" );
32
+ }
33
+ }
34
+
35
+ public String getConvertedList () {
36
+ return "np.array([" + String .join ("," , getConverted ()) + "])" ;
37
+ }
38
+
39
+ }
Original file line number Diff line number Diff line change
1
+ package org .ruivieira .plotlib .plots ;
2
+
3
+ import org .apache .commons .math3 .linear .AbstractRealMatrix ;
4
+ import org .apache .commons .math3 .linear .RealMatrix ;
5
+ import org .ruivieira .plotlib .MatrixAbstractPlot ;
6
+
7
+ public class MatShow extends MatrixAbstractPlot {
8
+
9
+ public MatShow (RealMatrix x ) {
10
+ super (x );
11
+ }
12
+
13
+ @ Override
14
+ public String render () {
15
+ script .append ("plt.matshow(" ).append (matrix .getConvertedList ()).append (")" );
16
+
17
+ return script .toString ();
18
+ }
19
+ }
You can’t perform that action at this time.
0 commit comments