8000 template to handle strings on x axis w/ legend · impressostudios/matplotlib-cpp@84148ab · GitHub
[go: up one dir, main page]

Skip to content

Commit 84148ab

Browse files
template to handle strings on x axis w/ legend
bool named_plot(const std::string& name, const std::vector<std::string>& x, const std::vector<Numeric>& y, const std::string& format = "") { .. } added template to be able to have named legend plotting: ex: time(x) vs signal(y) with legend. Prior to this change, the current library implements simple plots with no legends and with this change the above should be possible.
1 parent 70d508f commit 84148ab

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

matplotlibcpp.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,33 @@ bool named_plot(const std::string& name, const std::vector<Numeric>& x, const st
13941394
return res;
13951395
}
13961396

1397+
template<typename Numeric>
1398+
bool named_plot(const std::string& name, const std::vector<std::string>& x, const std::vector<Numeric>& y, const std::string& format = "")
1399+
{
1400+
detail::_interpreter::get();
1401+
1402+
PyObject* kwargs = PyDict_New();
1403+
PyDict_SetItemString(kwargs, "label", PyString_FromString(name.c_str()));
1404+
1405+
PyObject* xarray = detail::get_array(x);
1406+
PyObject* yarray = detail::get_array(y);
1407+
1408+
PyObject* pystring = PyString_FromString(format.c_str());
1409+
1410+
PyObject* plot_args = PyTuple_New(3);
1411+
PyTuple_SetItem(plot_args, 0, xarray);
1412+
PyTuple_SetItem(plot_args, 1, yarray);
1413+
PyTuple_SetItem(plot_args, 2, pystring);
1414+
1415+
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_plot, plot_args, kwargs);
1416+
1417+
Py_DECREF(kwargs);
1418+
Py_DECREF(plot_args);
1419+
if (res) Py_DECREF(res);
1420+
1421+
return res;
1422+
}
1423+
13971424
template<typename Numeric>
13981425
bool named_semilogx(const std::string& name, const std::vector<Numeric>& x, const std::vector<Numeric>& y, const std::string& format = "")
13991426
{

0 commit comments

Comments
 (0)
0