GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — legend(labels, keywords) overload · lava/matplotlib-cpp@78fa91b

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • examples
    • basic.cpp
  • matplotlibcpp.h
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎examples/basic.cpp‎

Copy file name to clipboardExpand all lines: examples/basic.cpp
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ int main()
3434
// Add graph title
3535
plt::title("Sample figure");
3636

37-
// Enable legend.
38-
plt::legend();
37+
// Enable legend, with explicit labels for the three lines.
38+
plt::legend(std::vector<std::string>{"sin(x)", "2", "log(x)"});
3939

4040
// save figure
4141
const char* filename = "./basic.png";
Collapse file

‎matplotlibcpp.h‎

Copy file name to clipboardExpand all lines: matplotlibcpp.h
+32Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,6 +1980,38 @@ inline void legend(const std::map<std::string, std::string>& keywords)
19801980
Py_DECREF(res);
19811981
}
19821982

1983+
template<typename StringType>
1984+
inline void legend(const std::vector<StringType>& labels, const std::map<std::string, std::string>& keywords = {})
1985+
{
1986+
detail::_interpreter::get();
1987+
1988+
PyObject* llist = PyList_New(labels.size());
1989+
if(!llist) throw std::runtime_error("Couldn't allocate labels list in legend().");
1990+
1991+
for(size_t i = 0; i < labels.size(); ++i) {
1992+
PyList_SetItem(llist, i, PyString_FromString(std::string(labels[i]).c_str()));
1993+
}
1994+
1995+
PyObject* args = PyTuple_New(1);
1996+
PyTuple_SetItem(args, 0, llist);
1997+
1998+
// construct keyword args
1999+
PyObject* kwargs = PyDict_New();
2000+
for(std::map<std::string, std::string>::const_iterator it = keywords.begin(); it != keywords.end(); ++it)
2001+
{
2002+
PyDict_SetItemString(kwargs, it->first.c_str(), PyString_FromString(it->second.c_str()));
2003+
}
2004+
2005+
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_legend, args, kwargs);
2006+
2007+
Py_DECREF(args);
2008+
Py_DECREF(kwargs);
2009+
2010+
if(!res) throw std::runtime_error("Call to legend() failed.");
2011+
2012+
Py_DECREF(res);
2013+
}
2014+
19832015
template<typename Numeric>
19842016
inline void set_aspect(Numeric ratio)
19852017
{

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.