Giter Club home page Giter Club logo

matplotj's Introduction

MatplotJ

A shameless copy of the Matplotlib plotting interface, using Tim Molter's XChart library to do the heavy lifting.

I've really written this for my own benefit, because I like the simple interface of Matplotlib. To show how concise it can be, here's how to display a simple XY plot in 3 lines of code (in this example the single array of doubles is used for both the x and y data):

Plotter plt = new Plotter();
plt.plot(new double[] {1, 2, 3});
plt.show();

Example 1

This being Java rather than Python, I've provided a constructor that allows you to define the title, the x-axis label and the y-axis label:

Plotter plt = new Plotter("My Chart Title", "My X-Axis Label", "My Y-Axis Label");
...

Example 2

But if you want to do it the Python way, you can do that too:

Plotter plt = new Plotter();
plt.title("My Chart Title");
plt.xlabel("My X-Axis Label");
plt.ylabel("My Y-Axis Label");
...

Series Legends

Each call to plot creates a new series on the chart, with a default legend of 'Series 1', 'Series 2', etc. If you want to specify the legend yourself, just pass that as a third parameter in the call to plot.

Plotter plt = new Plotter();
plt.plot(new double[] {1, 2, 3}, new double[] {1, 2, 3}, "My Series Legend");
plt.show();

Plot Style

The colour, line-style and marker-style of the plot will default to something sensible, and each time you call plot you'll get a new plot style by default. But if you want to set the style yourself, you can define it using the standard Matlab format string, where the first character defines the colour, the second character defines the marker style, and the remaining characters define the line-style (the marker and line characters are optional). For example, the following code will create a red plot with upright triangular markers and a solid line:

Plotter plt = new Plotter();
plt.plot(new double[] {1, 2, 3}, new double[] {1, 2, 3}, "My Series Legend", "r^-");
plt.show();

Example 3

Supported Plot Styles

Colours

Character Colour
b Blue
r Red
g Green
c Cyan
m Magenta
y Yellow
x Black
w White

Marker Styles

Character Marker Style
o Circle
d Diamond
s Square
v Upside-down triangle
^ Upright triangle
x Cross
+ Plus

If you don't provide a marker character, no marker will be printed.

Line Styles

Characters Line Style
- Solid
-- Dash Dash
-. Dash Dot

If you don't provide a line character, no line will be printed.

Saving the Chart

The chart can be saved to an image file by calling savefig. The supported file formats are bmp, jpg, png and gif. Note that you need to call show before you call savefig because the image file is generated from the displayed chart.

Plotter plt = new Plotter();
plt.plot(new double[] {1, 2, 3}, new double[] {1, 2, 3}, "My Series Legend", "r^-");
plt.show();
plt.savefig("MySampleChart.jpg");

An Example Chart with Multiple Series

int size = 30;
double[] xData1 = new double[size];
double[] xData2 = new double[size];
double[] yData1 = new double[size];
double[] yData2 = new double[size]; 
for (int i = 0; i < size; i++) {
 double radians = (Math.PI / (size / 2) * i);
 xData1[i] = xData2[i] = i - size / 2;
 yData1[i] = -.000001 * Math.sin(radians);
 yData2[i] = -.000001 * Math.cos(radians);
}
   
Plotter plt = new Plotter("Example sin and cos plots", "x", "f(x)");
plt.plot(xData1, yData1, "sin(x)");
plt.plot(xData2, yData2, "cos(x)");
plt.show();

Example 4

Multiple Charts (Subplots)

To create multiple charts on a single figure, use subplots(rows, cols), to set up the matrix of charts, then call subplot(index) to position the plotter on the chart you want to add data to. Note that indexing starts at 1, and the matrix is traversed from left to right and top to bottom.

Those familiar will matlab/matplotlib will notice that I've diverged from the interface here. But I find the matlab api for subplots overly complex, so I've decided not to copy it slavishly.

Plotter plt = new Plotter();
plt.subplots(2, 2);
        
for (int i = 1; i <= 4; i++) {
    double[] x = getConsecutiveValues(200);
    double[] y1 = getRandomWalk(200);
    double[] y2 = getRandomWalk(200);
    
    plt.subplot(i);
    plt.title("Title " + i).xlabel("X Label " + i).ylabel("YLabel " + i);
    plt.plot(x, y1, "Series A" + i, "b-");
    plt.plot(x, y2, "Series B" + i, "r-");
}

plt.show();

Example 5

matplotj's People

Contributors

nigelbcurrie avatar

Watchers

 avatar  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.