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 # 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 @@ ...@@ -6,7 +6,7 @@
<groupId>org.ruivieira</groupId> <groupId>org.ruivieira</groupId>
<artifactId>java-plotlib</artifactId> <artifactId>java-plotlib</artifactId>
<version>0.0.5</version> <version>0.0.6</version>
<build> <build>
<plugins> <plugins>
......
...@@ -6,8 +6,8 @@ import java.util.Optional; ...@@ -6,8 +6,8 @@ import java.util.Optional;
public abstract class AbstractPlot<T, U> implements Plot { public abstract class AbstractPlot<T, U> implements Plot {
protected final List<String> xs; protected final Converter<T> xs;
protected final List<String> ys; protected final Converter<U> ys;
protected Optional<String> colour = Optional.empty(); protected Optional<String> colour = Optional.empty();
...@@ -17,8 +17,8 @@ public abstract class AbstractPlot<T, U> implements Plot { ...@@ -17,8 +17,8 @@ public abstract class AbstractPlot<T, U> implements Plot {
protected final StringBuilder script = new StringBuilder(); protected final StringBuilder script = new StringBuilder();
public AbstractPlot(Collection<T> x, Collection<U> y) { public AbstractPlot(Collection<T> x, Collection<U> y) {
this.xs = new Converter<>(x).getConverted(); this.xs = new Converter<>(x);
this.ys = new Converter<>(y).getConverted(); 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; package org.ruivieira.plotlib;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -15,7 +16,12 @@ public class Converter<T> { ...@@ -15,7 +16,12 @@ public class Converter<T> {
public Converter(Collection<T> data) { public Converter(Collection<T> data) {
this.converted = data.stream().map(String::valueOf).collect(Collectors.toList()); 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; ...@@ -2,8 +2,7 @@ package org.ruivieira.plotlib;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.*;
import java.io.IOException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.Optional; import java.util.Optional;
...@@ -37,25 +36,25 @@ public class Figure { ...@@ -37,25 +36,25 @@ public class Figure {
try { try {
File tempFile = File.createTempFile("java-plotlib-", ".py"); File tempFile = File.createTempFile("java-plotlib-", ".py");
title.ifPresent(s -> script.append("plt.title('").append(s).append("')\n")); 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()); writeStringToFile(tempFile, script.toString(), Charset.defaultCharset());
runtime.exec(python + " " + tempFile.getAbsolutePath()); Process p = runtime.exec(python + " " + tempFile.getAbsolutePath());
p.waitFor();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} }
} }
public BufferedImage getBufferedImage() { public BufferedImage getBufferedImage() throws IOException {
BufferedImage img = null; File tempFile = File.createTempFile("tmp", ".png");
try {
File tempFile = File.createTempFile("java-plotlib-", ".png");
save(tempFile.getAbsolutePath()); save(tempFile.getAbsolutePath());
img = ImageIO.read(new File(tempFile.getAbsolutePath())); File savedImage = new File(tempFile.getAbsolutePath());
} catch (IOException e) { FileInputStream fis = new FileInputStream(savedImage);
e.printStackTrace(); System.out.println(savedImage);
} return ImageIO.read(fis);
return img;
} }
public String getPython() { public String getPython() {
......
...@@ -2,12 +2,14 @@ package org.ruivieira.plotlib; ...@@ -2,12 +2,14 @@ package org.ruivieira.plotlib;
import org.ruivieira.plotlib.plots.ScatterPlot; import org.ruivieira.plotlib.plots.ScatterPlot;
import javax.imageio.ImageIO;
import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
public class Test { public class Test {
public static void main(String[] args) { public static void main(String[] args) throws IOException {
Figure figure = new Figure(); Figure figure = new Figure();
...@@ -16,7 +18,11 @@ public class Test { ...@@ -16,7 +18,11 @@ public class Test {
figure.add(new ScatterPlot<>(Arrays.asList(x), Arrays.asList(y))); 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; package org.ruivieira.plotlib.plots;
import org.ruivieira.plotlib.AbstractPlot; import org.ruivieira.plotlib.AbstractPlot;
import org.ruivieira.plotlib.Arguments;
import org.ruivieira.plotlib.Plot; import org.ruivieira.plotlib.Plot;
import java.util.Collection; import java.util.Collection;
...@@ -26,9 +27,13 @@ public class InterpolationPlot<T, U> extends AbstractPlot<T, U> implements Plot ...@@ -26,9 +27,13 @@ public class InterpolationPlot<T, U> extends AbstractPlot<T, U> implements Plot
public String render() { public String render() {
script.append("from scipy.interpolate import interp1d\n"); script.append("from scipy.interpolate import interp1d\n");
script.append("import numpy as np\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(String.format("_z = interp1d(%s, %s, %s)\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"); 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)"); script.append("plt.plot(_x, _z(_x)");
renderColour(); renderColour();
......
package org.ruivieira.plotlib.plots; package org.ruivieira.plotlib.plots;
import org.ruivieira.plotlib.AbstractPlot; import org.ruivieira.plotlib.AbstractPlot;
import org.ruivieira.plotlib.Arguments;
import org.ruivieira.plotlib.Plot; import org.ruivieira.plotlib.Plot;
import java.util.Collection; import java.util.Collection;
...@@ -27,12 +28,13 @@ public class LinePlot<T, U> extends AbstractPlot<T, U> implements Plot { ...@@ -27,12 +28,13 @@ public class LinePlot<T, U> extends AbstractPlot<T, U> implements Plot {
@Override @Override
public String render() { public String render() {
script.append("plt.scatter([").append(String.join(",", xs)); script.append("plt.plot(")
script.append("], ["); .append(xs.getConvertedList())
script.append(String.join(",", ys)); .append(",")
script.append("]"); .append(ys.getConvertedList());
marker.ifPresent(s -> script.append(", marker='").append(s).append("'"));
linestyle.ifPresent(s -> script.append(", linestyle='").append(s).append("'")); marker.ifPresent(s -> script.append(", ").append(Arguments.build("marker", s)));
linestyle.ifPresent(s -> script.append(", ").append(Arguments.build("linestyle", s)));
renderColour(); renderColour();
renderAlpha(); renderAlpha();
......
...@@ -30,10 +30,10 @@ public class ScatterPlot<T, U> extends AbstractPlot<T, U> implements Plot { ...@@ -30,10 +30,10 @@ public class ScatterPlot<T, U> extends AbstractPlot<T, U> implements Plot {
@Override @Override
public String render() { public String render() {
script.append("plt.scatter([").append(String.join(",", xs)); script.append("plt.scatter(").append(xs.getConvertedList())
script.append("], ["); .append(",")
script.append(String.join(",", ys)); .append(ys.getConvertedList());
script.append("]");
marker.ifPresent(s -> script.append(", marker='").append(s).append("'")); marker.ifPresent(s -> script.append(", marker='").append(s).append("'"));
markerSize.ifPresent(s -> script.append(", s=").append(s)); 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