Commit b744ae05 authored by Rui Vieira's avatar Rui Vieira

Fix BufferedImage on Jupyter. Release 0.0.6.

parent b3aee4d6
[![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/ruivieira/java-plotlib/master?filepath=docs%2Fexamples.ipynb)
# java-plotlib
A simple Java wraper for Python's matplotlib.
See the examples [here](docs/examples.ipynb) or run the examples [online](https://mybinder.org/v2/gh/ruivieira/java-plotlib/master?filepath=docs%2Fexamples.ipynb).
\ No newline at end of file
This diff is collapsed.
......@@ -6,7 +6,7 @@
<groupId>org.ruivieira</groupId>
<artifactId>java-plotlib</artifactId>
<version>0.0.5</version>
<version>0.0.6</version>
<build>
<plugins>
......
......@@ -6,8 +6,8 @@ import java.util.Optional;
public abstract class AbstractPlot<T, U> implements Plot {
protected final List<String> xs;
protected final List<String> ys;
protected final Converter<T> xs;
protected final Converter<U> ys;
protected Optional<String> colour = Optional.empty();
......@@ -17,8 +17,8 @@ public abstract class AbstractPlot<T, U> implements Plot {
protected final StringBuilder script = new StringBuilder();
public AbstractPlot(Collection<T> x, Collection<U> y) {
this.xs = new Converter<>(x).getConverted();
this.ys = new Converter<>(y).getConverted();
this.xs = new Converter<>(x);
this.ys = new Converter<>(y);
}
......
package org.ruivieira.plotlib;
public class Arguments {
public static String build(String key, String value) {
final StringBuilder builder = new StringBuilder();
builder.append(key).append("=").append("'").append(value).append("'");
return builder.toString();
}
}
package org.ruivieira.plotlib;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
......@@ -15,7 +16,12 @@ public class Converter<T> {
public Converter(Collection<T> data) {
this.converted = data.stream().map(String::valueOf).collect(Collectors.toList());
}
public String getConvertedList() {
final StringBuilder builder = new StringBuilder();
builder.append("[").append(String.join(",", getConverted())).append("]");
return builder.toString();
}
}
......@@ -2,8 +2,7 @@ package org.ruivieira.plotlib;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.*;
import java.nio.charset.Charset;
import java.util.Optional;
......@@ -37,25 +36,25 @@ public class Figure {
try {
File tempFile = File.createTempFile("java-plotlib-", ".py");
title.ifPresent(s -> script.append("plt.title('").append(s).append("')\n"));
script.append("\n").append("plt.savefig('").append(imageName).append("')");
script.append("\n").append("plt.savefig('").append(imageName).append("', format='png', transparent=False)");
writeStringToFile(tempFile, script.toString(), Charset.defaultCharset());
runtime.exec(python + " " + tempFile.getAbsolutePath());
Process p = runtime.exec(python + " " + tempFile.getAbsolutePath());
p.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public BufferedImage getBufferedImage() {
BufferedImage img = null;
try {
File tempFile = File.createTempFile("java-plotlib-", ".png");
public BufferedImage getBufferedImage() throws IOException {
File tempFile = File.createTempFile("tmp", ".png");
save(tempFile.getAbsolutePath());
img = ImageIO.read(new File(tempFile.getAbsolutePath()));
} catch (IOException e) {
e.printStackTrace();
}
return img;
File savedImage = new File(tempFile.getAbsolutePath());
FileInputStream fis = new FileInputStream(savedImage);
System.out.println(savedImage);
return ImageIO.read(fis);
}
public String getPython() {
......
......@@ -2,12 +2,14 @@ package org.ruivieira.plotlib;
import org.ruivieira.plotlib.plots.ScatterPlot;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
public static void main(String[] args) throws IOException {
Figure figure = new Figure();
......@@ -16,7 +18,11 @@ public class Test {
figure.add(new ScatterPlot<>(Arrays.asList(x), Arrays.asList(y)));
figure.getBufferedImage();
System.out.println(figure.getBufferedImage());
String[] readers = ImageIO.getReaderFormatNames();
for (String reader : readers)
System.out.println("reader: " + reader);
}
}
package org.ruivieira.plotlib.plots;
import org.ruivieira.plotlib.AbstractPlot;
import org.ruivieira.plotlib.Arguments;
import org.ruivieira.plotlib.Plot;
import java.util.Collection;
......@@ -26,9 +27,13 @@ public class InterpolationPlot<T, U> extends AbstractPlot<T, U> implements Plot
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(String.format("_z = interp1d(%s, %s, %s)\n",
xs.getConvertedList(),
ys.getConvertedList(),
Arguments.build("kind", "cubic")));
script.append("_x = np.linspace(").append(xs.getConverted().get(0)).append(", ").append(xs.getConverted().get(xs.getConverted().size()-1)).append(", num=").append(this.steps).append(", endpoint=True)\n");
script.append("plt.plot(_x, _z(_x)");
renderColour();
......
package org.ruivieira.plotlib.plots;
import org.ruivieira.plotlib.AbstractPlot;
import org.ruivieira.plotlib.Arguments;
import org.ruivieira.plotlib.Plot;
import java.util.Collection;
......@@ -27,12 +28,13 @@ public class LinePlot<T, U> extends AbstractPlot<T, U> implements Plot {
@Override
public String render() {
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("'"));
linestyle.ifPresent(s -> script.append(", linestyle='").append(s).append("'"));
script.append("plt.plot(")
.append(xs.getConvertedList())
.append(",")
.append(ys.getConvertedList());
marker.ifPresent(s -> script.append(", ").append(Arguments.build("marker", s)));
linestyle.ifPresent(s -> script.append(", ").append(Arguments.build("linestyle", s)));
renderColour();
renderAlpha();
......
......@@ -30,10 +30,10 @@ public class ScatterPlot<T, U> extends AbstractPlot<T, U> implements Plot {
@Override
public String render() {
script.append("plt.scatter([").append(String.join(",", xs));
script.append("], [");
script.append(String.join(",", ys));
script.append("]");
script.append("plt.scatter(").append(xs.getConvertedList())
.append(",")
.append(ys.getConvertedList());
marker.ifPresent(s -> script.append(", marker='").append(s).append("'"));
markerSize.ifPresent(s -> script.append(", s=").append(s));
......
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