Skip to content

Commit ac87084

Browse files
committed
matplotlib: bring back plot() and linspace()
1 parent 1722712 commit ac87084

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/matplotlib.ts

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*!
2+
Copyright 2018 Propel http://propel.site/. All rights reserved.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
import { getOutputHandler, PlotData } from "./output_handler";
17+
import { isTensor } from "./util";
18+
19+
export function plot(...args) {
20+
if (!getOutputHandler()) {
21+
console.warn("plot: no output handler");
22+
return;
23+
}
24+
25+
const xs = [];
26+
const ys = [];
27+
let state = "x";
28+
for (let i = 0; i < args.length; i++) {
29+
const arg = args[i];
30+
switch (state) {
31+
case "x":
32+
xs.push(arg);
33+
state = "y";
34+
break;
35+
36+
case "y":
37+
ys.push(arg);
38+
state = "x";
39+
break;
40+
}
41+
}
42+
43+
const nSeries = Math.min(xs.length, ys.length);
44+
const data: PlotData = [];
45+
for (let i = 0; i < nSeries; ++i) {
46+
// TODO line = $.stack([xs[i], ys[i]], 1)
47+
const xv = isTensor(xs[i]) ? xs[i].dataSync() : xs[i];
48+
const yv = isTensor(ys[i]) ? ys[i].dataSync() : ys[i];
49+
const nPoints = Math.min(xv.length, yv.length);
50+
const line = [];
51+
for (let j = 0; j < nPoints; ++j) {
52+
line.push({ x: xv[j], y: yv[j] });
53+
}
54+
data.push(line);
55+
}
56+
57+
getOutputHandler().plot(data);
58+
}
59+
60+
export function linspace(start: number, stop: number, num: number): number[] {
61+
const d = (stop - start) / (num - 1);
62+
const values = new Array(num);
63+
for (let i = 0; i <= num - 1; ++i) {
64+
values[i] = start + i * d;
65+
}
66+
return values;
67+
}

src/sandbox.ts

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import * as test_internals from "./test_internals";
1717

1818
import { fetchArrayBuffer } from "./fetch";
19+
import * as matplotlib from "./matplotlib";
1920
import { Transpiler } from "./nb_transpiler";
2021
import { setOutputHandler } from "./output_handler";
2122
import { RPC, WindowRPC } from "./rpc";
@@ -33,6 +34,7 @@ const moduleCache: { [name: string]: any } = Object.create(null);
3334
async function importModule(target: string) {
3435
// Check whether module is a built-in.
3536
switch (target) {
37+
case "matplotlib": return matplotlib;
3638
case "test_internals": return test_internals;
3739
}
3840

0 commit comments

Comments
 (0)