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/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/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 @@ -277,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 @@ -350,10 +362,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) @@ -1270,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); } @@ -1810,7 +1824,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); } @@ -1818,7 +1832,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); } @@ -1826,7 +1840,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); } @@ -2818,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++)); }