matplotlib-cpp icon indicating copy to clipboard operation
matplotlib-cpp copied to clipboard

Small fixes to use with Visual Studio

Open GrandChris opened this issue 5 years ago • 2 comments

Hello, I encountered small issues while using this library with Visual Studio 2019 and Python 3.8.5.

First

On a debug build, it tries to link against the Debug binaries (.dll) of Python and Numpy. For Python, the debug binaries can be downloaded during installation, for Numpy, there are no such binaries available (unless you compile it yourself). For this reason, it is required to always link against the release binaries.

#if defined(_DEBUG)
    #undef _DEBUG
        // matplotlib would require a python and numpy debug library (not porvided)
        #include <matplotlibcpp.h>
    #define _DEBUG
#else
    #include <matplotlibcpp.h>
#endif

Second

#incude <string>

is missing, as already pointed out by https://github.com/lava/matplotlib-cpp/pull/218.

Third

There are some lines producing warnings.

$ git diff master bugfix/fixes-to-work-with-VisualStudio
diff --git a/matplotlibcpp.h b/matplotlibcpp.h
index 6770074..4ed3eb7 100644
--- a/matplotlibcpp.h
+++ b/matplotlibcpp.h
@@ -313,9 +313,9 @@ template <> struct select_npy_type<uint64_t> { 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<long long> { const static NPY_TYPES type = NPY_INT64; };
+//template <> struct select_npy_type<long long> { const static NPY_TYPES type = NPY_INT64; };
 static_assert(sizeof(unsigned long long) == 8);
-template <> struct select_npy_type<unsigned long long> { const static NPY_TYPES type = NPY_UINT64; };
+//template <> struct select_npy_type<unsigned long long> { const static NPY_TYPES type = NPY_UINT64; };
 // TODO: add int, long, etc.

 template<typename Numeric>
@@ -868,7 +868,7 @@ bool scatter(const std::vector<NumericX>& x,
     PyObject* yarray = detail::get_array(y);

     PyObject* kwargs = PyDict_New();
-    PyDict_SetItemString(kwargs, "s", PyLong_FromLong(s));
+    PyDict_SetItemString(kwargs, "s", PyLong_FromLong(static_cast<long>(s)));
     for (const auto& it : keywords)
     {
         PyDict_SetItemString(kwargs, it.first.c_str(), PyString_FromString(it.second.c_str()));
@@ -1424,7 +1424,7 @@ template<typename Numeric>
 bool plot(const std::vector<Numeric>& y, const std::string& format = "")
 {
     std::vector<Numeric> 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<Numeric>(i);
     return plot(x,y,format);
 }

GrandChris avatar Aug 30 '20 19:08 GrandChris

hi,

thank for your fixes but it doesn't work for me in

bool plot(const std::vector<Numeric>& y, const std::string& format = "") { std::vector<Numeric> x(y.size()); for(size_t i=0; i<x.size(); ++i) x.at(i) = static_cast<Numeric>(i); return plot(x,y,format); }

Returning me error C2440 'impossible to cast "size_t" into "Numeric"'

I'm using Visual Studio 2019, and I have installed matplotlib-cpp with vcpkg.

Do you know how I can fix this issue ? thanks

kingovchouffe avatar Apr 07 '21 12:04 kingovchouffe

hi kingovchouffe,

Numeric is a template with your type. I guess that you are using some weird type which can't be casted to size_t.

Current function declaration of matplotlib.h:1809:

template<typename Numeric>
bool plot(const std::vector<Numeric>& y, const std::string& format = "")
{
    std::vector<Numeric> x(y.size());
    for(size_t i=0; i<x.size(); ++i) x.at(i) = i;
    return plot(x,y,format);
}

GrandChris avatar Apr 07 '21 21:04 GrandChris