Commit adc34616 authored by Rui Vieira's avatar Rui Vieira

Add y limit operator

parent 5b8b2520
......@@ -6,6 +6,7 @@ public abstract class AbstractPlot implements Plot {
protected Optional<String> colour = Optional.empty();
protected Optional<Double> alpha = Optional.empty();
protected Optional<Coordinate> ylim = Optional.empty();
protected final StringBuilder script = new StringBuilder();
......@@ -14,6 +15,15 @@ public abstract class AbstractPlot implements Plot {
return this;
}
public AbstractPlot setYLim(Coordinate c) {
this.ylim = Optional.of(c);
return this;
}
public void renderYLim() {
ylim.ifPresent(s -> script.append("plt.ylim").append(s.toString()).append("\n"));
}
public void renderColour() {
colour.ifPresent(s -> script.append(", color='").append(s).append("'"));
}
......
package org.ruivieira.plotlib;
public class Coordinate {
public double getMin() {
return min;
}
public double getMax() {
return max;
}
private final double min;
private final double max;
@Override
public String toString() {
return "(" + min + ", " + max + ")";
}
public Coordinate(double min, double max) {
this.min = min;
this.max = max;
}
}
......@@ -3,17 +3,24 @@ package org.ruivieira.plotlib;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Optional;
import static org.apache.commons.io.FileUtils.writeStringToFile;
public class Figure {
private Optional<String> title = Optional.empty();
public StringBuilder script = new StringBuilder();
public Figure() {
script.append("import matplotlib\nimport matplotlib.pyplot as plt\n\n");
}
public void setTitle(String title) {
this.title = Optional.of(title);
}
public void add(Plot plot) {
script.append(plot.render());
}
......@@ -23,6 +30,7 @@ public class Figure {
File tempFile = File.createTempFile("java-plotlib-", ".py");
System.out.println(tempFile.getAbsolutePath());
System.out.println(tempFile.getName());
title.ifPresent(s -> script.append("plt.title('").append(s).append("')\n"));
script.append("\n").append("plt.savefig('").append(imageName).append("')");
writeStringToFile(tempFile, script.toString(), Charset.defaultCharset());
Process p = Runtime.getRuntime().exec("/usr/local/bin/python3 " + tempFile.getAbsolutePath());
......
package org.ruivieira.plotlib;
package org.ruivieira.plotlib.plots;
import org.ruivieira.plotlib.AbstractPlot;
import org.ruivieira.plotlib.Converter;
import org.ruivieira.plotlib.Plot;
import java.util.Collection;
import java.util.List;
......@@ -29,6 +33,9 @@ public class InterpolationPlot<T, U> extends AbstractPlot implements Plot {
script.append(")\n");
renderYLim();
System.out.println(script.toString());
return script.toString();
}
......
package org.ruivieira.plotlib;
package org.ruivieira.plotlib.plots;
import org.ruivieira.plotlib.AbstractPlot;
import org.ruivieira.plotlib.Converter;
import org.ruivieira.plotlib.Plot;
import java.util.Collection;
import java.util.List;
......@@ -36,6 +40,9 @@ public class ScatterPlot<T, U> extends AbstractPlot implements Plot {
renderAlpha();
script.append(")\n");
renderYLim();
System.out.println(script.toString());
return script.toString();
}
......
package org.ruivieira.plotlib.plots;
import org.ruivieira.plotlib.Plot;
public class YLim implements Plot {
private final double min;
private final double max;
public YLim(double min, double max) {
this.min = min;
this.max = max;
}
@Override
public String render() {
StringBuilder script = new StringBuilder();
script.append("plt.ylim(").append(min).append(", ").append(max).append(")\n");
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