Skip to content
Snippets Groups Projects
Commit b1536f73 authored by soblin's avatar soblin
Browse files

migrated .cpp in subplots_axes_and_figures

parent cac40010
No related branches found
No related tags found
No related merge requests found
......@@ -25,7 +25,7 @@ if(${ADD_DEMO})
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "-Wall -g -DUSE_GUI=${USE_GUI}")
add_subdirectory(gallery/lines_bars_and_markers)
# add_subdirectory(gallery/subplots_axes_and_figures)
add_subdirectory(gallery/subplots_axes_and_figures)
# add_subdirectory(gallery/statistics)
add_subdirectory(gallery/images_contours_and_fields)
add_subdirectory(gallery/shapes_and_collections)
......
......
......@@ -32,31 +32,31 @@ using namespace matplotlibcpp17;
int main() {
py::scoped_interpreter guard{};
auto plt = matplotlibcpp17::pyplot::import();
auto fig = plt.figure(kwargs_("tight_layout"_a = true));
auto fig = plt.figure(args_(), kwargs_("tight_layout"_a = true));
auto gs = GridSpec(2, 2);
// instead of gs[0, :]
auto ax = fig.add_subplot(args_(gs(0, py::slice(0, 2, 1)).unwrap()));
ax.plot(arange(0, 1000000, 10000));
ax.set_ylabel("YLabel0");
ax.set_xlabel("XLabel0");
ax.plot(args_(arange(0, 1000000, 10000)));
ax.set_ylabel(args_("YLabel0"));
ax.set_xlabel(args_("XLabel0"));
for (auto i : {0, 1}) {
ax = fig.add_subplot(args_(gs(1, i).unwrap()));
auto ys = arange(1.0, 0.0, -0.1);
decltype(ys) xs;
transform(ys.begin(), ys.end(), back_inserter(xs),
[](double x) { return x * 2000; });
ax.plot(xs, ys);
ax.set_ylabel(string("YLabel1 " + to_string(i)));
ax.set_xlabel(string("XLabel1 " + to_string(i)));
ax.plot(args_(xs, ys));
ax.set_ylabel(args_(string("YLabel1 " + to_string(i))));
ax.set_xlabel(args_(string("XLabel1 " + to_string(i))));
if (i == 0) {
for (auto &&tick : ax.get_xticklabels())
tick.set_rotation(55);
tick.set_rotation(args_(55));
}
}
fig.align_labels();
#if USE_GUI
plt.show();
#else
plt.savefig("align_labels_demo.png");
plt.savefig(args_("align_labels_demo.png"));
#endif
}
......@@ -20,28 +20,29 @@ int main() {
py::scoped_interpreter guard{};
auto plt = matplotlibcpp17::pyplot::import();
auto fig = plt.figure(kwargs_("constrained_layout"_a = true));
auto fig = plt.figure(args_(), kwargs_("constrained_layout"_a = true));
auto gs = GridSpec(3, 3, kwargs_("figure"_a = fig.unwrap()));
auto ax1 = fig.add_subplot(args_(gs(0, py::slice(0, 3, 1)).unwrap()));
auto ax2 = fig.add_subplot(args_(gs(1, py::slice(0, 2, 1)).unwrap()));
auto ax3 = fig.add_subplot(args_(gs(py::slice(1, 3, 1), 2).unwrap()));
auto ax4 = fig.add_subplot(args_(gs(2, 0).unwrap()));
auto ax5 = fig.add_subplot(args_(gs(2, 1).unwrap()));
fig.suptitle("GridSpec");
vector<axes::Axes> axs;
axs.push_back(fig.add_subplot(args_(gs(0, py::slice(0, 3, 1)).unwrap())));
axs.push_back(fig.add_subplot(args_(gs(1, py::slice(0, 2, 1)).unwrap())));
axs.push_back(fig.add_subplot(args_(gs(py::slice(1, 3, 1), 2).unwrap())));
axs.push_back(fig.add_subplot(args_(gs(2, 0).unwrap())));
axs.push_back(fig.add_subplot(args_(gs(2, 1).unwrap())));
fig.suptitle(args_("GridSpec"));
int cnt = 1;
for (const auto &ax : {ax1, ax2, ax3, ax4, ax5}) {
ax.text(0.5, 0.5, string("ax" + to_string(cnt)), "va"_a = "center",
"ha"_a = "center");
ax.tick_params("labelbottom"_a = false, "labelleft"_a = false);
for (auto &ax : axs) {
ax.text(args_(0.5, 0.5, string("ax" + to_string(cnt))),
kwargs_("va"_a = "center", "ha"_a = "center"));
ax.tick_params(args_(),
kwargs_("labelbottom"_a = false, "labelleft"_a = false));
cnt++;
}
#if USE_GUI
plt.show();
#else
plt.savefig("gridspec_multicolumn.png");
plt.savefig(args_("gridspec_multicolumn.png)");
#endif
}
......@@ -38,30 +38,30 @@ int main() {
[](double x) { return sin(4 * M_PI * x); });
plt.figure(args_(1));
plt.subplot(211);
plt.plot(t, s1);
plt.plot(args_(t, s1));
plt.subplot(212);
plt.plot(t, s2);
plt.plot(args_(t, s2));
#if USE_GUI
plt.show();
#else
plt.savefig("multiple_figs_demo1.png");
plt.savefig(args_("multiple_figs_demo1.png"));
#endif
plt.figure(args_(2));
plt.plot(t, s2);
plt.plot(args_(t, s2));
#if USE_GUI
plt.show();
#else
plt.savefig("multiple_figs_demo2.png");
plt.savefig(args_("multiple_figs_demo2.png"));
#endif
plt.figure(args_(2));
plt.subplot(211);
plt.plot(t, s2, "s");
plt.plot(args_(t, s2, "s"));
auto ax = plt.gca();
ax.set_xticks(args_());
ax.set_xticklabels(args_(py::list()));
#if USE_GUI
plt.show();
#else
plt.savefig("multiple_figs_demo3.png");
plt.savefig(args_("multiple_figs_demo3.png"));
#endif
return 0;
}
......@@ -157,6 +157,11 @@ public:
pybind11::object set_xticks(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// set_xticklabels
pybind11::object
set_xticklabels(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// set_ylabel
pybind11::object set_ylabel(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
......@@ -208,6 +213,7 @@ private:
LOAD_FUNC_ATTR(set_xlabel, self);
LOAD_FUNC_ATTR(set_xlim, self);
LOAD_FUNC_ATTR(set_xticks, self);
LOAD_FUNC_ATTR(set_xticklabels, self);
LOAD_FUNC_ATTR(set_ylabel, self);
LOAD_FUNC_ATTR(set_ylim, self);
LOAD_FUNC_ATTR(set_yticks, self);
......@@ -244,6 +250,7 @@ private:
pybind11::object set_xlabel_attr;
pybind11::object set_xlim_attr;
pybind11::object set_xticks_attr;
pybind11::object set_xticklabels_attr;
pybind11::object set_ylabel_attr;
pybind11::object set_ylim_attr;
pybind11::object set_yticks_attr;
......@@ -468,6 +475,13 @@ pybind11::object Axes::set_xticks(const pybind11::tuple &args,
return ret;
}
// set_xticklabels
pybind11::object Axes::set_xticklabels(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object ret = set_xticklabels_attr(*args, **kwargs);
return ret;
}
// set_ylabel
pybind11::object Axes::set_ylabel(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment