Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added basic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 15 additions & 14 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@
{
"target_name": "matplotlib",
"sources": [ "src/matplotlib.cc" ],
"libraries": [
"-ldl"
"include_dirs": [
"<!(python3 -c 'import numpy; print(numpy.get_include())')",
"<!@(node -p \"require('node-addon-api').include\")",
"lib"
],
"conditions": [
['OS=="mac"', {
"xcode_settings": {
"OTHER_CFLAGS": [
"<!(python-config --cflags)"
],
"OTHER_LDFLAGS": [
"<!(python-config --ldflags)"
]
}
"xcode_settings": {
"OTHER_CFLAGS": [
"<!(python3-config --cflags)",
"-fexceptions"
],
"OTHER_LDFLAGS": [
"<!(python3-config --ldflags)"
]
}
}, { # not OSX
"cflags": [
"<!(python-config --cflags)"
],
"libraries": [
"<!(python-config --libs)",
"<!(python3-config --cflags)",
"-fexceptions"
]
}]
]
Expand Down
31 changes: 31 additions & 0 deletions examples/animation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const plt = require('../build/Release/matplotlib');

const n = 1000;
const x = [];
const y = [];
const z = [];

for (let i = 0; i < n; i++) {
x.push(i * i);
y.push(Math.sin(2 * Math.PI * i / 360.0));
z.push(Math.log(i));

if (i % 10 == 0) {
// Clear previous plot
plt.clf();
// Plot line from given x and y data. Color is selected automatically.
plt.plot(x, y);
// Plot a line whose name will show up as 'log(x)' in the legend.
plt.named_plot('log(x)', x, z);

// Set x-axis to interval [0,1000000]
plt.xlim(0, n*n);

// Add graph title
plt.title('Sample figure');
// Enable legend.
plt.legend();
// Display plot continuously
plt.pause(0.01);
}
}
12 changes: 12 additions & 0 deletions examples/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const plt = require('../build/Release/matplotlib');

const x = [];
const y = [];

for (let i = 0; i < 20; i++) {
x.push(i);
y.push(i);
}

plt.bar(x, y);
plt.show();
41 changes: 41 additions & 0 deletions examples/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const plt = require('../build/Release/matplotlib');

// Prepare data.
const n = 5000;
const x = Array(5000);
const y = Array(5000);
const z = Array(5000);
const w = Array(5000).fill(2);

for(let i = 0; i < n; ++i) {
x[i] = i * i;
y[i] = Math.sin(2 * Math.PI * i / 360.0);
z[i] = Math.log(i);
}

// Set the size of output image = 1200x780 pixels
plt.figure_size(1200, 780);

// Plot line from given x and y data. Color is selected automatically.
plt.plot(x, y);

// Plot a red dashed line from given x and y data.
plt.plot(x, w,'r--');

// Plot a line whose name will show up as 'log(x)' in the legend.
plt.named_plot('log(x)', x, z);

// Set x-axis to interval [0,1000000]
plt.xlim(0, 1000*1000);

// Add graph title
plt.title('Sample figure');

// Enable legend.
plt.legend();

// save figure
const filename = './basic.png';
console.log(`Saving result to ${filename}`);

plt.save(filename);
Binary file added examples/basic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions examples/fill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const plt = require('../build/Release/matplotlib');

// Prepare data.
const theta = [];
for (let d = 0; d < 8 * Math.PI; d += 0.1) {
theta.push(d);
}

const a = 1;
const b = 0.2;

for (let dt = 0; dt < 2 * Math.PI; dt += Math.PI / 2.0) {
const x1 = [];
const y1 = [];
const x2 = [];
const y2 = [];

for (th of theta) {
x1.push(a * Math.cos(th + dt) * Math.exp(b * th));
y1.push(a * Math.sin(th + dt) * Math.exp(b * th));

x2.push(a * Math.cos(th + dt + Math.PI / 4.0) * Math.exp(b * th));
y2.push(a * Math.sin(th + dt + Math.PI / 4.0) * Math.exp(b * th));
}

plt.fill(x1.concat(x2.reverse()), y1.concat(y2.reverse()), {});
}

plt.show();
26 changes: 26 additions & 0 deletions examples/fill_between.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const plt = require('../build/Release/matplotlib');

// Prepare data.
const n = 5000;
const x = Array(5000);
const y = Array(5000);
const z = Array(5000);

for (let i = 0; i < n; ++i) {
x[i] = i * i;
y[i] = Math.sin(2 * Math.PI * i / 360.0);
z[i] = Math.log(i);
}

// TODO: find out why it crashes if uncommenting alpha: '0.4'?

// Prepare keywords to pass to PolyCollection. See
// https://matplotlib.org/api/_as_gen/matplotlib.axes.Axes.fill_between.html
const keywords = {
// alpha: '0.4',
color: 'grey',
hatch: '-',
};

plt.fill_between(x, y, z, keywords);
plt.show();
29 changes: 29 additions & 0 deletions examples/lines3d.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const plt = require('../build/Release/matplotlib');

const x = [];
const y = [];
const z = [];

let theta;
let r;
let z_inc = 4.0 / 99.0;
let theta_inc = (8.0 * Math.PI) / 99.0;

for (let i = 0; i < 100; i++) {
theta = -4.0 * Math.PI + theta_inc * i;
z.push(-2.0 + z_inc * i);
r = z[i] * z[i] + 1;
x.push(r * Math.sin(theta));
y.push(r * Math.cos(theta));
}

// TODO: does not look correct... memory problem? conversion problem?

plt.plot3(x, y, z, {
label: 'parametric curve',
});
plt.xlabel('x label');
plt.ylabel('y label');
plt.set_zlabel('z label'); // set_zlabel rather than just zlabel, in accordance with the Axes3D method
plt.legend();
plt.show();
5 changes: 5 additions & 0 deletions examples/minimal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const plt = require('../build/Release/matplotlib');

plt.plot([0, 1, 2, 3], [1, 3, 2, 4]);
plt.show();

19 changes: 19 additions & 0 deletions examples/quiver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const plt = require('../build/Release/matplotlib');

// u and v are respectively the x and y components of the arrows we're plotting
const x = [];
const y = [];
const u = [];
const v = [];

for (let i = -5; i <= 5; i++) {
for (let j = -5; j <= 5; j++) {
x.push(i);
u.push(-i);
y.push(j);
v.push(-j);
}
}

plt.quiver(x, y, u, v);
plt.show();
Binary file removed examples/show.png
Binary file not shown.
28 changes: 28 additions & 0 deletions examples/subplot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const plt = require('../build/Release/matplotlib');

// Prepare data
const n = 500;
const x = Array(n);
const y = Array(n);
const z = Array(n);

for(let i = 0; i < n; ++i) {
x[i] = i;
y[i] = Math.sin(2 * Math.PI * i / 360.0);
z[i] = 100.0 / i;
}

// TODO: fix segfault

// Set the 'super title'
plt.suptitle('My plot');
plt.subplot(1, 2, 1);
plt.plot(x, y);
plt.subplot(1, 2, 2);
plt.plot(x, z);
// Add some text to the plot
plt.text(100, 90, 'Hello!');


// Show plots
plt.show();
Binary file removed examples/subplot.png
Binary file not shown.
60 changes: 60 additions & 0 deletions examples/subplot2grid.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const plt = require('../build/Release/matplotlib');

// Prepare data
const n = 500;
const x = Array(500);
const u = Array(500);
const v = Array(500);
const w = Array(500);

for (let i = 0; i < n; ++i) {
x[i] = i;
u[i] = Math.sin(2 * Math.PI * i / 500.0);
v[i] = 100.0 / i;
w[i] = Math.sin(2 * Math.PI * i / 1000.0);
}

// TODO: fix segfault

// Set the "super title"
plt.suptitle("My plot");

const nrows = 3;
const ncols = 3;
let row = 2;
let col = 2;

plt.subplot2grid(nrows, ncols, row, col);
plt.plot(x, w, {
color: 'green',
linestyle: '-',
});

let spanr = 1;
let spanc = 2;
col = 0;

plt.subplot2grid(nrows, ncols, row, col, spanr, spanc);
plt.plot(x, v, {
color: 'red',
linestyle: '-',
});

spanr = 2;
spanc = 3;
row = 0;
col = 0;

plt.subplot2grid(nrows, ncols, row, col, spanr, spanc);
plt.plot(x, u, {
color: 'blue',
linestyle: '-',
});

// Add some text to the plot
plt.text(100., -0.5, "Hello!");

// Show plots
plt.show();

plt.pause();
16 changes: 16 additions & 0 deletions examples/xkcd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const plt = require('../build/Release/matplotlib');

const t = Array(1000);
const x = Array(t.length);

for (let i = 0; i < t.length; i++) {
t[i] = i / 100.0;
x[i] = Math.sin(2.0 * Math.PI * 1.0 * t[i]);
}

plt.xkcd();
plt.plot(t, x);
plt.title("AN ORDINARY SIN WAVE");
plt.show();

plt.pause();
Loading