Commit bc7d6088 authored by Rui Vieira's avatar Rui Vieira

Add plots for matrices (matshow)

parent 8061edc0
......@@ -6,7 +6,7 @@
<groupId>org.ruivieira</groupId>
<artifactId>java-plotlib</artifactId>
<version>0.0.10</version>
<version>0.0.11</version>
<build>
<plugins>
......@@ -43,6 +43,10 @@
</plugins>
</build>
<properties>
<commons.math.version>3.6.1</commons.math.version>
</properties>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
......@@ -54,6 +58,11 @@
<artifactId>tinylog</artifactId>
<version>1.3.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-math3</artifactId>
<version>${commons.math.version}</version>
</dependency>
</dependencies>
<profiles>
......
package org.ruivieira.plotlib;
import org.ruivieira.plotlib.converters.ListConverter;
import java.util.Collection;
import java.util.Optional;
public abstract class BinaryAbstractPlot<T, U> extends UnaryAbstractPlot<T> implements Plot {
protected final Converter<U> ys;
protected final ListConverter<U> ys;
protected Optional<String> colour = Optional.empty();
protected Optional<Double> alpha = Optional.empty();
......@@ -13,7 +15,7 @@ public abstract class BinaryAbstractPlot<T, U> extends UnaryAbstractPlot<T> impl
public BinaryAbstractPlot(Collection<T> x, Collection<U> y) {
super(x);
this.ys = new Converter<>(y);
this.ys = new ListConverter<>(y);
}
public BinaryAbstractPlot setYLim(Coordinate c) {
......
......@@ -22,7 +22,7 @@ public class Figure {
public StringBuilder script = new StringBuilder();
public Figure() {
script.append("import matplotlib\nimport matplotlib.pyplot as plt\n\n");
script.append("import matplotlib\nimport matplotlib.pyplot as plt\nimport numpy as np\n\n");
}
public void setTitle(String title) {
......
package org.ruivieira.plotlib;
import org.apache.commons.math3.linear.AbstractRealMatrix;
import org.apache.commons.math3.linear.RealMatrix;
import org.ruivieira.plotlib.converters.MatrixConverter;
public abstract class MatrixAbstractPlot implements Plot {
protected final MatrixConverter matrix;
protected final StringBuilder script = new StringBuilder();
public MatrixAbstractPlot(RealMatrix x) {
this.matrix = new MatrixConverter(x);
}
}
package org.ruivieira.plotlib;
import org.apache.commons.math3.linear.RealMatrix;
import org.pmw.tinylog.Configurator;
import org.pmw.tinylog.Level;
import org.pmw.tinylog.writers.ConsoleWriter;
import org.ruivieira.plotlib.plots.Histogram;
import org.ruivieira.plotlib.plots.HorizontalLine;
import org.ruivieira.plotlib.plots.MatShow;
import org.ruivieira.plotlib.plots.ScatterPlot;
import java.io.IOException;
import java.util.Arrays;
import static org.apache.commons.math3.linear.MatrixUtils.createRealIdentityMatrix;
public class Test {
......@@ -29,6 +33,11 @@ public class Test {
Figure figure = new Figure();
figure.add(new ScatterPlot<>(Arrays.asList(x), Arrays.asList(x)));
figure.add(new HorizontalLine<>(10.0).setColour("black").setLineStyle("--"));
figure.save("/tmp/did.png");
figure.show();
RealMatrix m = createRealIdentityMatrix(10);
Figure f = new Figure();
f.add(new MatShow(m));
f.show();
}
}
package org.ruivieira.plotlib;
import org.ruivieira.plotlib.converters.ListConverter;
import java.util.Collection;
public abstract class UnaryAbstractPlot<A> implements Plot {
protected final Converter<A> xs;
protected final ListConverter<A> xs;
protected final StringBuilder script = new StringBuilder();
public UnaryAbstractPlot(Collection<A> x) {
this.xs = new Converter<>(x);
this.xs = new ListConverter<>(x);
}
}
package org.ruivieira.plotlib;
package org.ruivieira.plotlib.converters;
import java.util.Collection;
......@@ -6,7 +6,7 @@ import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
public class Converter<T> {
public class ListConverter<T> {
public List<String> getConverted() {
return converted;
......@@ -15,7 +15,7 @@ public class Converter<T> {
private final List<String> converted;
public Converter(Collection<T> data) {
public ListConverter(Collection<T> data) {
this.converted = data.stream().map(Objects::toString).collect(Collectors.toList());
}
......
package org.ruivieira.plotlib.converters;
import org.apache.commons.math3.linear.AbstractRealMatrix;
import org.apache.commons.math3.linear.RealMatrix;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
public class MatrixConverter<T> {
public List<String> getConverted() {
return converted;
}
private final List<String> converted;
public MatrixConverter(RealMatrix data) {
final double[][] entries = data.getData();
this.converted = new ArrayList<>();
for (int rows = 0 ; rows < data.getRowDimension() ; rows++) {
List<String> rowList = new ArrayList<>();
final double[] rowEntries = data.getRow(rows);
for (int i = 0 ; i < rowEntries.length ; i++) {
rowList.add(String.valueOf(rowEntries[i]));
}
this.converted.add("[" + String.join(",", rowList) + "]");
}
}
public String getConvertedList() {
return "np.array([" + String.join(",", getConverted()) + "])";
}
}
package org.ruivieira.plotlib.plots;
import org.apache.commons.math3.linear.AbstractRealMatrix;
import org.apache.commons.math3.linear.RealMatrix;
import org.ruivieira.plotlib.MatrixAbstractPlot;
public class MatShow extends MatrixAbstractPlot {
public MatShow(RealMatrix x) {
super(x);
}
@Override
public String render() {
script.append("plt.matshow(").append(matrix.getConvertedList()).append(")");
return script.toString();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment