From 75139f3c74407a420ad82388ca6a9776748c4852 Mon Sep 17 00:00:00 2001 From: James Butterworth Date: Wed, 3 Aug 2022 10:59:24 +0100 Subject: [PATCH 1/2] Greatly simplified CMakeLists.txt --- CMakeLists.txt | 237 ++++++++++++++++++++++++++---------------------- matplotlibcpp.h | 8 +- 2 files changed, 135 insertions(+), 110 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bb2decd..d9dc4d0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,133 +1,158 @@ cmake_minimum_required(VERSION 3.8 FATAL_ERROR) project(matplotlib_cpp LANGUAGES CXX) -include(GNUInstallDirs) -set(PACKAGE_NAME matplotlib_cpp) -set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/${PACKAGE_NAME}/cmake) - - -# Library target add_library(matplotlib_cpp INTERFACE) -target_include_directories(matplotlib_cpp - INTERFACE - $ - $ -) -target_compile_features(matplotlib_cpp INTERFACE - cxx_std_11 + +target_include_directories(matplotlib_cpp INTERFACE + ${CMAKE_CURRENT_SOURCE_DIR} ) -# TODO: Use `Development.Embed` component when requiring cmake >= 3.18 -find_package(Python3 COMPONENTS Interpreter Development REQUIRED) + +find_package(Python3 COMPONENTS Interpreter Development NumPy REQUIRED) +find_package(PythonLibs REQUIRED) target_link_libraries(matplotlib_cpp INTERFACE - Python3::Python - Python3::Module -) -find_package(Python3 COMPONENTS NumPy) -if(Python3_NumPy_FOUND) - target_link_libraries(matplotlib_cpp INTERFACE Python3::NumPy - ) -else() - target_compile_definitions(matplotlib_cpp INTERFACE WITHOUT_NUMPY) -endif() -install( - TARGETS matplotlib_cpp - EXPORT install_targets + ${PYTHON_LIBRARIES} ) -# Examples -add_executable(minimal examples/minimal.cpp) -target_link_libraries(minimal PRIVATE matplotlib_cpp) -set_target_properties(minimal PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") - -add_executable(basic examples/basic.cpp) -target_link_libraries(basic PRIVATE matplotlib_cpp) -set_target_properties(basic PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") - -add_executable(modern examples/modern.cpp) -target_link_libraries(modern PRIVATE matplotlib_cpp) -set_target_properties(modern PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") +################## +# ORIGINAL CMAKE # +################## -add_executable(animation examples/animation.cpp) -target_link_libraries(animation PRIVATE matplotlib_cpp) -set_target_properties(animation PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") +#cmake_minimum_required(VERSION 3.8 FATAL_ERROR) +#project(matplotlib_cpp LANGUAGES CXX) -add_executable(nonblock examples/nonblock.cpp) -target_link_libraries(nonblock PRIVATE matplotlib_cpp) -set_target_properties(nonblock PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") +#add_library(matplotlib_cpp INTERFACE) -add_executable(xkcd examples/xkcd.cpp) -target_link_libraries(xkcd PRIVATE matplotlib_cpp) -set_target_properties(xkcd PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") +#include(GNUInstallDirs) +#set(PACKAGE_NAME matplotlib_cpp) +#set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/${PACKAGE_NAME}/cmake) -add_executable(bar examples/bar.cpp) -target_link_libraries(bar PRIVATE matplotlib_cpp) -set_target_properties(bar PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") -add_executable(fill_inbetween examples/fill_inbetween.cpp) -target_link_libraries(fill_inbetween PRIVATE matplotlib_cpp) -set_target_properties(fill_inbetween PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") +#target_include_directories(matplotlib_cpp +# INTERFACE +# $ +# $ +#) -add_executable(fill examples/fill.cpp) -target_link_libraries(fill PRIVATE matplotlib_cpp) -set_target_properties(fill PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") - -add_executable(update examples/update.cpp) -target_link_libraries(update PRIVATE matplotlib_cpp) -set_target_properties(update PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") - -add_executable(subplot2grid examples/subplot2grid.cpp) -target_link_libraries(subplot2grid PRIVATE matplotlib_cpp) -set_target_properties(subplot2grid PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") - -add_executable(lines3d examples/lines3d.cpp) -target_link_libraries(lines3d PRIVATE matplotlib_cpp) -set_target_properties(lines3d PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") - -if(Python3_NumPy_FOUND) - add_executable(surface examples/surface.cpp) - target_link_libraries(surface PRIVATE matplotlib_cpp) - set_target_properties(surface PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") +#target_compile_features(matplotlib_cpp INTERFACE +# cxx_std_11 +#) +# TODO: Use `Development.Embed` component when requiring cmake >= 3.18 +#find_package(Python3 COMPONENTS Interpreter Development REQUIRED) +#target_link_libraries(matplotlib_cpp INTERFACE + #Python3::Python + #Python3::Module + #) +#find_package(Python3 COMPONENTS NumPy) + +#if(Python3_NumPy_FOUND) +# target_link_libraries(matplotlib_cpp INTERFACE +# Python3::NumPy +# ) +#else() +# target_compile_definitions(matplotlib_cpp INTERFACE WITHOUT_NUMPY) +#endif() + +#install( +# TARGETS matplotlib_cpp +# EXPORT install_targets +#) - add_executable(colorbar examples/colorbar.cpp) - target_link_libraries(colorbar PRIVATE matplotlib_cpp) - set_target_properties(colorbar PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") - add_executable(contour examples/contour.cpp) - target_link_libraries(contour PRIVATE matplotlib_cpp) - set_target_properties(contour PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") - add_executable(spy examples/spy.cpp) - target_link_libraries(spy PRIVATE matplotlib_cpp) - set_target_properties(spy PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") -endif() +# Examples +#add_executable(minimal examples/minimal.cpp) +#target_link_libraries(minimal PRIVATE matplotlib_cpp) +#set_target_properties(minimal PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") +# +#add_executable(basic examples/basic.cpp) +#target_link_libraries(basic PRIVATE matplotlib_cpp) +#set_target_properties(basic PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") +# +#add_executable(modern examples/modern.cpp) +#target_link_libraries(modern PRIVATE matplotlib_cpp) +#set_target_properties(modern PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") +# +#add_executable(animation examples/animation.cpp) +#target_link_libraries(animation PRIVATE matplotlib_cpp) +#set_target_properties(animation PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") +# +#add_executable(nonblock examples/nonblock.cpp) +#target_link_libraries(nonblock PRIVATE matplotlib_cpp) +#set_target_properties(nonblock PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") +# +#add_executable(xkcd examples/xkcd.cpp) +#target_link_libraries(xkcd PRIVATE matplotlib_cpp) +#set_target_properties(xkcd PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") +# +#add_executable(bar examples/bar.cpp) +#target_link_libraries(bar PRIVATE matplotlib_cpp) +#set_target_properties(bar PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") +# +#add_executable(fill_inbetween examples/fill_inbetween.cpp) +#target_link_libraries(fill_inbetween PRIVATE matplotlib_cpp) +#set_target_properties(fill_inbetween PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") +# +#add_executable(fill examples/fill.cpp) +#target_link_libraries(fill PRIVATE matplotlib_cpp) +#set_target_properties(fill PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") +# +#add_executable(update examples/update.cpp) +#target_link_libraries(update PRIVATE matplotlib_cpp) +#set_target_properties(update PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") +# +#add_executable(subplot2grid examples/subplot2grid.cpp) +#target_link_libraries(subplot2grid PRIVATE matplotlib_cpp) +#set_target_properties(subplot2grid PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") +# +#add_executable(lines3d examples/lines3d.cpp) +#target_link_libraries(lines3d PRIVATE matplotlib_cpp) +#set_target_properties(lines3d PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") +# +#if(Python3_NumPy_FOUND) +# add_executable(surface examples/surface.cpp) +# target_link_libraries(surface PRIVATE matplotlib_cpp) +# set_target_properties(surface PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") +# +# add_executable(colorbar examples/colorbar.cpp) +# target_link_libraries(colorbar PRIVATE matplotlib_cpp) +# set_target_properties(colorbar PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") +# add_executable(contour examples/contour.cpp) +# target_link_libraries(contour PRIVATE matplotlib_cpp) +# set_target_properties(contour PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") + +# add_executable(spy examples/spy.cpp) +# target_link_libraries(spy PRIVATE matplotlib_cpp) +# set_target_properties(spy PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") +#endif() # Install headers -install(FILES - "${PROJECT_SOURCE_DIR}/matplotlibcpp.h" - DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) +#install(FILES +# "${PROJECT_SOURCE_DIR}/matplotlibcpp.h" +# DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} +#) # Install targets file -install(EXPORT install_targets - FILE - ${PACKAGE_NAME}Targets.cmake - NAMESPACE - ${PACKAGE_NAME}:: - DESTINATION - ${INSTALL_CONFIGDIR} -) +#install(EXPORT install_targets +# FILE +# ${PACKAGE_NAME}Targets.cmake +# NAMESPACE +# ${PACKAGE_NAME}:: +# DESTINATION +# ${INSTALL_CONFIGDIR} +#) # Install matplotlib_cppConfig.cmake -include(CMakePackageConfigHelpers) -configure_package_config_file( - ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${PACKAGE_NAME}Config.cmake.in - ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE_NAME}Config.cmake - INSTALL_DESTINATION ${INSTALL_CONFIGDIR} -) -install(FILES - ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE_NAME}Config.cmake - DESTINATION ${INSTALL_CONFIGDIR} -) +#include(CMakePackageConfigHelpers) +#configure_package_config_file( +# ${CMAKE_CURRENT_SOURCE_DIR}/cmake/${PACKAGE_NAME}Config.cmake.in +# ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE_NAME}Config.cmake +# INSTALL_DESTINATION ${INSTALL_CONFIGDIR} +#) +#install(FILES +# ${CMAKE_CURRENT_BINARY_DIR}/${PACKAGE_NAME}Config.cmake +# DESTINATION ${INSTALL_CONFIGDIR} +#) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index d95d46a..8e35492 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -350,10 +350,10 @@ template <> struct select_npy_type { const static NPY_TYPES type = NPY // Sanity checks; comment them out or change the numpy type below if you're compiling on // a platform where they don't apply -static_assert(sizeof(long long) == 8); -template <> struct select_npy_type { const static NPY_TYPES type = NPY_INT64; }; -static_assert(sizeof(unsigned long long) == 8); -template <> struct select_npy_type { const static NPY_TYPES type = NPY_UINT64; }; +//static_assert(sizeof(long long) == 8); +//template <> struct select_npy_type { const static NPY_TYPES type = NPY_INT64; }; +//static_assert(sizeof(unsigned long long) == 8); +//template <> struct select_npy_type { const static NPY_TYPES type = NPY_UINT64; }; template PyObject* get_array(const std::vector& v) From 76861734b3e4b54c333745464b4ec800309f6ad7 Mon Sep 17 00:00:00 2001 From: James Butterworth Date: Wed, 3 Aug 2022 12:25:56 +0100 Subject: [PATCH 2/2] Added options to give defined bins to histogram --- matplotlibcpp.h | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index 8e35492..6d72540 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -884,8 +884,8 @@ bool arrow(Numeric x, Numeric y, Numeric end_x, Numeric end_y, const std::string } template< typename Numeric> -bool hist(const std::vector& y, long bins=10,std::string color="b", - double alpha=1.0, bool cumulative=false) +bool hist(const std::vector& y, const long bins=10, + std::string color="b", double alpha=1.0, bool cumulative=false) { detail::_interpreter::get(); @@ -901,9 +901,39 @@ bool hist(const std::vector& y, long bins=10,std::string color="b", PyTuple_SetItem(plot_args, 0, yarray); + PyObject* res = PyObject_Call( + detail::_interpreter::get().s_python_function_hist, plot_args, kwargs + ); - PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_hist, plot_args, kwargs); + Py_DECREF(plot_args); + Py_DECREF(kwargs); + if(res) Py_DECREF(res); + + return res; +} + +template< typename Numeric> +bool hist(const std::vector& y, const std::vector& bin_ticks, + std::string color="b", double alpha=1.0, bool cumulative=false) +{ + detail::_interpreter::get(); + PyObject* yarray = detail::get_array(y); + PyObject* bin_array = detail::get_array(bin_ticks); + + PyObject* kwargs = PyDict_New(); + PyDict_SetItemString(kwargs, "bins", bin_array); + PyDict_SetItemString(kwargs, "color", PyString_FromString(color.c_str())); + PyDict_SetItemString(kwargs, "alpha", PyFloat_FromDouble(alpha)); + PyDict_SetItemString(kwargs, "cumulative", cumulative ? Py_True : Py_False); + + PyObject* plot_args = PyTuple_New(1); + + PyTuple_SetItem(plot_args, 0, yarray); + + PyObject* res = PyObject_Call( + detail::_interpreter::get().s_python_function_hist, plot_args, kwargs + ); Py_DECREF(plot_args); Py_DECREF(kwargs);