Skip to content

Commit f5db6b2

Browse files
committed
Merge branch 'develop'
2 parents d261c7d + 1f7f292 commit f5db6b2

File tree

6 files changed

+158
-63
lines changed

6 files changed

+158
-63
lines changed

CMakeLists.txt

Lines changed: 59 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,62 +2,74 @@ cmake_minimum_required(VERSION 3.12)
22

33
project(
44
"matplotlibcpp17"
5-
VERSION 0.0.0
6-
DESCRIPTION "A C++ header-only plotting library based on pybind11 and matplotlib, featuring more flexibility than matplotlibcpp"
7-
HOMEPAGE_URL "https://soblin.github.io/matplotlibcpp17/"
8-
)
5+
VERSION 1.0.0
6+
DESCRIPTION
7+
"A C++ header-only plotting library based on pybind11 and matplotlib, featuring more flexibility than matplotlibcpp"
8+
HOMEPAGE_URL "https://soblin.github.io/matplotlibcpp17/")
99

10-
find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
10+
find_package(
11+
Python3
12+
COMPONENTS Interpreter Development
13+
REQUIRED)
1114
find_package(pybind11 2.4.3 REQUIRED)
1215

13-
1416
# check matplotlib minor version
1517
execute_process(
16-
COMMAND
17-
${Python3_EXECUTABLE} "-c"
18-
"import matplotlib;
19-
print(str(matplotlib.__version__))"
18+
COMMAND ${Python3_EXECUTABLE} "-c" "import matplotlib;
19+
print(str(matplotlib.__version__), end='')"
2020
RESULT_VARIABLE MATPLOTLIB_VERSION_CHECKING
21-
OUTPUT_VARIABLE MATPLOTLIB_VERSION
22-
)
21+
OUTPUT_VARIABLE MATPLOTLIB_VERSION)
2322

2423
if(NOT MATPLOTLIB_VERSION_CHECKING MATCHES 0)
25-
message(FATAL_ERROR
26-
"Could not check matplotlib.__version__")
24+
message(FATAL_ERROR "Could not check matplotlib.__version__")
2725
endif()
2826
message(STATUS "Detected matplotlib version is ${MATPLOTLIB_VERSION}")
2927
if(${MATPLOTLIB_VERSION} VERSION_LESS 3.4)
3028
message(WARNING "Detected matplotlib version is < 3.4.0")
3129
set(MATPLOTLIB_MINOR_VER_GTE_4 0)
3230
else()
31+
message(STATUS "Detected matplotlib version is >= 3.4.0")
3332
set(MATPLOTLIB_MINOR_VER_GTE_4 1)
3433
endif()
3534

36-
3735
# gallery
3836
if(NOT DEFINED USE_GUI)
3937
set(USE_GUI 1)
38+
message(STATUS "USE_GUI = ON")
4039
endif()
4140
if(NOT DEFINED ADD_DEMO)
4241
set(ADD_DEMO 1)
4342
endif()
43+
if(USE_GUI)
44+
message(STATUS "USE_GUI = ON")
45+
else()
46+
message(STATUS "USE_GUI = OFF")
47+
endif()
48+
if(ADD_DEMO)
49+
message(STATUS "ADD_DEMO = ON")
50+
else()
51+
message(STATUS "ADD_DEMO = OFF")
52+
endif()
4453

4554
set(matplotlibcpp17_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/include")
4655

4756
function(add_demo name path)
4857
add_executable(${name} ${path})
49-
target_include_directories(${name} PUBLIC
50-
${Python3_INCLUDE_DIRS}
51-
${matplotlibcpp17_INCLUDE_DIRS}
52-
)
58+
target_include_directories(${name} PUBLIC ${Python3_INCLUDE_DIRS}
59+
${matplotlibcpp17_INCLUDE_DIRS})
5360
target_link_libraries(${name} ${Python3_LIBRARIES} pybind11::embed xtensor)
5461
endfunction()
5562

5663
if(${ADD_DEMO})
57-
find_package(Python3 COMPONENTS NumPy REQUIRED)
64+
find_package(
65+
Python3
66+
COMPONENTS NumPy
67+
REQUIRED)
5868
find_package(xtensor 0.24.0 REQUIRED)
5969
set(CMAKE_CXX_STANDARD 17)
60-
set(CMAKE_CXX_FLAGS "-Wall -g -DUSE_GUI=${USE_GUI} -DMATPLOTLIB_MINOR_VER_GTE_4=${MATPLOTLIB_MINOR_VER_GTE_4}")
70+
set(CMAKE_CXX_FLAGS
71+
"-Wall -g -DUSE_GUI=${USE_GUI} -DMATPLOTLIB_MINOR_VER_GTE_4=${MATPLOTLIB_MINOR_VER_GTE_4}"
72+
)
6173
add_subdirectory(gallery/lines_bars_and_markers)
6274
add_subdirectory(gallery/subplots_axes_and_figures)
6375
add_subdirectory(gallery/statistics)
@@ -67,7 +79,6 @@ if(${ADD_DEMO})
6779
add_subdirectory(gallery/mplot3d)
6880
endif()
6981

70-
7182
# test
7283
enable_testing()
7384
if(NOT DEFINED DO_TEST)
@@ -77,63 +88,51 @@ if(${DO_TEST})
7788
add_subdirectory(tests)
7889
endif()
7990

80-
81-
# install
82-
## https://dominikberner.ch/cmake-interface-lib/
91+
# install https://dominikberner.ch/cmake-interface-lib/
8392
include(GNUInstallDirs)
8493
add_library(${PROJECT_NAME} INTERFACE)
85-
target_include_directories(${PROJECT_NAME}
86-
INTERFACE
87-
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
88-
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
89-
)
94+
target_include_directories(
95+
${PROJECT_NAME}
96+
INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
97+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
9098
target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_17)
91-
install(TARGETS ${PROJECT_NAME}
99+
install(
100+
TARGETS ${PROJECT_NAME}
92101
EXPORT ${PROJECT_NAME}_Targets
93102
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
94-
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
95-
)
103+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
96104
include(CMakePackageConfigHelpers)
97-
write_basic_package_version_file("${PROJECT_NAME}ConfigVersion.cmake"
105+
write_basic_package_version_file(
106+
"${PROJECT_NAME}ConfigVersion.cmake"
98107
VERSION ${PROJECT_VERSION}
99-
COMPATIBILITY SameMajorVersion
100-
)
108+
COMPATIBILITY SameMajorVersion)
101109
configure_package_config_file(
102110
"${PROJECT_SOURCE_DIR}/cmake/${PROJECT_NAME}Config.cmake.in"
103111
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
104-
INSTALL_DESTINATION
105-
${CMAKE_INSTALL_DATAROOTDIR}/cmake/${PROJECT_NAME}
106-
)
112+
INSTALL_DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/cmake/${PROJECT_NAME})
107113
# to /share/cmake/matplotlibcpp17
108-
install(FILES
109-
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
110-
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
111-
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/cmake/${PROJECT_NAME}
112-
)
114+
install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
115+
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
116+
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/cmake/${PROJECT_NAME})
113117
# to /share/cmake/matplotlibcpp17
114-
install(EXPORT ${PROJECT_NAME}_Targets
118+
install(
119+
EXPORT ${PROJECT_NAME}_Targets
115120
FILE ${PROJECT_NAME}Targets.cmake
116121
NAMESPACE ${PROJECT_NAME}::
117-
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/cmake/${PROJECT_NAME}
118-
)
122+
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/cmake/${PROJECT_NAME})
119123
# to /include/matplotlibcpp17
120124
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/${PROJECT_NAME}
121-
DESTINATION include
122-
)
125+
DESTINATION include)
123126
set(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/LICENSE")
124127
# create .deb
125128
include("${PROJECT_SOURCE_DIR}/cmake/package.cmake")
126129

127-
# uninstall target
128-
## actually it's just `xargs rm < install_manifest.txt`
129-
## https://gitlab.kitware.com/cmake/community/-/wikis/FAQ#can-i-do-make-uninstall-with-cmake
130+
# uninstall target. actually it's just `xargs rm < install_manifest.txt`
131+
# https://gitlab.kitware.com/cmake/community/-/wikis/FAQ#can-i-do-make-uninstall-with-cmake
130132
if(NOT TARGET uninstall)
131-
configure_file(
132-
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/uninstall.cmake.in"
133-
"${CMAKE_CURRENT_BINARY_DIR}/uninstall.cmake"
134-
IMMEDIATE @ONLY
135-
)
136-
add_custom_target(uninstall
137-
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/uninstall.cmake
138-
)
133+
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/uninstall.cmake.in"
134+
"${CMAKE_CURRENT_BINARY_DIR}/uninstall.cmake" IMMEDIATE @ONLY)
135+
add_custom_target(
136+
uninstall COMMAND ${CMAKE_COMMAND} -P
137+
${CMAKE_CURRENT_BINARY_DIR}/uninstall.cmake)
139138
endif()

gallery/mplot3d/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ add_demo(lorenz_attractor lorenz_attractor.cpp)
33
add_demo(contour3d contour3d.cpp)
44
add_demo(subplot3d subplot3d.cpp)
55
add_demo(errorbar3d errorbar3d.cpp)
6+
add_demo(surface3d surface3d)
67

78
add_custom_target(mplot3d
8-
DEPENDS lines3d lorenz_attractor contour3d subplot3d errorbar3d
9+
DEPENDS lines3d lorenz_attractor contour3d subplot3d errorbar3d surface3d
910
COMMAND lines3d
1011
COMMAND lorenz_attractor
1112
COMMAND contour3d
1213
COMMAND subplot3d
1314
COMMAND errorbar3d
15+
COMMAND surface3d
1416
COMMENT "running mplot3d"
1517
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../images"
1618
)

gallery/mplot3d/subplot3d.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <pybind11/numpy.h>
66

77
#include <matplotlibcpp17/pyplot.h>
8+
#include <matplotlibcpp17/cm.h>
89

910
#include <xtensor/xbuilder.hpp>
1011
#include <xtensor/xmath.hpp>
@@ -79,8 +80,11 @@ int main() {
7980
auto Y_ = py::array(py::cast(std::move(Y)));
8081
auto Z_ = py::array(py::cast(std::move(Z)));
8182
auto surf = ax.plot_surface(
82-
Args(X_, Y_, Z_), Kwargs("rstride"_a = 1, "cstride"_a = 1,
83-
"linewidth"_a = 0, "antialiased"_a = false));
83+
Args(X_, Y_, Z_),
84+
Kwargs("rstride"_a = 1, "cstride"_a = 1, "linewidth"_a = 0,
85+
"antialiased"_a = false, "cmap"_a = cm::coolwarm()));
86+
ax.set_zlim(Args(-1.01, 1.01));
87+
fig.colorbar(Args(surf), Kwargs("shrink"_a = 0.5, "aspect"_a = 10));
8488
}
8589
{
8690
auto ax = fig.add_subplot(Args(1, 2, 2), Kwargs("projection"_a = "3d"));

gallery/mplot3d/surface3d.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// example from https://matplotlib.org/stable/gallery/mplot3d/surface3d.html
2+
3+
#include <pybind11/embed.h>
4+
#include <pybind11/stl.h>
5+
#include <pybind11/numpy.h>
6+
7+
#include <matplotlibcpp17/pyplot.h>
8+
#include <matplotlibcpp17/cm.h>
9+
10+
#include <xtensor/xbuilder.hpp>
11+
#include <xtensor/xmath.hpp>
12+
13+
#include <vector>
14+
15+
namespace py = pybind11;
16+
using namespace py::literals;
17+
using namespace std;
18+
using namespace matplotlibcpp17;
19+
20+
using mesh2D = vector<vector<double>>;
21+
22+
int main() {
23+
py::scoped_interpreter guard{};
24+
auto plt = matplotlibcpp17::pyplot::import();
25+
auto [fig, ax] =
26+
plt.subplots(Kwargs("subplot_kw"_a = py::dict("projection"_a = "3d")));
27+
28+
auto xs = xt::arange(-5.0, 5.0, 0.25);
29+
auto [X0, Y0] = xt::meshgrid(xs, xs);
30+
auto R0 = xt::sqrt(xt::pow(X0, 2) + xt::pow(Y0, 2));
31+
auto Z0 = xt::sin(R0);
32+
// to vector<vector>
33+
const int sz = xs.shape()[0];
34+
mesh2D X(sz), Y(sz), Z(sz);
35+
for (int i = 0; i < sz; ++i) {
36+
X[i].resize(sz);
37+
Y[i].resize(sz);
38+
Z[i].resize(sz);
39+
for (int j = 0; j < sz; ++j) {
40+
X[i][j] = X0(i, j);
41+
Y[i][j] = Y0(i, j);
42+
Z[i][j] = Z0(i, j);
43+
}
44+
}
45+
// to numpy array (vector<vector> is converted to list of list)
46+
auto X_ = py::array(py::cast(std::move(X)));
47+
auto Y_ = py::array(py::cast(std::move(Y)));
48+
auto Z_ = py::array(py::cast(std::move(Z)));
49+
auto surf = ax.plot_surface(Args(X_, Y_, Z_),
50+
Kwargs("rstride"_a = 1, "cstride"_a = 1,
51+
"linewidth"_a = 0, "antialiased"_a = false,
52+
"cmap"_a = cm::coolwarm()));
53+
ax.set_zlim(Args(-1.01, 1.01));
54+
fig.colorbar(Args(surf), Kwargs("shrink"_a = 0.5, "aspect"_a = 5));
55+
plt.show();
56+
}

include/matplotlibcpp17/axes.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ struct DECL_STRUCT_ATTR Axes : public BaseWrapper {
200200
pybind11::object set_zlabel(const pybind11::tuple &args = pybind11::tuple(),
201201
const pybind11::dict &kwargs = pybind11::dict());
202202

203+
// set_zlim
204+
pybind11::object set_zlim(const pybind11::tuple &args = pybind11::tuple(),
205+
const pybind11::dict &kwargs = pybind11::dict());
206+
203207
// text
204208
pybind11::object text(const pybind11::tuple &args = pybind11::tuple(),
205209
const pybind11::dict &kwargs = pybind11::dict());
@@ -260,6 +264,7 @@ struct DECL_STRUCT_ATTR Axes : public BaseWrapper {
260264
LOAD_FUNC_ATTR(set_ylabel, self);
261265
LOAD_FUNC_ATTR(set_ylim, self);
262266
LOAD_FUNC_ATTR(set_yticks, self);
267+
LOAD_FUNC_ATTR(set_zlim, self);
263268
LOAD_FUNC_ATTR(text, self);
264269
LOAD_FUNC_ATTR(tick_params, self);
265270
}
@@ -302,6 +307,7 @@ struct DECL_STRUCT_ATTR Axes : public BaseWrapper {
302307
pybind11::object set_ylim_attr;
303308
pybind11::object set_yticks_attr;
304309
pybind11::object set_zlabel_attr;
310+
pybind11::object set_zlim_attr;
305311
pybind11::object text_attr;
306312
pybind11::object tick_params_attr;
307313
bool projection_3d;
@@ -611,6 +617,13 @@ pybind11::object Axes::set_zlabel(const pybind11::tuple &args,
611617
return ret;
612618
}
613619

620+
// set_zlim
621+
pybind11::object Axes::set_zlim(const pybind11::tuple &args,
622+
const pybind11::dict &kwargs) {
623+
pybind11::object ret = set_zlim_attr(*args, **kwargs);
624+
return ret;
625+
}
626+
614627
// text
615628
pybind11::object Axes::text(const pybind11::tuple &args,
616629
const pybind11::dict &kwargs) {

include/matplotlibcpp17/cm.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @file cm.h
3+
* @brief corresponding header for matplotlib.cm
4+
**/
5+
6+
#ifndef MATPLOTLIBCPP17_CM_H
7+
#define MATPLOTLIBCPP17_CM_H
8+
9+
#include <pybind11/pybind11.h>
10+
11+
namespace matplotlibcpp17::cm {
12+
13+
pybind11::object coolwarm() {
14+
pybind11::object ret =
15+
pybind11::module::import("matplotlib.cm").attr("coolwarm");
16+
return ret;
17+
}
18+
19+
} // namespace matplotlibcpp17::cm
20+
21+
#endif /* MATPLOTLIBCPP17_CM_H */

0 commit comments

Comments
 (0)