-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
56 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
struct __attribute__((visibility("hidden"))) figure { | ||
// TODO: For "hidden" class does ctor/dtor works ? | ||
void init(pybind11::object instance_) { | ||
instance = instance_; | ||
savefig = instance.attr("savefig"); | ||
struct __attribute__((visibility("hidden"))) Figure { | ||
Figure() {} | ||
Figure(pybind11::object figure) { | ||
self = figure; | ||
load_attrs(); | ||
} | ||
pybind11::object instance; | ||
Figure(pybind11::module mod) { | ||
self = mod.attr("figure"); | ||
load_attrs(); | ||
} | ||
void load_attrs() { LOAD_ATTR(savefig, self); } | ||
pybind11::object self; | ||
pybind11::object savefig; | ||
pybind11::object add_subplot; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,34 @@ | ||
#ifndef MATPLOTLIBCPP_11 | ||
#define MATPLOTLIBCPP_11 | ||
#ifndef MATPLOTLIBCPP_11_H | ||
#define MATPLOTLIBCPP_11_H | ||
|
||
#include <pybind11/pybind11.h> | ||
|
||
namespace matplotlib_cpp11 { | ||
|
||
static bool g_imported = false; | ||
|
||
#define LOAD_ATTR(attr_, mod_) \ | ||
do { \ | ||
attr_ = mod_.attr(#attr_); \ | ||
} while (0) | ||
|
||
#include "axes.h" | ||
#include "figure.h" | ||
#include "pyplot.h" | ||
|
||
static pyplot &import() { | ||
static pyplot g_pyplot; | ||
static figure g_figure; | ||
static axes g_axes; | ||
static Figure g_figure; | ||
static Axes g_axes; | ||
if (not g_imported) { | ||
g_imported = true; | ||
|
||
// pyplot singleton | ||
g_pyplot.mod = pybind11::module::import("matplotlib.pyplot"); | ||
g_pyplot.plot = g_pyplot.mod.attr("plot"); | ||
g_pyplot.show = g_pyplot.mod.attr("show"); | ||
g_pyplot.subplot_attr = g_pyplot.mod.attr("subplot"); | ||
g_pyplot.subplots_attr = g_pyplot.mod.attr("subplots"); | ||
|
||
// figure singleton | ||
// g_figure.mod = pybind11::module::import("matplotlib.figure"); | ||
|
||
// axes singleton | ||
auto mod = pybind11::module::import("matplotlib.pyplot"); | ||
g_pyplot = pyplot(mod); | ||
} | ||
return g_pyplot; | ||
} | ||
|
||
} // namespace matplotlib_cpp11 | ||
|
||
#endif /* MATPLOTLIBCPP_11 */ | ||
#endif /* MATPLOTLIBCPP_11_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,27 @@ | ||
struct __attribute__((visibility("hidden"))) pyplot { | ||
pyplot() {} | ||
pyplot(pybind11::module mod_) { | ||
mod = mod_; | ||
load_attrs(); | ||
} | ||
void load_attrs() { | ||
LOAD_ATTR(plot, mod); | ||
LOAD_ATTR(show, mod); | ||
subplot_attr = mod.attr("subplot"); | ||
subplots_attr = mod.attr("subplots"); | ||
} | ||
pybind11::module mod; | ||
pybind11::object plot; | ||
pybind11::object show; | ||
pybind11::object subplot_attr; | ||
pybind11::object subplots_attr; | ||
axes subplot() { | ||
auto ret = subplot_attr(); | ||
axes ax; | ||
ax.init(ret); | ||
return ax; | ||
} | ||
std::tuple<figure, axes> subplots() { | ||
Axes subplot() { return Axes(subplot_attr()); } | ||
std::tuple<Figure, Axes> subplots() { | ||
// TODO: a lot of versions like subplots(111) or subplots(py::tuple(1,1,1)) | ||
pybind11::list ret = subplots_attr(); | ||
figure fig; | ||
fig.init(ret[0]); | ||
axes ax; | ||
ax.init(ret[1]); | ||
return {fig, ax}; | ||
pybind11::object fig = ret[0], ax = ret[1]; | ||
return {Figure(fig), Axes(ax)}; | ||
} | ||
Figure figure() { return Figure(mod); } | ||
Axes axes() { return Axes(mod); } | ||
}; |