-
Notifications
You must be signed in to change notification settings - Fork 2
/
matplotlib.h
166 lines (148 loc) · 4.31 KB
/
matplotlib.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#ifndef MATPLOTLIB_H
#define MATPLOTLIB_H
#include <node.h>
#include <string>
#include <Python.h>
#ifdef linux
#include <dlfcn.h>
#endif
#if PY_MAJOR_VERSION >= 3
#define PyString_FromString PyUnicode_FromString
#endif
namespace plt {
struct interpreter {
public:
PyObject *plot;
PyObject *subplot;
PyObject *show;
PyObject *legend;
PyObject *grid;
PyObject *save;
PyObject *xlim;
PyObject *ylim;
PyObject *title;
PyObject *axis;
PyObject *xlabel;
PyObject *ylabel;
PyObject *clf;
PyObject *cla;
PyObject *close;
PyObject *xkcd;
PyObject *empty_tuple;
static interpreter& get() {
static interpreter context;
return context;
}
private:
interpreter() {
#if PY_MAJOR_VERSION >= 3
wchar_t name[] = L"matplotnode";
#else
char name[] = "matplotnode";
#endif
Py_SetProgramName(name);
#ifdef linux
#if PY_MAJOR_VERSION >= 3
dlopen("libpython3.so", RTLD_LAZY | RTLD_GLOBAL);
#else
dlopen("libpython2.7.so", RTLD_LAZY | RTLD_GLOBAL);
#endif
#endif
Py_Initialize();
PyObject *matplotlibname = PyString_FromString("matplotlib");
PyObject* matplotlib = PyImport_Import(matplotlibname);
Py_DECREF(matplotlibname);
if (!matplotlib) {
PyErr_Print();
fprintf(stderr, "Could not import matplotlib.pyplot.\n");
return;
}
PyObject_CallMethod(matplotlib, const_cast<char*>("use"), const_cast<char*>("s"), "TkAgg");
PyObject *pyplotname = PyString_FromString("matplotlib.pyplot");
PyObject *pyplot = PyImport_Import(pyplotname);
Py_DECREF(pyplotname);
if (!pyplot) {
PyErr_Print();
fprintf(stderr, "Could not import matplotlib.pyplot.\n");
return;
}
plot = PyObject_GetAttrString(pyplot, "plot");
subplot = PyObject_GetAttrString(pyplot, "subplot");
show = PyObject_GetAttrString(pyplot, "show");
legend = PyObject_GetAttrString(pyplot, "legend");
grid = PyObject_GetAttrString(pyplot, "grid");
save = PyObject_GetAttrString(pyplot, "savefig");
xlim = PyObject_GetAttrString(pyplot, "xlim");
ylim = PyObject_GetAttrString(pyplot, "ylim");
title = PyObject_GetAttrString(pyplot, "title");
axis = PyObject_GetAttrString(pyplot, "axis");
xlabel = PyObject_GetAttrString(pyplot, "xlabel");
ylabel = PyObject_GetAttrString(pyplot, "ylabel");
clf = PyObject_GetAttrString(pyplot, "clf");
cla = PyObject_GetAttrString(pyplot, "cla");
close = PyObject_GetAttrString(pyplot, "close");
xkcd = PyObject_GetAttrString(pyplot, "xkcd");
if (!plot
|| !subplot
|| !show
|| !legend
|| !grid
|| !save
|| !xlim
|| !ylim
|| !title
|| !axis
|| !xlabel
|| !ylabel
|| !clf
|| !cla
|| !close
|| !xkcd) {
PyErr_Print();
fprintf(stderr, "Error loading matplotlib functions.\n");
return;
}
if (!PyCallable_Check(plot)
|| !PyCallable_Check(subplot)
|| !PyCallable_Check(show)
|| !PyCallable_Check(legend)
|| !PyCallable_Check(grid)
|| !PyCallable_Check(save)
|| !PyCallable_Check(xlim)
|| !PyCallable_Check(ylim)
|| !PyCallable_Check(title)
|| !PyCallable_Check(axis)
|| !PyCallable_Check(xlabel)
|| !PyCallable_Check(ylabel)
|| !PyCallable_Check(clf)
|| !PyCallable_Check(cla)
|| !PyCallable_Check(close)
|| !PyCallable_Check(xkcd)) {
PyErr_Print();
fprintf(stderr, "One or more of the matplotlib functions are not callable.\n");
return;
}
empty_tuple = PyTuple_New(0);
}
~interpreter() {
Py_Finalize();
}
};
void plot(v8::FunctionCallbackInfo<v8::Value>& info);
void subplot(v8::FunctionCallbackInfo<v8::Value>& info);
void show(v8::FunctionCallbackInfo<v8::Value>& info);
void legend(v8::FunctionCallbackInfo<v8::Value>& info);
void grid(v8::FunctionCallbackInfo<v8::Value>& info);
void save(v8::FunctionCallbackInfo<v8::Value>& info);
void xlim(v8::FunctionCallbackInfo<v8::Value>& info);
void ylim(v8::FunctionCallbackInfo<v8::Value>& info);
void title(v8::FunctionCallbackInfo<v8::Value>& info);
void axis(v8::FunctionCallbackInfo<v8::Value>& info);
void xlabel(v8::FunctionCallbackInfo<v8::Value>& info);
void ylabel(v8::FunctionCallbackInfo<v8::Value>& info);
void clf(v8::FunctionCallbackInfo<v8::Value>& info);
void cla(v8::FunctionCallbackInfo<v8::Value>& info);
void close(v8::FunctionCallbackInfo<v8::Value>& info);
void xkcd(v8::FunctionCallbackInfo<v8::Value>& info);
}
#endif