Code Monkey home page Code Monkey logo

matplotlib4j's Introduction

matplotlib4j

Maven Central Build Status License: MIT

A simplest interface library to enable your java project to use matplotlib.

Of course it is able to be imported to scala project as below. The API is designed as similar to the original matplotlib's.

How to use

Here is an example. Find more examples on MainTest.java

Plot plt = Plot.create();
plt.plot()
    .add(Arrays.asList(1.3, 2))
    .label("label")
    .linestyle("--");
plt.xlabel("xlabel");
plt.ylabel("ylabel");
plt.text(0.5, 0.2, "text");
plt.title("Title!");
plt.legend();
plt.show();

Another example to draw Contour.

// Data generation
List<Double> x = NumpyUtils.linspace(-1, 1, 100);
List<Double> y = NumpyUtils.linspace(-1, 1, 100);
NumpyUtils.Grid<Double> grid = NumpyUtils.meshgrid(x, y);

List<List<Double>> zCalced = grid.calcZ((xi, yj) -> Math.sqrt(xi * xi + yj * yj));

// Plotting
Plot plt = Plot.create();
ContourBuilder contour = plt.contour().add(x, y, zCalced);
plt.clabel(contour)
    .inline(true)
    .fontsize(10);
plt.title("contour");
plt.legend().loc("upper right");
plt.show();

In addition to the interactive window opened by .show(), .savefig() is also supported. Only one thing to note is that plt.executeSilently() triggers to output figure files after calling .savefig(). This is by design as method chain coding style.

Random rand = new Random();
List<Double> x = IntStream.range(0, 1000).mapToObj(i -> rand.nextGaussian())
        .collect(Collectors.toList());

Plot plt = Plot.create();
plt.hist().add(x).orientation(HistBuilder.Orientation.horizontal);
plt.ylim(-5, 5);
plt.title("histogram");
plt.savefig("/tmp/histogram.png").dpi(200);

// Don't miss this line to output the file!
plt.executeSilently();

This code generates the following picture at /tmp/histogram.png.

Major supported functions

  • plot()
  • pcolor()
  • contour()
  • hist()
  • savefig()
  • subplot()
  • xlim(), ylim(), xscale(), yscale(), xlabel(), ylabel()

Pyenv support

It is possible to choose a python environment to run matplotlib with pyenv and pyenv-virtualenv support. Create Plot object by specifying existing names as follows.

// with pyenv name
Plot plt = Plot.create(PythonConfig.pyenvConfig("anaconda3-4.4.0"));
// with pyenv and virtualenv name
Plot plt = Plot.create(PythonConfig.pyenvVirtualenvConfig("anaconda3-4.4.0", "env_plot"));

Dependency

  • Java 8
  • Python with Matplotlib installed

It may work with almost all not too old Python and Matplotlib versions, but no guarantee. It has been tested with

  • Python 2.7.10, 3.6.1
  • Matplotlib 1.3.1, 2.0.2

If it does not work on your environment, please report that through github issue with the error message and your environment (OS, python and matplotlib versions).

Configure on your project

This library is now found on maven central repository.

Import to your projects as follows.

Maven

<dependency>
    <groupId>com.github.sh0nk</groupId>
    <artifactId>matplotlib4j</artifactId>
    <version>0.4.0</version>
</dependency>

Gradle

compile 'com.github.sh0nk:matplotlib4j:0.4.0'

License

MIT

matplotlib4j's People

Contributors

bernard12 avatar sh0nk avatar tokuhirom avatar vfrico avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.