Skip to content

Add title function and example of it. #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ int main()

// Set x-axis to interval [0,1000000]
plt::xlim(0, 1000*1000);
// Add graph title
plt::title("sample figure");
// Enable legend.
plt::legend();
// Show plot
// save figure
plt::save("./basic.png");
}
17 changes: 17 additions & 0 deletions matplotlibcpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace matplotlibcpp {
PyObject *s_python_function_legend;
PyObject *s_python_function_xlim;
PyObject *s_python_function_ylim;
PyObject *s_python_function_title;
PyObject *s_python_empty_tuple;

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

s_python_function_save = PyObject_GetAttrString(pylabmod, "savefig");
Expand All @@ -70,6 +72,7 @@ namespace matplotlibcpp {
|| !s_python_function_plot
|| !s_python_function_legend
|| !s_python_function_xlim
|| !s_python_function_title
|| !s_python_function_ylim)
{ throw std::runtime_error("Couldnt find required function!"); }

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

Expand Down Expand Up @@ -244,6 +248,19 @@ namespace matplotlibcpp {
Py_DECREF(res);
}


inline void title(const std::string &titlestr)
{
PyObject* pytitlestr = PyString_FromString(titlestr.c_str());
PyObject* args = PyTuple_New(1);
PyTuple_SetItem(args, 0, pytitlestr);

PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_title, args);
if(!res) throw std::runtime_error("Call to title() failed.");

//if PyDeCRFF, the show function doesn't wook on Mac OS
}

inline void show()
{
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_show, detail::_interpreter::get().s_python_empty_tuple);
Expand Down