Thanks to visit codestin.com
Credit goes to github.com

Skip to content

added plot_scatter #113

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

Closed
wants to merge 1 commit into from
Closed
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
95 changes: 95 additions & 0 deletions matplotlibcpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,101 @@ void plot_surface(const std::vector<::std::vector<Numeric>> &x,
if (res) Py_DECREF(res);
}

template <typename Numeric>
void plot_scatter(const std::vector<::std::vector<Numeric>> &x,
const std::vector<::std::vector<Numeric>> &y,
const std::vector<::std::vector<Numeric>> &z)
{

//const std::map<std::string;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this PR ready? There seems to be a lot of dead code.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, forgot to delete irrelevant code taken from another example. I used this plot_scatter function in my code and it's working correctly...

//std::string> &keywords = std::map<std::string, std::string>()

// We lazily load the modules here the first time this function is called
// because I'm not sure that we can assume "matplotlib installed" implies
// "mpl_toolkits installed" on all platforms, and we don't want to require
// it for people who don't need 3d plots.
static PyObject *mpl_toolkitsmod = nullptr, *axis3dmod = nullptr;
if (!mpl_toolkitsmod) {
detail::_interpreter::get();

PyObject* mpl_toolkits = PyString_FromString("mpl_toolkits");
PyObject* axis3d = PyString_FromString("mpl_toolkits.mplot3d");
if (!mpl_toolkits || !axis3d) { throw std::runtime_error("couldnt create string"); }

mpl_toolkitsmod = PyImport_Import(mpl_toolkits);
Py_DECREF(mpl_toolkits);
if (!mpl_toolkitsmod) { throw std::runtime_error("Error loading module mpl_toolkits!"); }

axis3dmod = PyImport_Import(axis3d);
Py_DECREF(axis3d);
if (!axis3dmod) { throw std::runtime_error("Error loading module mpl_toolkits.mplot3d!"); }
}

assert(x.size() == y.size());
assert(y.size() == z.size());

// using numpy arrays
PyObject *xarray = get_2darray(x);
PyObject *yarray = get_2darray(y);
PyObject *zarray = get_2darray(z);

// construct positional args
PyObject *args = PyTuple_New(3);
PyTuple_SetItem(args, 0, xarray);
PyTuple_SetItem(args, 1, yarray);
PyTuple_SetItem(args, 2, zarray);

// Build up the kw args.
PyObject *kwargs = PyDict_New();
//PyDict_SetItemString(kwargs, "rstride", PyInt_FromLong(1));
//PyDict_SetItemString(kwargs, "cstride", PyInt_FromLong(1));

// PyObject *python_colormap_coolwarm = PyObject_GetAttrString( \
detail::_interpreter::get().s_python_colormap, "coolwarm");

//PyDict_SetItemString(kwargs, "cmap", python_colormap_coolwarm);
/*
for (std::map<std::string, std::string>::const_iterator it = keywords.begin();
it != keywords.end(); ++it) {
PyDict_SetItemString(kwargs, it->first.c_str(),
PyString_FromString(it->second.c_str()));
}
*/

PyObject *fig =
PyObject_CallObject(detail::_interpreter::get().s_python_function_figure,
detail::_interpreter::get().s_python_empty_tuple);
if (!fig) throw std::runtime_error("Call to figure() failed.");

PyObject *gca_kwargs = PyDict_New();
PyDict_SetItemString(gca_kwargs, "projection", PyString_FromString("3d"));

PyObject *gca = PyObject_GetAttrString(fig, "gca");
if (!gca) throw std::runtime_error("No gca");
Py_INCREF(gca);
PyObject *axis = PyObject_Call(
gca, detail::_interpreter::get().s_python_empty_tuple, gca_kwargs);

if (!axis) throw std::runtime_error("No axis");
Py_INCREF(axis);

Py_DECREF(gca);
Py_DECREF(gca_kwargs);

PyObject *scatter = PyObject_GetAttrString(axis, "scatter");
if (!scatter) throw std::runtime_error("No scatter");

Py_INCREF(scatter);
PyObject *res = PyObject_Call(scatter, args, kwargs);
if (!res) throw std::runtime_error("failed scatter");
Py_DECREF(scatter);

Py_DECREF(axis);
Py_DECREF(args);
Py_DECREF(kwargs);
if (res) Py_DECREF(res);
}

template<typename Numeric>
bool stem(const std::vector<Numeric> &x, const std::vector<Numeric> &y, const std::map<std::string, std::string>& keywords)
{
Expand Down