Skip to content

Feature/matplotlib version attr check #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions gallery/shapes_and_collections/patch_collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion include/matplotlibcpp17/axes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions include/matplotlibcpp17/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,26 @@

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

#include <iostream>

#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 <pybind11/pybind11.h>

namespace matplotlibcpp17 {
Expand Down