From efa69ad5f00dcc2509a12b6fc764f821ce11fbb2 Mon Sep 17 00:00:00 2001 From: kokosxD Date: Fri, 21 May 2021 18:34:21 +0300 Subject: [PATCH 1/4] Fix explicit specialization has already been defined On WIndows only --- matplotlibcpp.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index d95d46ad..f3d56923 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -350,10 +350,12 @@ 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 +#ifndef _WIN32 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; }; +#endif template PyObject* get_array(const std::vector& v) From cadb1cb40c9758798394666ee6368ae442c02947 Mon Sep 17 00:00:00 2001 From: kokosxD Date: Fri, 21 May 2021 18:35:59 +0300 Subject: [PATCH 2/4] Define math constants for Visual Studio Like M_PI as mentioned in some examples --- README.md | 8 +------- matplotlibcpp.h | 5 +++++ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 0f8479f1..2fa128e2 100644 --- a/README.md +++ b/README.md @@ -26,8 +26,6 @@ int main() { A more comprehensive example: ```cpp #include "matplotlibcpp.h" -#include - namespace plt = matplotlibcpp; int main() @@ -67,9 +65,7 @@ int main() Alternatively, matplotlib-cpp also supports some C++11-powered syntactic sugar: ```cpp -#include #include "matplotlibcpp.h" - using namespace std; namespace plt = matplotlibcpp; @@ -104,8 +100,6 @@ Or some *funny-looking xkcd-styled* example: ```cpp #include "matplotlibcpp.h" #include -#include - namespace plt = matplotlibcpp; int main() { @@ -291,4 +285,4 @@ Todo/Issues/Wishlist in "".' * MacOS: `Unable to import matplotlib.pyplot`. Cause: In mac os image rendering back end of matplotlib (what-is-a-backend to render using the API of Cocoa by default). There is Qt4Agg and GTKAgg and as a back-end is not the default. Set the back end of macosx that is differ compare with other windows or linux os. -Solution is described [here](https://stackoverflow.com/questions/21784641/installation-issue-with-matplotlib-python?noredirect=1&lq=1), additional information can be found there too(see links in answers). +Solution is described [here](https://stackoverflow.com/questions/21784641/installation-issue-with-matplotlib-python?noredirect=1&lq=1), additional information can be found there too(see links in answers). \ No newline at end of file diff --git a/matplotlibcpp.h b/matplotlibcpp.h index f3d56923..f6d1f92e 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -1,5 +1,10 @@ #pragma once +// Define math constants for Visual Studio https://docs.microsoft.com/en-us/cpp/c-runtime-library/math-constants +#if defined(_MSC_VER) +#define _USE_MATH_DEFINES +#endif + // Python headers must be included before any system headers, since // they define _POSIX_C_SOURCE #include From 616a1ea84b4181cd14096ef0f6a5cc881cdf21a9 Mon Sep 17 00:00:00 2001 From: kokosxD Date: Fri, 21 May 2021 18:37:30 +0300 Subject: [PATCH 3/4] Fix C4244 convertion warnings See [Visual Studio Compiler Warning C4244](https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-levels-3-and-4-c4244). --- matplotlibcpp.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index f6d1f92e..b9cd4c5d 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -1817,7 +1817,7 @@ template bool plot(const std::vector& y, const std::string& format = "") { std::vector x(y.size()); - for(size_t i=0; i(i); return plot(x,y,format); } @@ -1825,7 +1825,7 @@ template bool plot(const std::vector& y, const std::map& keywords) { std::vector x(y.size()); - for(size_t i=0; i(i); return plot(x,y,keywords); } @@ -1833,7 +1833,7 @@ template bool stem(const std::vector& y, const std::string& format = "") { std::vector x(y.size()); - for (size_t i = 0; i < x.size(); ++i) x.at(i) = i; + for(size_t i = 0; i < x.size(); ++i) x.at(i) = static_cast(i); return stem(x, y, format); } From f59b63b52b400b76fde0dce9a899bd8fab5d1db5 Mon Sep 17 00:00:00 2001 From: Denton Woods Date: Sun, 29 Aug 2021 08:31:25 -0500 Subject: [PATCH 4/4] Toggle building examples and fixed Windows issues - Added MATPLOTLIBCPP_BUILD_EXAMPLES to CMake (good for disabling when importing this into other projects) - Fixed several cast warnings - Fixed Python header trying to link to debug library --- .gitignore | 1 + CMakeLists.txt | 130 +++++++++++++++++++++--------------------- examples/colorbar.cpp | 2 +- examples/lines3d.cpp | 2 +- examples/update.cpp | 4 +- matplotlibcpp.h | 15 +++-- 6 files changed, 82 insertions(+), 72 deletions(-) diff --git a/.gitignore b/.gitignore index 1c4a1b0a..929245e0 100644 --- a/.gitignore +++ b/.gitignore @@ -33,6 +33,7 @@ # Build /examples/build/* +build/ # vim temp files *.sw* diff --git a/CMakeLists.txt b/CMakeLists.txt index bb2decd8..f8cf8f0f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,7 @@ include(GNUInstallDirs) set(PACKAGE_NAME matplotlib_cpp) set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/${PACKAGE_NAME}/cmake) +option(MATPLOTLIBCPP_BUILD_EXAMPLES "Compile example programs" ON) # Library target add_library(matplotlib_cpp INTERFACE) @@ -37,72 +38,73 @@ install( # 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") +if(MATPLOTLIBCPP_BUILD_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() endif() - # Install headers install(FILES "${PROJECT_SOURCE_DIR}/matplotlibcpp.h" diff --git a/examples/colorbar.cpp b/examples/colorbar.cpp index f53e01da..77c7354f 100644 --- a/examples/colorbar.cpp +++ b/examples/colorbar.cpp @@ -13,7 +13,7 @@ int main() std::vector z(ncols * nrows); for (int j=0; j(std::sin(std::hypot(i - ncols/2, j - nrows/2))); } } diff --git a/examples/lines3d.cpp b/examples/lines3d.cpp index fd4610d2..e0b9c581 100644 --- a/examples/lines3d.cpp +++ b/examples/lines3d.cpp @@ -10,7 +10,7 @@ int main() double theta, r; double z_inc = 4.0/99.0; double theta_inc = (8.0 * M_PI)/99.0; - for (double i = 0; i < 100; i += 1) { + for (int i = 0; i < 100; i += 1) { theta = -4.0 * M_PI + theta_inc*i; z.push_back(-2.0 + z_inc*i); r = z[i]*z[i] + 1; diff --git a/examples/update.cpp b/examples/update.cpp index 64f49067..d493cdeb 100644 --- a/examples/update.cpp +++ b/examples/update.cpp @@ -24,9 +24,9 @@ int main() std::vector x, y; const double w = 0.05; - const double a = n/2; + const double a = n/2.0; - for (size_t i=0; i +#if defined(_WIN32) && defined(_DEBUG) + // Python.h will try to load the library debug version, but that is not installed by default. + #undef _DEBUG + #include + #define _DEBUG +#else + #include +#endif #include #include @@ -282,7 +289,7 @@ struct _interpreter { s_python_function_colorbar = PyObject_GetAttrString(pymod, "colorbar"); s_python_function_subplots_adjust = safe_import(pymod,"subplots_adjust"); s_python_function_rcparams = PyObject_GetAttrString(pymod, "rcParams"); - s_python_function_spy = PyObject_GetAttrString(pymod, "spy"); + s_python_function_spy = PyObject_GetAttrString(pymod, "spy"); #ifndef WITHOUT_NUMPY s_python_function_imshow = safe_import(pymod, "imshow"); #endif @@ -1277,7 +1284,7 @@ bool bar(const std::vector & y, detail::_interpreter::get(); std::vector x; - for (std::size_t i = 0; i < y.size(); i++) { x.push_back(i); } + for (std::size_t i = 0; i < y.size(); i++) { x.push_back(static_cast(i)); } return bar(x, y, ec, ls, lw, keywords); } @@ -2825,7 +2832,7 @@ struct plot_impl PyObject* pystring = PyString_FromString(format.c_str()); auto itx = begin(x), ity = begin(y); - for(size_t i = 0; i < xs; ++i) { + for(int i = 0; i < xs; ++i) { PyList_SetItem(xlist, i, PyFloat_FromDouble(*itx++)); PyList_SetItem(ylist, i, PyFloat_FromDouble(*ity++)); }