Skip to content

Commit 666d574

Browse files
authored
Merge pull request #17 from soblin/feature/colormap
Feature/colormap
2 parents 9b77093 + 99bcb0c commit 666d574

File tree

6 files changed

+144
-61
lines changed

6 files changed

+144
-61
lines changed

CMakeLists.txt

Lines changed: 45 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,25 @@ cmake_minimum_required(VERSION 3.12)
33
project(
44
"matplotlibcpp17"
55
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-
)
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;
18+
COMMAND ${Python3_EXECUTABLE} "-c" "import matplotlib;
1919
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)
@@ -34,7 +32,6 @@ else()
3432
set(MATPLOTLIB_MINOR_VER_GTE_4 1)
3533
endif()
3634

37-
3835
# gallery
3936
if(NOT DEFINED USE_GUI)
4037
set(USE_GUI 1)
@@ -58,18 +55,21 @@ set(matplotlibcpp17_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/include")
5855

5956
function(add_demo name path)
6057
add_executable(${name} ${path})
61-
target_include_directories(${name} PUBLIC
62-
${Python3_INCLUDE_DIRS}
63-
${matplotlibcpp17_INCLUDE_DIRS}
64-
)
58+
target_include_directories(${name} PUBLIC ${Python3_INCLUDE_DIRS}
59+
${matplotlibcpp17_INCLUDE_DIRS})
6560
target_link_libraries(${name} ${Python3_LIBRARIES} pybind11::embed xtensor)
6661
endfunction()
6762

6863
if(${ADD_DEMO})
69-
find_package(Python3 COMPONENTS NumPy REQUIRED)
64+
find_package(
65+
Python3
66+
COMPONENTS NumPy
67+
REQUIRED)
7068
find_package(xtensor 0.24.0 REQUIRED)
7169
set(CMAKE_CXX_STANDARD 17)
72-
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+
)
7373
add_subdirectory(gallery/lines_bars_and_markers)
7474
add_subdirectory(gallery/subplots_axes_and_figures)
7575
add_subdirectory(gallery/statistics)
@@ -79,7 +79,6 @@ if(${ADD_DEMO})
7979
add_subdirectory(gallery/mplot3d)
8080
endif()
8181

82-
8382
# test
8483
enable_testing()
8584
if(NOT DEFINED DO_TEST)
@@ -89,63 +88,51 @@ if(${DO_TEST})
8988
add_subdirectory(tests)
9089
endif()
9190

92-
93-
# install
94-
## https://dominikberner.ch/cmake-interface-lib/
91+
# install https://dominikberner.ch/cmake-interface-lib/
9592
include(GNUInstallDirs)
9693
add_library(${PROJECT_NAME} INTERFACE)
97-
target_include_directories(${PROJECT_NAME}
98-
INTERFACE
99-
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
100-
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
101-
)
94+
target_include_directories(
95+
${PROJECT_NAME}
96+
INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
97+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
10298
target_compile_features(${PROJECT_NAME} INTERFACE cxx_std_17)
103-
install(TARGETS ${PROJECT_NAME}
99+
install(
100+
TARGETS ${PROJECT_NAME}
104101
EXPORT ${PROJECT_NAME}_Targets
105102
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
106-
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
107-
)
103+
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
108104
include(CMakePackageConfigHelpers)
109-
write_basic_package_version_file("${PROJECT_NAME}ConfigVersion.cmake"
105+
write_basic_package_version_file(
106+
"${PROJECT_NAME}ConfigVersion.cmake"
110107
VERSION ${PROJECT_VERSION}
111-
COMPATIBILITY SameMajorVersion
112-
)
108+
COMPATIBILITY SameMajorVersion)
113109
configure_package_config_file(
114110
"${PROJECT_SOURCE_DIR}/cmake/${PROJECT_NAME}Config.cmake.in"
115111
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
116-
INSTALL_DESTINATION
117-
${CMAKE_INSTALL_DATAROOTDIR}/cmake/${PROJECT_NAME}
118-
)
112+
INSTALL_DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/cmake/${PROJECT_NAME})
119113
# to /share/cmake/matplotlibcpp17
120-
install(FILES
121-
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
122-
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
123-
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/cmake/${PROJECT_NAME}
124-
)
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})
125117
# to /share/cmake/matplotlibcpp17
126-
install(EXPORT ${PROJECT_NAME}_Targets
118+
install(
119+
EXPORT ${PROJECT_NAME}_Targets
127120
FILE ${PROJECT_NAME}Targets.cmake
128121
NAMESPACE ${PROJECT_NAME}::
129-
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/cmake/${PROJECT_NAME}
130-
)
122+
DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/cmake/${PROJECT_NAME})
131123
# to /include/matplotlibcpp17
132124
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/${PROJECT_NAME}
133-
DESTINATION include
134-
)
125+
DESTINATION include)
135126
set(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/LICENSE")
136127
# create .deb
137128
include("${PROJECT_SOURCE_DIR}/cmake/package.cmake")
138129

139-
# uninstall target
140-
## actually it's just `xargs rm < install_manifest.txt`
141-
## 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
142132
if(NOT TARGET uninstall)
143-
configure_file(
144-
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/uninstall.cmake.in"
145-
"${CMAKE_CURRENT_BINARY_DIR}/uninstall.cmake"
146-
IMMEDIATE @ONLY
147-
)
148-
add_custom_target(uninstall
149-
COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/uninstall.cmake
150-
)
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)
151138
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
@@ -195,6 +195,10 @@ struct DECL_STRUCT_ATTR Axes : public BaseWrapper {
195195
pybind11::object set_zlabel(const pybind11::tuple &args = pybind11::tuple(),
196196
const pybind11::dict &kwargs = pybind11::dict());
197197

198+
// set_zlim
199+
pybind11::object set_zlim(const pybind11::tuple &args = pybind11::tuple(),
200+
const pybind11::dict &kwargs = pybind11::dict());
201+
198202
// text
199203
pybind11::object text(const pybind11::tuple &args = pybind11::tuple(),
200204
const pybind11::dict &kwargs = pybind11::dict());
@@ -254,6 +258,7 @@ struct DECL_STRUCT_ATTR Axes : public BaseWrapper {
254258
LOAD_FUNC_ATTR(set_ylabel, self);
255259
LOAD_FUNC_ATTR(set_ylim, self);
256260
LOAD_FUNC_ATTR(set_yticks, self);
261+
LOAD_FUNC_ATTR(set_zlim, self);
257262
LOAD_FUNC_ATTR(text, self);
258263
LOAD_FUNC_ATTR(tick_params, self);
259264
}
@@ -295,6 +300,7 @@ struct DECL_STRUCT_ATTR Axes : public BaseWrapper {
295300
pybind11::object set_ylim_attr;
296301
pybind11::object set_yticks_attr;
297302
pybind11::object set_zlabel_attr;
303+
pybind11::object set_zlim_attr;
298304
pybind11::object text_attr;
299305
pybind11::object tick_params_attr;
300306
bool projection_3d;
@@ -597,6 +603,13 @@ pybind11::object Axes::set_zlabel(const pybind11::tuple &args,
597603
return ret;
598604
}
599605

606+
// set_zlim
607+
pybind11::object Axes::set_zlim(const pybind11::tuple &args,
608+
const pybind11::dict &kwargs) {
609+
pybind11::object ret = set_zlim_attr(*args, **kwargs);
610+
return ret;
611+
}
612+
600613
// text
601614
pybind11::object Axes::text(const pybind11::tuple &args,
602615
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)