|
37 | 37 | #if PY_MAJOR_VERSION >= 3
|
38 | 38 | #define PyString_FromString PyUnicode_FromString
|
39 | 39 | #define PyInt_FromLong PyLong_FromLong
|
40 |
| -#define PyString_FromString PyUnicode_FromString |
41 | 40 | #endif
|
42 | 41 |
|
43 | 42 | #define CPP20 202002L
|
@@ -2424,6 +2423,46 @@ namespace matplotlibcpp
|
2424 | 2423 | Py_DECREF(res);
|
2425 | 2424 | }
|
2426 | 2425 |
|
| 2426 | + template<std::ranges::contiguous_range Labels> |
| 2427 | + void legend(const Labels& labels, const std::map<std::string, std::string>& keywords={}) |
| 2428 | + { |
| 2429 | + detail::_interpreter::get(); |
| 2430 | + |
| 2431 | + PyObject* llist = PyList_New(labels.size()); |
| 2432 | + if(llist == nullptr) |
| 2433 | + throw "Can't allocate labels list in legend() function."; |
| 2434 | + |
| 2435 | + // iterate over labels, hopefully not too many! |
| 2436 | + size_t i=0; |
| 2437 | + for (const auto& l : labels) { |
| 2438 | + PyObject* str = PyString_FromString(l.c_str()); |
| 2439 | + PyList_SET_ITEM(llist, i++, str); |
| 2440 | + } |
| 2441 | + |
| 2442 | + // construct keyword args |
| 2443 | + PyObject* kwargs = PyDict_New(); |
| 2444 | + for(std::map<std::string, std::string>::const_iterator it |
| 2445 | + = keywords.begin(); |
| 2446 | + it != keywords.end(); ++it) { |
| 2447 | + PyDict_SetItemString(kwargs, it->first.c_str(), |
| 2448 | + PyString_FromString(it->second.c_str())); |
| 2449 | + } |
| 2450 | + |
| 2451 | + PyObject* args = PyTuple_New(1); |
| 2452 | + PyTuple_SetItem(args, 0, llist); |
| 2453 | + |
| 2454 | + PyObject* res = PyObject_Call( |
| 2455 | + detail::_interpreter::get().s_python_function_legend, |
| 2456 | + args, kwargs); |
| 2457 | + // PyObject* res = PyObject_Call( |
| 2458 | + // detail::_interpreter::get().s_python_function_legend, |
| 2459 | + // detail::_interpreter::get().s_python_empty_tuple, kwargs); |
| 2460 | + if(!res) throw std::runtime_error("Call to legend() failed."); |
| 2461 | + |
| 2462 | + Py_DECREF(kwargs); |
| 2463 | + Py_DECREF(res); |
| 2464 | + } |
| 2465 | + |
2427 | 2466 | template<typename Numeric>
|
2428 | 2467 | void set_aspect(Numeric ratio)
|
2429 | 2468 | {
|
|
0 commit comments