From c66f74b66c89dd268a85777bb1bbcc1a1248b0e0 Mon Sep 17 00:00:00 2001 From: Matthew Mosley Date: Sun, 14 Oct 2018 16:01:37 -0400 Subject: [PATCH] added support for scatter plots --- matplotlibcpp.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/matplotlibcpp.h b/matplotlibcpp.h index c4b27678..9e61d71c 100644 --- a/matplotlibcpp.h +++ b/matplotlibcpp.h @@ -43,6 +43,7 @@ struct _interpreter { PyObject *s_python_function_loglog; PyObject *s_python_function_fill_between; PyObject *s_python_function_hist; + PyObject *s_python_function_scatter; PyObject *s_python_function_subplot; PyObject *s_python_function_legend; PyObject *s_python_function_xlim; @@ -146,6 +147,7 @@ struct _interpreter { s_python_function_loglog = PyObject_GetAttrString(pymod, "loglog"); s_python_function_fill_between = PyObject_GetAttrString(pymod, "fill_between"); s_python_function_hist = PyObject_GetAttrString(pymod,"hist"); + s_python_function_scatter = PyObject_GetAttrString(pymod,"scatter"); s_python_function_subplot = PyObject_GetAttrString(pymod, "subplot"); s_python_function_legend = PyObject_GetAttrString(pymod, "legend"); s_python_function_ylim = PyObject_GetAttrString(pymod, "ylim"); @@ -438,6 +440,31 @@ bool hist(const std::vector& y, long bins=10,std::string color="b", dou return res; } +template +bool scatter(const std::vector& x, + const std::vector& y, + const double s=1.0) // The marker size in points**2 +{ + assert(x.size() == y.size()); + + PyObject* xarray = get_array(x); + PyObject* yarray = get_array(y); + + PyObject* kwargs = PyDict_New(); + PyDict_SetItemString(kwargs, "s", PyLong_FromLong(s)); + + PyObject* plot_args = PyTuple_New(2); + PyTuple_SetItem(plot_args, 0, xarray); + PyTuple_SetItem(plot_args, 1, yarray); + + PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_scatter, plot_args, kwargs); + + Py_DECREF(plot_args); + if(res) Py_DECREF(res); + + return res; +} + template< typename Numeric> bool named_hist(std::string label,const std::vector& y, long bins=10, std::string color="b", double alpha=1.0) {