Commit 5b8b2520 authored by Rui Vieira's avatar Rui Vieira

Add InterpolationPlot

parent 39e92a16
package org.ruivieira.plotlib;
import java.util.Optional;
public abstract class AbstractPlot implements Plot {
protected Optional<String> colour = Optional.empty();
protected Optional<Double> alpha = Optional.empty();
protected final StringBuilder script = new StringBuilder();
public AbstractPlot setColour(String colour) {
this.colour = Optional.of(colour);
return this;
}
public void renderColour() {
colour.ifPresent(s -> script.append(", color='").append(s).append("'"));
}
public AbstractPlot setAlpha(Double alpha) {
this.alpha = Optional.of(alpha);
return this;
}
public void renderAlpha() {
alpha.ifPresent(s -> script.append(", alpha=").append(s));
}
}
package org.ruivieira.plotlib;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
public class Converter<T> {
public List<String> getConverted() {
return converted;
}
private final List<String> converted;
public Converter(Collection<T> data) {
this.converted = data.stream().map(String::valueOf).collect(Collectors.toList());
}
}
package org.ruivieira.plotlib;
import java.util.Collection;
import java.util.List;
public class InterpolationPlot<T, U> extends AbstractPlot implements Plot {
List<String> xs;
List<String> ys;
int steps;
public InterpolationPlot(Collection<T> x, Collection<U> y, int steps) {
this.xs = new Converter<>(x).getConverted();
this.ys = new Converter<>(y).getConverted();
this.steps = steps;
}
@Override
public String render() {
script.append("from scipy.interpolate import interp1d\n");
script.append("import numpy as np\n");
script.append("_z = interp1d([").append(String.join(",", xs)).append("]");
script.append(", [").append(String.join(",", ys)).append("], kind='cubic')\n");
script.append("_x = np.linspace(").append(xs.get(0)).append(", ").append(xs.get(xs.size()-1)).append(", num=").append(this.steps).append(", endpoint=True)\n");
script.append("plt.plot(_x, _z(_x)");
renderColour();
renderAlpha();
script.append(")\n");
System.out.println(script.toString());
return script.toString();
}
}
......@@ -2,6 +2,6 @@ package org.ruivieira.plotlib;
public interface Plot {
String render();
abstract String render();
}
package org.ruivieira.plotlib;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
public class ScatterPlot<T, U> implements Plot {
public class ScatterPlot<T, U> extends AbstractPlot implements Plot {
List<String> xs = new ArrayList<>();
List<String> ys = new ArrayList<>();
final List<String> xs;
final List<String> ys;
private Optional<String> marker = Optional.empty();
private Optional<String> colour = Optional.empty();
public ScatterPlot(T[] x, U[] y) {
for (T _x : x) {
xs.add(String.valueOf(_x));
}
for (U _y : y) {
ys.add(String.valueOf(_y));
}
}
public ScatterPlot(Collection<T> x, Collection<U> y) {
for (T _x : x) {
xs.add(String.valueOf(_x));
}
for (U _y : y) {
ys.add(String.valueOf(_y));
}
this.xs = new Converter<>(x).getConverted();
this.ys = new Converter<>(y).getConverted();
}
public ScatterPlot setMarker(String marker) {
......@@ -37,17 +24,20 @@ public class ScatterPlot<T, U> implements Plot {
return this;
}
public String render() {
final StringBuilder rendered = new StringBuilder();
rendered.append("plt.scatter([").append(String.join(",", xs));
rendered.append("], [");
rendered.append(String.join(",", ys));
rendered.append("]");
marker.ifPresent(s -> rendered.append(", marker='").append(s).append("'"));
rendered.append(")\n");
System.out.println(rendered.toString());
return rendered.toString();
script.append("plt.scatter([").append(String.join(",", xs));
script.append("], [");
script.append(String.join(",", ys));
script.append("]");
marker.ifPresent(s -> script.append(", marker='").append(s).append("'"));
renderColour();
renderAlpha();
script.append(")\n");
System.out.println(script.toString());
return script.toString();
}
}
......@@ -9,7 +9,7 @@ public class Test {
Integer[] x = new Integer[]{1, 2, 3, 4};
Integer[] y = new Integer[]{7, 9, 5, 6};
figure.add(new ScatterPlot<>(x, y));
// figure.add(new ScatterPl/ot<>(x, y));
// figure.save();
......
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