Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bfa2300

Browse files
author
Martino
committed
Added annotate() method
1 parent d430c69 commit bfa2300

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

matplotlibcpp.h

+31-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespace matplotlibcpp {
3131
PyObject *s_python_function_ylabel;
3232
PyObject *s_python_function_grid;
3333
PyObject *s_python_empty_tuple;
34+
PyObject *s_python_function_annotate;
3435

3536
/* For now, _interpreter is implemented as a singleton since its currently not possible to have
3637
multiple independent embedded python interpreters without patching the python source code
@@ -75,12 +76,13 @@ namespace matplotlibcpp {
7576
s_python_function_grid = PyObject_GetAttrString(pymod, "grid");
7677
s_python_function_xlim = PyObject_GetAttrString(pymod, "xlim");
7778
s_python_function_save = PyObject_GetAttrString(pylabmod, "savefig");
79+
s_python_function_annotate = PyObject_GetAttrString(pymod,"annotate");
7880

7981
if( !s_python_function_show
8082
|| !s_python_function_figure
8183
|| !s_python_function_plot
8284
|| !s_python_function_subplot
83-
|| !s_python_function_legend
85+
|| !s_python_function_legend
8486
|| !s_python_function_ylim
8587
|| !s_python_function_title
8688
|| !s_python_function_axis
@@ -89,14 +91,16 @@ namespace matplotlibcpp {
8991
|| !s_python_function_grid
9092
|| !s_python_function_xlim
9193
|| !s_python_function_save
94+
|| !s_python_function_annotate
9295
)
9396
{ throw std::runtime_error("Couldn't find required function!"); }
9497

9598
if( !PyFunction_Check(s_python_function_show)
9699
|| !PyFunction_Check(s_python_function_figure)
97100
|| !PyFunction_Check(s_python_function_plot)
98101
|| !PyFunction_Check(s_python_function_subplot)
99-
|| !PyFunction_Check(s_python_function_legend)
102+
|| !PyFunction_Check(s_python_function_legend)
103+
|| !PyFunction_Check(s_python_function_annotate)
100104
|| !PyFunction_Check(s_python_function_ylim)
101105
|| !PyFunction_Check(s_python_function_title)
102106
|| !PyFunction_Check(s_python_function_axis)
@@ -116,6 +120,31 @@ namespace matplotlibcpp {
116120
}
117121
};
118122
}
123+
124+
bool annotate(std::string annotation, double x, double y)
125+
{
126+
PyObject * xy = PyTuple_New(2);
127+
PyObject * str = PyString_FromString(annotation.c_str());
128+
129+
PyTuple_SetItem(xy,0,PyFloat_FromDouble(x));
130+
PyTuple_SetItem(xy,1,PyFloat_FromDouble(y));
131+
132+
PyObject* kwargs = PyDict_New();
133+
PyDict_SetItemString(kwargs, "xy", xy);
134+
135+
PyObject* args = PyTuple_New(1);
136+
PyTuple_SetItem(args, 0, str);
137+
138+
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_annotate, args, kwargs);
139+
140+
Py_DECREF(xy);
141+
Py_DECREF(args);
142+
Py_DECREF(kwargs);
143+
144+
if(res) Py_DECREF(res);
145+
146+
return res;
147+
}
119148

120149
template<typename Numeric>
121150
bool plot(const std::vector<Numeric> &x, const std::vector<Numeric> &y, const std::map<std::string, std::string>& keywords)

0 commit comments

Comments
 (0)