Skip to content

Commit f5b73c0

Browse files
committed
Merge pull request lava#3 from AtsushiSakai/master
Add title function and example of it.
2 parents 0fba0d4 + 335ffa6 commit f5b73c0

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

examples/basic.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ int main()
2424

2525
// Set x-axis to interval [0,1000000]
2626
plt::xlim(0, 1000*1000);
27+
// Add graph title
28+
plt::title("sample figure");
2729
// Enable legend.
2830
plt::legend();
29-
// Show plot
31+
// save figure
3032
plt::save("./basic.png");
3133
}

matplotlibcpp.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ namespace matplotlibcpp {
2323
PyObject *s_python_function_legend;
2424
PyObject *s_python_function_xlim;
2525
PyObject *s_python_function_ylim;
26+
PyObject *s_python_function_title;
2627
PyObject *s_python_empty_tuple;
2728

2829
/* For now, _interpreter is implemented as a singleton since its currently not possible to have
@@ -60,6 +61,7 @@ namespace matplotlibcpp {
6061
s_python_function_plot = PyObject_GetAttrString(pymod, "plot");
6162
s_python_function_legend = PyObject_GetAttrString(pymod, "legend");
6263
s_python_function_ylim = PyObject_GetAttrString(pymod, "ylim");
64+
s_python_function_title = PyObject_GetAttrString(pymod, "title");
6365
s_python_function_xlim = PyObject_GetAttrString(pymod, "xlim");
6466

6567
s_python_function_save = PyObject_GetAttrString(pylabmod, "savefig");
@@ -70,6 +72,7 @@ namespace matplotlibcpp {
7072
|| !s_python_function_plot
7173
|| !s_python_function_legend
7274
|| !s_python_function_xlim
75+
|| !s_python_function_title
7376
|| !s_python_function_ylim)
7477
{ throw std::runtime_error("Couldnt find required function!"); }
7578

@@ -79,6 +82,7 @@ namespace matplotlibcpp {
7982
|| !PyFunction_Check(s_python_function_plot)
8083
|| !PyFunction_Check(s_python_function_legend)
8184
|| !PyFunction_Check(s_python_function_xlim)
85+
|| !PyFunction_Check(s_python_function_title)
8286
|| !PyFunction_Check(s_python_function_ylim))
8387
{ throw std::runtime_error("Python object is unexpectedly not a PyFunction."); }
8488

@@ -244,6 +248,19 @@ namespace matplotlibcpp {
244248
Py_DECREF(res);
245249
}
246250

251+
252+
inline void title(const std::string &titlestr)
253+
{
254+
PyObject* pytitlestr = PyString_FromString(titlestr.c_str());
255+
PyObject* args = PyTuple_New(1);
256+
PyTuple_SetItem(args, 0, pytitlestr);
257+
258+
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_title, args);
259+
if(!res) throw std::runtime_error("Call to title() failed.");
260+
261+
//if PyDeCRFF, the show function doesn't wook on Mac OS
262+
}
263+
247264
inline void show()
248265
{
249266
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_show, detail::_interpreter::get().s_python_empty_tuple);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy