diff --git a/CMakeLists.txt b/CMakeLists.txt index b18530b..471a120 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,29 @@ project( find_package(Python3 COMPONENTS Interpreter Development REQUIRED) find_package(pybind11 2.4.3 REQUIRED) -set(matplotlibcpp17_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/include") + +# check matplotlib minor version +execute_process( + COMMAND + "python3" "-c" + "import matplotlib; +print(str(matplotlib.__version__))" + RESULT_VARIABLE MATPLOTLIB_VERSION_CHECKING + OUTPUT_VARIABLE MATPLOTLIB_VERSION + ) + +if(NOT MATPLOTLIB_VERSION_CHECKING MATCHES 0) + message(FATAL_ERROR + "Could not check matplotlib.__version__") +endif() +message("Detected matplotlib version is ${MATPLOTLIB_VERSION}") +if(${MATPLOTLIB_VERSION} VERSION_LESS 3.4) + message(WARNING "Detected matplotlib version is < 3.4.0") + set(MATPLOTLIB_MINOR_VER_GTE_4 0) +else() + set(MATPLOTLIB_MINOR_VER_GTE_4 1) +endif() + # gallery if(NOT DEFINED USE_GUI) @@ -20,6 +42,8 @@ if(NOT DEFINED ADD_DEMO) set(ADD_DEMO 1) endif() +set(matplotlibcpp17_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/include") + function(add_demo name path) add_executable(${name} ${path}) target_include_directories(${name} PUBLIC @@ -33,7 +57,7 @@ if(${ADD_DEMO}) find_package(Python3 COMPONENTS NumPy REQUIRED) find_package(xtensor 0.24.0 REQUIRED) set(CMAKE_CXX_STANDARD 17) - set(CMAKE_CXX_FLAGS "-Wall -g -DUSE_GUI=${USE_GUI}") + set(CMAKE_CXX_FLAGS "-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/subplots_axes_and_figures) add_subdirectory(gallery/statistics) diff --git a/gallery/shapes_and_collections/patch_collection.cpp b/gallery/shapes_and_collections/patch_collection.cpp index 41e4554..3450c9e 100644 --- a/gallery/shapes_and_collections/patch_collection.cpp +++ b/gallery/shapes_and_collections/patch_collection.cpp @@ -60,6 +60,7 @@ int main() { py::list colors = py::cast(colors_); auto p = collections::PatchCollection(Args(patches), Kwargs("alpha"_a = 0.4)); p.set_array(Args(colors)); + // NOTE: error in python3.6.9 ? ax.add_collection(Args(p.unwrap())); fig.colorbar(Args(p.unwrap()), Kwargs("ax"_a = ax.unwrap())); #if USE_GUI diff --git a/include/matplotlibcpp17/axes.h b/include/matplotlibcpp17/axes.h index 5ea63cb..41ff7e8 100644 --- a/include/matplotlibcpp17/axes.h +++ b/include/matplotlibcpp17/axes.h @@ -206,7 +206,11 @@ struct DECL_STRUCT_ATTR Axes : public BaseWrapper { LOAD_FUNC_ATTR(add_patch, self); LOAD_FUNC_ATTR(axhline, self); LOAD_FUNC_ATTR(bar, self); +#if MATPLOTLIB_MINOR_VER_GTE_4 LOAD_FUNC_ATTR(bar_label, self); +#else + WARN_MSG("Not loading bar_label because matplotlib version is < 3.4.0"); +#endif LOAD_FUNC_ATTR(barh, self); LOAD_FUNC_ATTR(contour, self); LOAD_FUNC_ATTR(fill, self); @@ -227,7 +231,7 @@ struct DECL_STRUCT_ATTR Axes : public BaseWrapper { plot_surface_attr = self.attr("plot_surface"); plot_wireframe_attr = self.attr("plot_wireframe"); set_zlabel_attr = self.attr("set_zlabel"); - std::cout << "Loaded Axes3D." << std::endl; + INFO_MSG("Loaded Axes3D"); } catch(...) {} LOAD_FUNC_ATTR(quiver, self); @@ -325,8 +329,14 @@ container::BarContainer Axes::bar(const pybind11::tuple &args, // bar_label pybind11::object Axes::bar_label(const pybind11::tuple &args, const pybind11::dict &kwargs) { +#if MATPLOTLIB_MINOR_VER_GTE_4 pybind11::object ret = bar_label_attr(*args, **kwargs); return ret; +#else + ERROR_MSG( + "Call to bar_label is invalid because matplotlib version is < 3.4.0"); + std::exit(0); +#endif } // barh diff --git a/include/matplotlibcpp17/common.h b/include/matplotlibcpp17/common.h index 639030a..a1029f7 100644 --- a/include/matplotlibcpp17/common.h +++ b/include/matplotlibcpp17/common.h @@ -8,6 +8,26 @@ #define DECL_STRUCT_ATTR __attribute__((visibility("hidden"))) +#include + +#define INFO_MSG(msg) \ + do { \ + std::cout << "Info [" __FILE__ << "@" << __LINE__ << "]: "; \ + std::cout << #msg << std::endl; \ + } while (0) + +#define WARN_MSG(msg) \ + do { \ + std::cout << "Warn [" __FILE__ << "@" << __LINE__ << "]: "; \ + std::cout << #msg << std::endl; \ + } while (0) + +#define ERROR_MSG(msg) \ + do { \ + std::cerr << "Error [" __FILE__ << "@" << __LINE__ << "]: "; \ + std::cerr << #msg << std::endl; \ + } while (0) + #include namespace matplotlibcpp17 {