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

added subplots_axes_and_figures/two_scales

parent c2dd7040
No related branches found
No related tags found
No related merge requests found
...@@ -70,13 +70,13 @@ if(${ADD_DEMO}) ...@@ -70,13 +70,13 @@ if(${ADD_DEMO})
set(CMAKE_CXX_FLAGS set(CMAKE_CXX_FLAGS
"-Wall -g -DUSE_GUI=${USE_GUI} -DMATPLOTLIB_MINOR_VER_GTE_4=${MATPLOTLIB_MINOR_VER_GTE_4}" "-Wall -g -DUSE_GUI=${USE_GUI} -DMATPLOTLIB_MINOR_VER_GTE_4=${MATPLOTLIB_MINOR_VER_GTE_4}"
) )
add_subdirectory(gallery/lines_bars_and_markers) # 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/statistics)
add_subdirectory(gallery/images_contours_and_fields) # add_subdirectory(gallery/images_contours_and_fields)
add_subdirectory(gallery/shapes_and_collections) # add_subdirectory(gallery/shapes_and_collections)
add_subdirectory(gallery/artist_animation) # add_subdirectory(gallery/artist_animation)
add_subdirectory(gallery/mplot3d) # add_subdirectory(gallery/mplot3d)
endif() endif()
# test # test
......
...@@ -3,13 +3,15 @@ add_demo(gridspec_multicolumn gridspec_multicolumn.cpp) ...@@ -3,13 +3,15 @@ add_demo(gridspec_multicolumn gridspec_multicolumn.cpp)
add_demo(multiple_figs_demo multiple_figs_demo.cpp) add_demo(multiple_figs_demo multiple_figs_demo.cpp)
add_demo(colorbar_placement colorbar_placement.cpp) add_demo(colorbar_placement colorbar_placement.cpp)
add_demo(subplots subplots.cpp) add_demo(subplots subplots.cpp)
add_demo(two_scales two_scales.cpp)
add_custom_target(subplots_axes_and_figures add_custom_target(subplots_axes_and_figures
DEPENDS align_labels_demo gridspec_multicolumn multiple_figs_demo colorbar_placement DEPENDS align_labels_demo gridspec_multicolumn multiple_figs_demo colorbar_placement two_scales
COMMAND align_labels_demo COMMAND align_labels_demo
COMMAND gridspec_multicolumn COMMAND gridspec_multicolumn
COMMAND multiple_figs_demo COMMAND multiple_figs_demo
COMMAND colorbar_placement COMMAND colorbar_placement
COMMAND two_scales
COMMENT "subplots_axes_and_figures" COMMENT "subplots_axes_and_figures"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
) )
// example from
// https://matplotlib.org/stable/gallery/subplots_axes_and_figures/two_scales.html
#include <matplotlibcpp17/pyplot.h>
#include <xtensor/xbuilder.hpp>
#include <xtensor/xmath.hpp>
#include <vector>
using namespace std;
using namespace matplotlibcpp17;
int main() {
auto t_ = xt::arange(0.01, 10.0, 0.01);
auto data1_ = xt::exp(t_);
auto data2_ = xt::sin(2 * M_PI * t_);
vector<double> t(t_.begin(), t_.end()), data1(data1_.begin(), data1_.end()),
data2(data2_.begin(), data2_.end());
py::scoped_interpreter guard{};
auto plt = pyplot::import();
auto [fig, ax1] = plt.subplots();
auto color = "tab:red";
ax1.set_xlabel(Args("time (s)"));
ax1.set_ylabel(Args("exp"), Kwargs("color"_a = color));
ax1.plot(Args(t, data1), Kwargs("color"_a = color));
ax1.tick_params(Args(), Kwargs("axis"_a = "y", "labelcolor"_a = color));
auto ax2 =
ax1.twinx(); // instantiate a second axes that shares the same x-axis
color = "tab:blue";
ax2.set_ylabel(Args("sin"), Kwargs("color"_a = color));
ax2.plot(Args(t, data2), Kwargs("color"_a = color));
ax2.tick_params(Args(), Kwargs("axis"_a = "y", "labelcolor"_a = color));
fig.tight_layout();
plt.show();
}
...@@ -212,6 +212,10 @@ public: ...@@ -212,6 +212,10 @@ public:
pybind11::object tick_params(const pybind11::tuple &args = pybind11::tuple(), pybind11::object tick_params(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict()); const pybind11::dict &kwargs = pybind11::dict());
// twinx
Axes twinx(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
private: private:
void load_attrs() { void load_attrs() {
LOAD_FUNC_ATTR(add_artist, self); LOAD_FUNC_ATTR(add_artist, self);
...@@ -267,6 +271,7 @@ private: ...@@ -267,6 +271,7 @@ private:
LOAD_FUNC_ATTR(set_yticks, self); LOAD_FUNC_ATTR(set_yticks, self);
LOAD_FUNC_ATTR(text, self); LOAD_FUNC_ATTR(text, self);
LOAD_FUNC_ATTR(tick_params, self); LOAD_FUNC_ATTR(tick_params, self);
LOAD_FUNC_ATTR(twinx, self);
} }
pybind11::object add_artist_attr; pybind11::object add_artist_attr;
pybind11::object add_collection_attr; pybind11::object add_collection_attr;
...@@ -310,6 +315,7 @@ private: ...@@ -310,6 +315,7 @@ private:
pybind11::object set_zlim_attr; pybind11::object set_zlim_attr;
pybind11::object text_attr; pybind11::object text_attr;
pybind11::object tick_params_attr; pybind11::object tick_params_attr;
pybind11::object twinx_attr;
bool projection_3d; bool projection_3d;
}; };
...@@ -638,6 +644,12 @@ pybind11::object Axes::tick_params(const pybind11::tuple &args, ...@@ -638,6 +644,12 @@ pybind11::object Axes::tick_params(const pybind11::tuple &args,
return ret; return ret;
} }
// twinx
Axes Axes::twinx(const pybind11::tuple &args, const pybind11::dict &kwargs) {
pybind11::object ret = twinx_attr(*args, **kwargs);
return Axes(ret);
}
} // namespace matplotlibcpp17::axes } // namespace matplotlibcpp17::axes
#endif /* MATPLOTLIBCPP17_AXES_H */ #endif /* MATPLOTLIBCPP17_AXES_H */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment