8000 add loc and bbox_to_anchor support for legend · Mgomez-01/matplotlib-cpp@c5a2b1d · GitHub
[go: up one dir, main page]

Skip to content

Commit c5a2b1d

Browse files
committed
add loc and bbox_to_anchor support for legend
1 parent 377ac84 commit c5a2b1d

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

matplotlibcpp.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,12 +1161,25 @@ inline void figure_size(size_t w, size_t h) {
11611161
Py_DECREF(res);
11621162
}
11631163

1164-
inline void legend() {
1164+
template <typename Vector = std::vector<double>>
1165+
inline void legend(const std::string &loc = "best",
1166+
const Vector &bbox_to_anchor = Vector()) {
11651167
detail::_interpreter::get();
11661168

1169+
PyObject *kwargs = PyDict_New();
1170+
PyDict_SetItemString(kwargs, "loc", PyString_FromString(loc.c_str()));
1171+
1172+
if (bbox_to_anchor.size() == 2 || bbox_to_anchor.size() == 4) {
1173+
PyObject *bbox = get_array(bbox_to_anchor);
1174+
PyDict_SetItemString(kwargs, "bbox_to_anchor", bbox);
1175+
}
1176+
11671177
PyObject *res =
1168-
PyObject_CallObject(detail::_interpreter::get().s_python_function_legend,
1169-
detail::_interpreter::get().s_python_empty_tuple);
1178+
PyObject_Call(detail::_interpreter::get().s_python_function_legend,
1179+
detail::_interpreter::get().s_python_empty_tuple, kwargs);
1180+
1181+
Py_DECREF(kwargs);
1182+
11701183
if (!res)
11711184
throw std::runtime_error("Call to legend() failed.");
11721185

0 commit comments

Comments
 (0)
0