8000 Added text and suptitle · OhHandsome/matplotlib-cpp@b60d338 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit b60d338

Browse files
charasynlava
authored andcommitted
Added text and suptitle
1 parent 3d555d2 commit b60d338

File tree

2 files changed

+71
-3
lines changed

2 files changed

+71
-3
lines changed

examples/subplot.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#define _USE_MATH_DEFINES
2+
#include <cmath>
3+
#include "../matplotlibcpp.h"
4+
5+
using namespace std;
6+
namespace plt = matplotlibcpp;
7+
8+
int main()
9+
{
10+
// Prepare data
11+
int n = 500;
12+
std::vector<double> x(n), y(n), z(n), w(n,2);
13+
for(int i=0; i<n; ++i) {
14+
x.at(i) = i;
15+
y.at(i) = sin(2*M_PI*i/360.0);
16+
z.at(i) = 100.0 / i;
17+
}
18+
19+
// Set the "super title"
20+
plt::suptitle("My plot");
21+
plt::subplot(1, 2, 1);
22+
plt::plot(x, y, "r-");
23+
plt::subplot(1, 2, 2);
24+
plt::plot(x, z, "k-");
25+
// Add some text to the plot
26+
plt::text(100, 90, "Hello!");
27+
28+
29+
// Show plots
30+
plt::show();
31+
}

matplotlibcpp.h

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ struct _interpreter {
6262
PyObject *s_python_empty_tuple;
6363
PyObject *s_python_function_stem;
6464
PyObject *s_python_function_xkcd;
65+
PyObject *s_python_function_text;
66+
PyObject *s_python_function_suptitle;
6567

6668
/* For now, _interpreter is implemented as a singleton since its currently not possible to have
6769
multiple independent embedded python interpreters without patching the python source code
@@ -165,6 +167,8 @@ struct _interpreter {
165167
s_python_function_tight_layout = PyObject_GetAttrString(pymod, "tight_layout");
166168
s_python_function_stem = PyObject_GetAttrString(pymod, "st 8000 em");
167169
s_python_function_xkcd = PyObject_GetAttrString(pymod, "xkcd");
170+
s_python_function_text = PyObject_GetAttrString(pymod, "text");
171+
s_python_function_suptitle = PyObject_GetAttrString(pymod, "suptitle");
168172

169173
if( !s_python_function_show
170174
|| !s_python_function_close
@@ -195,6 +199,8 @@ struct _interpreter {
195199
|| !s_python_function_tight_layout
196200
|| !s_python_function_stem
197201
|| !s_python_function_xkcd
202+
|| !s_python_function_text
203+
|| !s_python_function_suptitle
198204
) { throw std::runtime_error("Couldn't find required function!"); }
199205

200206
if ( !PyFunction_Check(s_python_function_show)
@@ -225,6 +231,8 @@ struct _interpreter {
225231
|| !PyFunction_Check(s_python_function_errorbar)
226232
|| !PyFunction_Check(s_python_function_stem)
227233
|| !PyFunction_Check(s_python_function_xkcd)
234+
|| !PyFunction_Check(s_python_function_text)
235+
|| !PyFunction_Check(s_python_function_suptitle)
228236
) { throw std::runtime_error 8000 ("Python object is unexpectedly not a PyFunction."); }
229237

230238
s_python_empty_tuple = PyTuple_New(0);
@@ -788,6 +796,22 @@ bool stem(const std::vector<Numeric>& y, const std::string& format = "")
788796
return stem(x, y, format);
789797
}
790798

799+
template<typename Numeric>
800+
void text(Numeric x, Numeric y, const std::string& format = "")
801+
{
802+
PyObject* args = PyTuple_New(3);
803+
PyTuple_SetItem(args, 0, PyFloat_FromDouble(x));
804+
PyTuple_SetItem(args, 1, PyFloat_FromDouble(y));
805+
PyTuple_SetItem(args, 2, PyString_FromString(format.c_str()));
806+
807+
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_text, args);
808+
if(!res) throw std::runtime_error("Call to text() failed.");
809+
810+
Py_DECREF(args);
811+
Py_DECREF(res);
812+
}
813+
814+
791815
inline void figure()
792816
{
793817
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_figure, detail::_interpreter::get().s_python_empty_tuple);
@@ -807,7 +831,7 @@ inline void figure_size(size_t w, size_t h)
807831
PyDict_SetItemString(kwargs, "figsize", size);
808832
PyDict_SetItemString(kwargs, "dpi", PyLong_FromSize_t(dpi));
809833

810-
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_figure,
834+
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_figure,
811835
detail::_interpreter::get().s_python_empty_tuple, kwargs);
812836

813837
Py_DECREF(kwargs);
@@ -931,7 +955,7 @@ inline void xticks(const std::vector<Numeric> &ticks, const std::vector<std::str
931955
Py_DECREF(args);
932956
Py_DECREF(kwargs);
933957
if(!res) throw std::runtime_error("Call to xticks() failed");
934-
958+
935959
Py_DECREF(res);
936960
}
937961

@@ -978,7 +1002,7 @@ inline void yticks(const std::vector<Numeric> &ticks, const std::vector<std::str
9781002
Py_DECREF(args);
9791003
Py_DECREF(kwargs);
9801004
if(!res) throw std::runtime_error("Call to yticks() failed");
981-
1005+
9821006
Py_DECREF(res);
9831007
}
9841008

@@ -1016,6 +1040,19 @@ inline void title(const std::string &titlestr)
10161040
Py_DECREF(res);
10171041
}
10181042

1043+
inline void suptitle(const std::string &suptitlestr)
1044+
{
1045+
PyObject* pysuptitlestr = PyString_FromString(suptitlestr.c_str());
1046+
PyObject* args = PyTuple_New(1);
1047+
PyTuple_SetItem(args, 0, pysuptitlestr);
1048+
1049+
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_suptitle, args);
1050+
if(!res) throw std::runtime_error("Call to suptitle() failed.");
1051+
1052+
Py_DECREF(args);
1053+
Py_DECREF(res);
1054+
}
1055+
10191056
inline void axis(const std::string &axisstr)
10201057
{
10211058
PyObject* str = PyString_FromString(axisstr.c_str());

0 commit comments

Comments
 (0)
0