8000 call to loglog directly instead of redirection to plot · Mgomez-01/matplotlib-cpp@180d6d1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 180d6d1

Browse files
committed
call to loglog directly instead of redirection to plot
1 parent 9a3d1ef commit 180d6d1

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

matplotlibcpp.h

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -501,33 +501,33 @@ bool plot(const VectorY &y,
501501
template <typename VectorX, typename VectorY>
502502
bool loglog(const VectorX &x, const VectorY &y, const std::string &s = "",
503503
const std::map<std::string, std::string> &keywords = {}) {
504-
detail::_interpreter::get();
504+
assert(x.size() == y.size());
505505

506-
// argument for xscale/yscale is only the string "log"
507-
PyObject *log_arg = PyTuple_New(1);
508-
PyObject *pystring = PyString_FromString("log");
509-
PyTuple_SetItem(log_arg, 0, pystring);
506+
PyObject *xarray = get_array(x);
507+
PyObject *yarray = get_array(y);
510508

511-
// call xscale("log") and yscale("log"), no kwargs needed hence pass NULL,
512-
// as explained in https://docs.python.org/3/c-api/object.html
513-
PyObject *res_x = PyObject_Call(
514-
detail::_interpreter::get().s_python_function_xscale, log_arg, NULL);
515-
PyObject *res_y = PyObject_Call(
516-
detail::_interpreter::get().s_python_function_yscale, log_arg, NULL);
509+
PyObject *pystring = PyString_FromString(s.c_str());
517510

518-
// clean up
519-
Py_DECREF(log_arg);
511+
PyObject *plot_args = PyTuple_New(3);
512+
PyTuple_SetItem(plot_args, 0, xarray);
513+
PyTuple_SetItem(plot_args, 1, yarray);
514+
PyTuple_SetItem(plot_args, 2, pystring);
515+
516+
PyObject *kwargs = PyDict_New();
517+
for (auto const &item : keywords) {
518+
PyDict_SetItemString(kwargs, item.first.c_str(),
519+
PyString_FromString(item.second.c_str()));
520+
}
520521

521-
if (!res_x)
522-
throw std::runtime_error("Call to xscale() failed");
523-
Py_DECREF(res_x);
522+
PyObject *res = PyObject_Call(
523+
detail::_interpreter::get().s_python_function_loglog, plot_args, kwargs);
524524

525-
if (!res_y)
526-
throw std::runtime_error("Call to yscale() failed");
527-
Py_DECREF(res_y);
525+
Py_DECREF(plot_args);
526+
Py_DECREF(kwargs);
527+
if (res)
528+
Py_DECREF(res);
528529

529-
// call plot, which gets now plotted in doubly logarithmic scale
530-
return plot(x, y, s, keywords);
530+
return res;
531531
}
532532

533533
template <typename VectorX, typename VectorY>

0 commit comments

Comments
 (0)
0