Reputation: 3072
i use matplotlib from c++ for (debug-)plotting. I want to plot several plots in the same graph. So far i have
template<typename NumericX, typename NumericY>
bool plot(const NumericX const* x, const NumericY const* y, std::size_t size, const std::string& s = "")
{
PyObject* xlist = PyList_New(size);
PyObject* ylist = PyList_New(size);
PyObject* pystring = PyUnicode_FromString(s.c_str());
for(size_t i = 0; i < size; ++i) {
PyList_SetItem(xlist, i, PyFloat_FromDouble(x[i]));
PyList_SetItem(ylist, i, PyFloat_FromDouble(y[i]));
}
PyObject* plot_args = PyTuple_New(3);
PyTuple_SetItem(plot_args, 0, xlist);
PyTuple_SetItem(plot_args, 1, ylist);
PyTuple_SetItem(plot_args, 2, pystring);
PyObject* pymod = PyImport_Import(pyplotname);
PyObject* s_python_function_plot = PyObject_GetAttrString(pymod, "plot");
PyObject* res = PyObject_CallObject(s_python_function_plot, plot_args);
Py_DECREF(xlist);
Py_DECREF(ylist);
Py_DECREF(plot_args);
if(res) Py_DECREF(res);
return res;
}
but calling
plot(x_0,_y_0, size_0);
plot(x_1,_y_1, size_1);
show(); // just calling matplotlib.show
shows only the first plot. What can i do, to get all the plots?
Upvotes: 2
Views: 1454
Reputation: 387
I know it has been a long time since you posted the question and the matplotlib implementation may have improved but still, I have faced your same problem so here is how I coped with it:
say your data are (x0,_y_0) and (x_1,_y_1) (let's forget about size_0 and size_1 for the moment).
Just call subplot()
before any plot()
:
#include <matplotlibcpp.h>
namespace plt = matplotlibcpp;
plt::subplot(2, 1, 1); // 2 rows, 1 column, first plot
plt::plot(x_0,_y_0);
plt::subplot(2, 1, 2); // 2 rows, 1 column, second plot
plt::plot(x_1,_y_1);
plt::show();
Using just plot()
typically displays both traces overlapped in the same graph, like in Python.
Hope this can help.
Upvotes: 2