Skip to content

Commit 27ce78e

Browse files
AtsushiSakaiBenno Evers
authored andcommitted
Add xlabel, ylabel, axis and grid functions
1 parent f5b73c0 commit 27ce78e

File tree

2 files changed

+77
-5
lines changed

2 files changed

+77
-5
lines changed

examples/basic.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ 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");
27+
28+
// Add graph title
29+
plt::title("Sample figure");
2930
// Enable legend.
3031
plt::legend();
3132
// save figure

matplotlibcpp.h

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ namespace matplotlibcpp {
2424
PyObject *s_python_function_xlim;
2525
PyObject *s_python_function_ylim;
2626
PyObject *s_python_function_title;
27+
PyObject *s_python_function_axis;
28+
PyObject *s_python_function_xlabel;
29+
PyObject *s_python_function_ylabel;
30+
PyObject *s_python_function_grid;
2731
PyObject *s_python_empty_tuple;
2832

2933
/* For now, _interpreter is implemented as a singleton since its currently not possible to have
@@ -62,6 +66,10 @@ namespace matplotlibcpp {
6266
s_python_function_legend = PyObject_GetAttrString(pymod, "legend");
6367
s_python_function_ylim = PyObject_GetAttrString(pymod, "ylim");
6468
s_python_function_title = PyObject_GetAttrString(pymod, "title");
69+
s_python_function_axis = PyObject_GetAttrString(pymod, "axis");
70+
s_python_function_xlabel = PyObject_GetAttrString(pymod, "xlabel");
71+
s_python_function_ylabel = PyObject_GetAttrString(pymod, "ylabel");
72+
s_python_function_grid = PyObject_GetAttrString(pymod, "grid");
6573
s_python_function_xlim = PyObject_GetAttrString(pymod, "xlim");
6674

6775
s_python_function_save = PyObject_GetAttrString(pylabmod, "savefig");
@@ -72,8 +80,13 @@ namespace matplotlibcpp {
7280
|| !s_python_function_plot
7381
|| !s_python_function_legend
7482
|| !s_python_function_xlim
83+
|| !s_python_function_ylim
7584
|| !s_python_function_title
76-
|| !s_python_function_ylim)
85+
|| !s_python_function_axis
86+
|| !s_python_function_xlabel
87+
|| !s_python_function_ylabel
88+
|| !s_python_function_grid
89+
)
7790
{ throw std::runtime_error("Couldnt find required function!"); }
7891

7992
if(!PyFunction_Check(s_python_function_show)
@@ -82,8 +95,13 @@ namespace matplotlibcpp {
8295
|| !PyFunction_Check(s_python_function_plot)
8396
|| !PyFunction_Check(s_python_function_legend)
8497
|| !PyFunction_Check(s_python_function_xlim)
98+
|| !PyFunction_Check(s_python_function_ylim)
8599
|| !PyFunction_Check(s_python_function_title)
86-
|| !PyFunction_Check(s_python_function_ylim))
100+
|| !PyFunction_Check(s_python_function_axis)
101+
|| !PyFunction_Check(s_python_function_xlabel)
102+
|| !PyFunction_Check(s_python_function_ylabel)
103+
|| !PyFunction_Check(s_python_function_grid)
104+
)
87105
{ throw std::runtime_error("Python object is unexpectedly not a PyFunction."); }
88106

89107
s_python_empty_tuple = PyTuple_New(0);
@@ -258,9 +276,62 @@ namespace matplotlibcpp {
258276
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_title, args);
259277
if(!res) throw std::runtime_error("Call to title() failed.");
260278

261-
//if PyDeCRFF, the show function doesn't wook on Mac OS
279+
// if PyDeCRFF, the function doesn't work on Mac OS
262280
}
263281

282+
inline void axis(const std::string &axisstr)
283+
{
284+
PyObject* str = PyString_FromString(axisstr.c_str());
285+
PyObject* args = PyTuple_New(1);
286+
PyTuple_SetItem(args, 0, str);
287+
288+
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_axis, args);
289+
if(!res) throw std::runtime_error("Call to title() failed.");
290+
291+
// if PyDeCRFF, the function doesn't work on Mac OS
292+
}
293+
294+
295+
296+
inline void xlabel(const std::string &str)
297+
{
298+
PyObject* pystr = PyString_FromString(str.c_str());
299+
PyObject* args = PyTuple_New(1);
300+
PyTuple_SetItem(args, 0, pystr);
301+
302+
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_xlabel, args);
303+
if(!res) throw std::runtime_error("Call to xlabel() failed.");
304+
305+
// if PyDeCRFF, the function doesn't work on Mac OS
306+
}
307+
308+
inline void ylabel(const std::string &str)
309+
{
310+
PyObject* pystr = PyString_FromString(str.c_str());
311+
PyObject* args = PyTuple_New(1);
312+
PyTuple_SetItem(args, 0, pystr);
313+
314+
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_ylabel, args);
315+
if(!res) throw std::runtime_error("Call to ylabel() failed.");
316+
317+
// if PyDeCRFF, the function doesn't work on Mac OS
318+
}
319+
320+
inline void grid(bool flag)
321+
{
322+
PyObject* pyflag = flag ? Py_True : Py_False;
323+
324+
PyObject* args = PyTuple_New(1);
325+
PyTuple_SetItem(args, 0, pyflag);
326+
327+
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_grid, args);
328+
if(!res) throw std::runtime_error("Call to grid() failed.");
329+
330+
// if PyDeCRFF, the function doesn't work on Mac OS
331+
}
332+
333+
334+
264335
inline void show()
265336
{
266337
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