-
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.
adding GridSpec, Text, and align_labels_demo.cpp
- Loading branch information
Showing
12 changed files
with
153 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ NamespaceIndentation: None | |
Language: Cpp | ||
Standard: Auto | ||
ColumnLimit: 80 | ||
SortIncludes: false |
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
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,2 +1,56 @@ | ||
// example from | ||
// https://matplotlib.org/stable/gallery/subplots_axes_and_figures/align_labels_demo.html#sphx-glr-gallery-subplots-axes-and-figures-align-labels-demo-py | ||
|
||
#include <pybind11/embed.h> | ||
#include <pybind11/stl.h> | ||
|
||
#include <matplotlib_cpp11/matplotlib_cpp11.h> | ||
|
||
#include <algorithm> | ||
#include <iostream> | ||
#include <vector> | ||
|
||
namespace py = pybind11; | ||
using namespace py::literals; | ||
|
||
template <typename T> std::vector<T> arange(T start, T end, T h) { | ||
int N = static_cast<int>((end - start) / h); | ||
std::vector<T> xs(N); | ||
T val = start; | ||
for (int i = 0; i < N; ++i) { | ||
xs[i] = val; | ||
; | ||
val += h; | ||
} | ||
return xs; | ||
} | ||
|
||
using namespace std; | ||
namespace pyplot = matplotlib_cpp11; | ||
|
||
int main() { | ||
py::initialize_interpreter(); | ||
auto plt = pyplot::import(); | ||
auto fig = pyplot::Figure(plt.figure("tight_layout"_a = true)); | ||
auto gs = pyplot::GridSpec(2, 2); | ||
auto ax = pyplot::Axes(fig.add_subplot(gs(0, -1))); | ||
ax.plot(arange(0, 1000000, 10000)); | ||
ax.set_ylabel("YLabel0"); | ||
ax.set_xlabel("XLabel0"); | ||
for (auto i : {0, 1}) { | ||
ax = pyplot::Axes(fig.add_subplot(gs(1, i))); | ||
auto ys = arange(1.0, 0.0, -0.1); | ||
decltype(ys) xs; | ||
std::for_each(ys.begin(), ys.end(), | ||
[&](auto val) { xs.push_back(val * 2000); }); | ||
ax.plot(xs, ys); | ||
ax.set_ylabel(string("YLabel1 " + to_string(i))); | ||
ax.set_xlabel(string("XLabel1 " + to_string(i))); | ||
if (i == 0) { | ||
for (auto &&tick : ax.get_xticklabels()) | ||
tick.set_rotation(55); | ||
} | ||
} | ||
fig.align_labels(); | ||
plt.show(); | ||
} |
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,20 +1,31 @@ | ||
#include <vector> | ||
|
||
struct __attribute__((visibility("hidden"))) Axes { | ||
Axes() {} | ||
Axes(pybind11::object axes) { | ||
self = axes; | ||
load_attrs(); | ||
} | ||
Axes(pybind11::module mod) { | ||
self = mod.attr("axes"); | ||
load_attrs(); | ||
} | ||
void load_attrs() { | ||
LOAD_ATTR(plot, self); | ||
LOAD_ATTR(set, self); | ||
LOAD_ATTR(grid, self); | ||
LOAD_ATTR(set_xlabel, self); | ||
LOAD_ATTR(set_ylabel, self); | ||
get_xticklabels_attr = self.attr("get_xticklabels"); | ||
} | ||
pybind11::object self; | ||
pybind11::object plot; | ||
pybind11::object set; | ||
pybind11::object grid; | ||
pybind11::object set_xlabel; | ||
pybind11::object set_ylabel; | ||
pybind11::object get_xticklabels_attr; | ||
std::vector<Text> get_xticklabels() { | ||
pybind11::list ret = get_xticklabels_attr(); | ||
std::vector<Text> texts; | ||
for (pybind11::size_t i = 0; i < ret.size(); ++i) { | ||
texts.push_back(Text(ret[i])); | ||
} | ||
return texts; | ||
} | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#define LOAD_ATTR(attr_, mod_) \ | ||
do { \ | ||
attr_ = mod_.attr(#attr_); \ | ||
} while (0) |
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,15 +1,15 @@ | ||
struct __attribute__((visibility("hidden"))) Figure { | ||
Figure() {} | ||
Figure(pybind11::object figure) { | ||
self = figure; | ||
load_attrs(); | ||
} | ||
Figure(pybind11::module mod) { | ||
self = mod.attr("figure"); | ||
load_attrs(); | ||
void load_attrs() { | ||
LOAD_ATTR(savefig, self); | ||
LOAD_ATTR(add_subplot, self); | ||
LOAD_ATTR(align_labels, self); | ||
} | ||
void load_attrs() { LOAD_ATTR(savefig, self); } | ||
pybind11::object self; | ||
pybind11::object savefig; | ||
pybind11::object add_subplot; | ||
pybind11::object align_labels; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
struct __attribute__((visibility("hidden"))) GridSpec { | ||
GridSpec(int nrow_, int ncol_) { | ||
nrow = nrow_; | ||
ncol = ncol_; | ||
load_attrs(); | ||
} | ||
void load_attrs() { | ||
gridspec_attr = | ||
pybind11::module::import("matplotlib.gridspec").attr("GridSpec"); | ||
self = gridspec_attr(nrow, ncol); | ||
} | ||
pybind11::object operator()(int r, int c) { | ||
// if r == -1 or c == -1, do slice | ||
if (r == -1) { | ||
if (c == -1) { | ||
return self; | ||
} | ||
auto rs = pybind11::slice(0, nrow, 1); | ||
return self[pybind11::make_tuple(rs, c)]; | ||
} else if (c == -1) { | ||
auto cs = pybind11::slice(0, ncol, 1); | ||
return self[pybind11::make_tuple(r, cs)]; | ||
} else | ||
return self[pybind11::make_tuple(r, c)]; | ||
} | ||
int nrow, ncol; | ||
pybind11::object self; | ||
pybind11::object gridspec_attr; | ||
}; |
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
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
struct __attribute__((visibility("hidden"))) Text { | ||
Text(pybind11::object text) { | ||
self = text; | ||
load_attrs(); | ||
} | ||
void load_attrs() { LOAD_ATTR(set_rotation, self); } | ||
pybind11::object self; | ||
pybind11::object set_rotation; | ||
}; |