Skip to content

Commit 311bbe3

Browse files
author
MamoruSobue
committed
adding # if-else-endif for matplotlib minor version
1 parent 80d6e78 commit 311bbe3

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

CMakeLists.txt

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,29 @@ project(
1010
find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
1111
find_package(pybind11 2.4.3 REQUIRED)
1212

13-
set(matplotlibcpp17_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/include")
13+
14+
# check matplotlib minor version
15+
execute_process(
16+
COMMAND
17+
"python3" "-c"
18+
"import matplotlib;
19+
print(str(matplotlib.__version__))"
20+
RESULT_VARIABLE MATPLOTLIB_VERSION_CHECKING
21+
OUTPUT_VARIABLE MATPLOTLIB_VERSION
22+
)
23+
24+
if(NOT MATPLOTLIB_VERSION_CHECKING MATCHES 0)
25+
message(FATAL_ERROR
26+
"Could not check matplotlib.__version__")
27+
endif()
28+
message("Detected matplotlib version is ${MATPLOTLIB_VERSION}")
29+
if(${MATPLOTLIB_VERSION} VERSION_LESS 3.4)
30+
message(WARNING "Detected matplotlib version is < 3.4.0")
31+
set(MATPLOTLIB_MINOR_VER_GTE_4 0)
32+
else()
33+
set(MATPLOTLIB_MINOR_VER_GTE_4 1)
34+
endif()
35+
1436

1537
# gallery
1638
if(NOT DEFINED USE_GUI)
@@ -20,6 +42,8 @@ if(NOT DEFINED ADD_DEMO)
2042
set(ADD_DEMO 1)
2143
endif()
2244

45+
set(matplotlibcpp17_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/include")
46+
2347
function(add_demo name path)
2448
add_executable(${name} ${path})
2549
target_include_directories(${name} PUBLIC
@@ -33,7 +57,7 @@ if(${ADD_DEMO})
3357
find_package(Python3 COMPONENTS NumPy REQUIRED)
3458
find_package(xtensor 0.24.0 REQUIRED)
3559
set(CMAKE_CXX_STANDARD 17)
36-
set(CMAKE_CXX_FLAGS "-Wall -g -DUSE_GUI=${USE_GUI}")
60+
set(CMAKE_CXX_FLAGS "-Wall -g -DUSE_GUI=${USE_GUI} -DMATPLOTLIB_MINOR_VER_GTE_4=${MATPLOTLIB_MINOR_VER_GTE_4}")
3761
add_subdirectory(gallery/lines_bars_and_markers)
3862
add_subdirectory(gallery/subplots_axes_and_figures)
3963
add_subdirectory(gallery/statistics)

gallery/shapes_and_collections/patch_collection.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ int main() {
6060
py::list colors = py::cast(colors_);
6161
auto p = collections::PatchCollection(Args(patches), Kwargs("alpha"_a = 0.4));
6262
p.set_array(Args(colors));
63+
// NOTE: error in python3.6.9 ?
6364
ax.add_collection(Args(p.unwrap()));
6465
fig.colorbar(Args(p.unwrap()), Kwargs("ax"_a = ax.unwrap()));
6566
#if USE_GUI

include/matplotlibcpp17/axes.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,11 @@ struct DECL_STRUCT_ATTR Axes : public BaseWrapper {
206206
LOAD_FUNC_ATTR(add_patch, self);
207207
LOAD_FUNC_ATTR(axhline, self);
208208
LOAD_FUNC_ATTR(bar, self);
209+
#if MATPLOTLIB_MINOR_VER_GTE_4
209210
LOAD_FUNC_ATTR(bar_label, self);
211+
#else
212+
WARN_MSG("Not loading bar_label because matplotlib version is < 3.4.0");
213+
#endif
210214
LOAD_FUNC_ATTR(barh, self);
211215
LOAD_FUNC_ATTR(contour, self);
212216
LOAD_FUNC_ATTR(fill, self);
@@ -227,7 +231,7 @@ struct DECL_STRUCT_ATTR Axes : public BaseWrapper {
227231
plot_surface_attr = self.attr("plot_surface");
228232
plot_wireframe_attr = self.attr("plot_wireframe");
229233
set_zlabel_attr = self.attr("set_zlabel");
230-
std::cout << "Loaded Axes3D." << std::endl;
234+
INFO_MSG("Loaded Axes3D");
231235
}
232236
catch(...) {}
233237
LOAD_FUNC_ATTR(quiver, self);
@@ -325,8 +329,14 @@ container::BarContainer Axes::bar(const pybind11::tuple &args,
325329
// bar_label
326330
pybind11::object Axes::bar_label(const pybind11::tuple &args,
327331
const pybind11::dict &kwargs) {
332+
#if MATPLOTLIB_MINOR_VER_GTE_4
328333
pybind11::object ret = bar_label_attr(*args, **kwargs);
329334
return ret;
335+
#else
336+
ERROR_MSG(
337+
"Call to bar_label is invalid because matplotlib version is < 3.4.0");
338+
std::exit(0);
339+
#endif
330340
}
331341

332342
// barh

include/matplotlibcpp17/common.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,26 @@
88

99
#define DECL_STRUCT_ATTR __attribute__((visibility("hidden")))
1010

11+
#include <iostream>
12+
13+
#define INFO_MSG(msg) \
14+
do { \
15+
std::cout << "Info [" __FILE__ << "@" << __LINE__ << "]: "; \
16+
std::cout << #msg << std::endl; \
17+
} while (0)
18+
19+
#define WARN_MSG(msg) \
20+
do { \
21+
std::cout << "Warn [" __FILE__ << "@" << __LINE__ << "]: "; \
22+
std::cout << #msg << std::endl; \
23+
} while (0)
24+
25+
#define ERROR_MSG(msg) \
26+
do { \
27+
std::cerr << "Error [" __FILE__ << "@" << __LINE__ << "]: "; \
28+
std::cerr << #msg << std::endl; \
29+
} while (0)
30+
1131
#include <pybind11/pybind11.h>
1232

1333
namespace matplotlibcpp17 {

0 commit comments

Comments
 (0)