Skip to content

Commit cfce4f4

Browse files
committed
subplot2grid implementation and example
1 parent 94c0215 commit cfce4f4

File tree

3 files changed

+79
-4
lines changed

3 files changed

+79
-4
lines changed

Makefile

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
examples: minimal basic modern animation nonblock xkcd quiver bar surface fill_inbetween fill update
1+
examples: minimal basic modern animation nonblock xkcd quiver bar surface fill_inbetween fill update subplot2grid
22

33
minimal: examples/minimal.cpp matplotlibcpp.h
44
cd examples && g++ -DWITHOUT_NUMPY minimal.cpp -I/usr/include/python2.7 -lpython2.7 -o minimal -std=c++11
@@ -32,9 +32,12 @@ fill_inbetween: examples/fill_inbetween.cpp matplotlibcpp.h
3232

3333
fill: examples/fill.cpp matplotlibcpp.h
3434
cd examples && g++ fill.cpp -I/usr/include/python2.7 -lpython2.7 -o fill -std=c++11
35-
35+
3636
update: examples/update.cpp matplotlibcpp.h
3737
cd examples && g++ update.cpp -I/usr/include/python2.7 -lpython2.7 -o update -std=c++11
3838

39+
subplot2grid: examples/subplot2grid.cpp matplotlibcpp.h
40+
cd examples && g++ subplot2grid.cpp -I/usr/include/python2.7 -lpython2.7 -o subplot2grid -std=c++11
41+
3942
clean:
40-
rm -f examples/{minimal,basic,modern,animation,nonblock,xkcd,quiver,bar,surface,fill_inbetween,fill,update}
43+
rm -f examples/{minimal,basic,modern,animation,nonblock,xkcd,quiver,bar,surface,fill_inbetween,fill,update,subplot2grid}

examples/subplot2grid.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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), u(n), v(n), w(n);
13+
for(int i=0; i<n; ++i) {
14+
x.at(i) = i;
15+
u.at(i) = sin(2*M_PI*i/500.0);
16+
v.at(i) = 100.0 / i;
17+
w.at(i) = sin(2*M_PI*i/1000.0);
18+
}
19+
20+
// Set the "super title"
21+
plt::suptitle("My plot");
22+
23+
const long nrows=3, ncols=3;
24+
long row = 2, col = 2;
25+
26+
plt::subplot2grid(nrows, ncols, row, col);
27+
plt::plot(x, w, "g-");
28+
29+
long spanr = 1, spanc = 2;
30+
col = 0;
31+
plt::subplot2grid(nrows, ncols, row, col, spanr, spanc);
32+
plt::plot(x, v, "r-");
33+
34+
spanr = 2, spanc = 3;
35+
row = 0, col = 0;
36+
plt::subplot2grid(nrows, ncols, row, col, spanr, spanc);
37+
plt::plot(x, u, "b-");
38+
// Add some text to the plot
39+
plt::text(100., -0.5, "Hello!");
40+
41+
42+
// Show plots
43+
plt::show();
44+
}

matplotlibcpp.h

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ struct _interpreter {
4747
PyObject *s_python_function_hist;
4848
PyObject *s_python_function_scatter;
4949
PyObject *s_python_function_subplot;
50+
PyObject *s_python_function_subplot2grid;
5051
PyObject *s_python_function_legend;
5152
PyObject *s_python_function_xlim;
5253
PyObject *s_python_function_ion;
@@ -167,6 +168,7 @@ struct _interpreter {
167168
s_python_function_hist = PyObject_GetAttrString(pymod,"hist");
168169
s_python_function_scatter = PyObject_GetAttrString(pymod,"scatter");
169170
s_python_function_subplot = PyObject_GetAttrString(pymod, "subplot");
171+
s_python_function_subplot2grid = PyObject_GetAttrString(pymod, "subplot2grid");
170172
s_python_function_legend = PyObject_GetAttrString(pymod, "legend");
171173
s_python_function_ylim = PyObject_GetAttrString(pymod, "ylim");
172174
s_python_function_title = PyObject_GetAttrString(pymod, "title");
@@ -205,6 +207,7 @@ struct _interpreter {
205207
|| !s_python_function_fill
206208
|| !s_python_function_fill_between
207209
|| !s_python_function_subplot
210+
|| !s_python_function_subplot2grid
208211
|| !s_python_function_legend
209212
|| !s_python_function_ylim
210213
|| !s_python_function_title
@@ -243,6 +246,7 @@ struct _interpreter {
243246
|| !PyFunction_Check(s_python_function_fill)
244247
|| !PyFunction_Check(s_python_function_fill_between)
245248
|| !PyFunction_Check(s_python_function_subplot)
249+
|| !PyFunction_Check(s_python_function_subplot2grid)
246250
|| !PyFunction_Check(s_python_function_legend)
247251
|| !PyFunction_Check(s_python_function_annotate)
248252
|| !PyFunction_Check(s_python_function_ylim)
@@ -1026,7 +1030,6 @@ bool named_loglog(const std::string& name, const std::vector<Numeric>& x, const
10261030
PyTuple_SetItem(plot_args, 0, xarray);
10271031
PyTuple_SetItem(plot_args, 1, yarray);
10281032
PyTuple_SetItem(plot_args, 2, pystring);
1029-
10301033
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_loglog, plot_args, kwargs);
10311034

10321035
Py_DECREF(kwargs);
@@ -1321,6 +1324,31 @@ inline void subplot(long nrows, long ncols, long plot_number)
13211324
Py_DECREF(res);
13221325
}
13231326

1327+
void subplot2grid(long nrows, long ncols, long rowid=0, long colid=0, long rowspan=1, long colspan=1)
1328+
{
1329+
PyObject* shape = PyTuple_New(2);
1330+
PyTuple_SetItem(shape, 0, PyLong_FromLong(nrows));
1331+
PyTuple_SetItem(shape, 1, PyLong_FromLong(ncols));
1332+
1333+
PyObject* loc = PyTuple_New(2);
1334+
PyTuple_SetItem(loc, 0, PyLong_FromLong(rowid));
1335+
PyTuple_SetItem(loc, 1, PyLong_FromLong(colid));
1336+
1337+
PyObject* args = PyTuple_New(4);
1338+
PyTuple_SetItem(args, 0, shape);
1339+
PyTuple_SetItem(args, 1, loc);
1340+
PyTuple_SetItem(args, 2, PyLong_FromLong(rowspan));
1341+
PyTuple_SetItem(args, 3, PyLong_FromLong(colspan));
1342+
1343+
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_subplot2grid, args);
1344+
if(!res) throw std::runtime_error("Call to subplot2grid() failed.");
1345+
1346+
Py_DECREF(shape);
1347+
Py_DECREF(loc);
1348+
Py_DECREF(args);
1349+
Py_DECREF(res);
1350+
}
1351+
13241352
inline void title(const std::string &titlestr, const std::map<std::string, std::string> &keywords = {})
13251353
{
13261354
PyObject* pytitlestr = PyString_FromString(titlestr.c_str());

0 commit comments

Comments
 (0)