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

Skip to content

Commit 1ac91f7

Browse files
tkphdBenno Evers
authored and
Benno Evers
committed
subplot2grid implementation and example
1 parent 6ec72da commit 1ac91f7

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

examples/subplot2grid.cpp

+44
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

+32-1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ struct _interpreter {
6161
PyObject *s_python_function_imshow;
6262
PyObject *s_python_function_scatter;
6363
PyObject *s_python_function_subplot;
64+
PyObject *s_python_function_subplot2grid;
6465
PyObject *s_python_function_legend;
6566
PyObject *s_python_function_xlim;
6667
PyObject *s_python_function_ion;
@@ -193,6 +194,7 @@ struct _interpreter {
193194
s_python_function_hist = safe_import(pymod,"hist");
194195
s_python_function_scatter = safe_import(pymod,"scatter");
195196
s_python_function_subplot = safe_import(pymod, "subplot");
197+
s_python_function_subplot2grid = safe_import(pymod, "subplot2grid");
196198
s_python_function_legend = safe_import(pymod, "legend");
197199
s_python_function_ylim = safe_import(pymod, "ylim");
198200
s_python_function_title = safe_import(pymod, "title");
@@ -362,6 +364,9 @@ bool plot(const std::vector<Numeric> &x, const std::vector<Numeric> &y, const st
362364
return res;
363365
}
364366

367+
// TODO - it should be possible to make this work by implementing
368+
// a non-numpy alternative for `get_2darray()`.
369+
#ifndef WITHOUT_NUMPY
365370
template <typename Numeric>
366371
void plot_surface(const std::vector<::std::vector<Numeric>> &x,
367372
const std::vector<::std::vector<Numeric>> &y,
@@ -453,6 +458,8 @@ void plot_surface(const std::vector<::std::vector<Numeric>> &x,
453458
Py_DECREF(kwargs);
454459
if (res) Py_DECREF(res);
455460
}
461+
#endif // WITHOUT_NUMPY
462+
456463

457464
template<typename Numeric>
458465
bool stem(const std::vector<Numeric> &x, const std::vector<Numeric> &y, const std::map<std::string, std::string>& keywords)
@@ -1073,7 +1080,6 @@ bool named_loglog(const std::string& name, const std::vector<Numeric>& x, const
10731080
PyTuple_SetItem(plot_args, 0, xarray);
10741081
PyTuple_SetItem(plot_args, 1, yarray);
10751082
PyTuple_SetItem(plot_args, 2, pystring);
1076-
10771083
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_loglog, plot_args, kwargs);
10781084

10791085
Py_DECREF(kwargs);
@@ -1379,6 +1385,31 @@ inline void subplot(long nrows, long ncols, long plot_number)
13791385
Py_DECREF(res);
13801386
}
13811387

1388+
void subplot2grid(long nrows, long ncols, long rowid=0, long colid=0, long rowspan=1, long colspan=1)
1389+
{
1390+
PyObject* shape = PyTuple_New(2);
1391+
PyTuple_SetItem(shape, 0, PyLong_FromLong(nrows));
1392+
PyTuple_SetItem(shape, 1, PyLong_FromLong(ncols));
1393+
1394+
PyObject* loc = PyTuple_New(2);
1395+
PyTuple_SetItem(loc, 0, PyLong_FromLong(rowid));
1396+
PyTuple_SetItem(loc, 1, PyLong_FromLong(colid));
1397+
1398+
PyObject* args = PyTuple_New(4);
1399+
PyTuple_SetItem(args, 0, shape);
1400+
PyTuple_SetItem(args, 1, loc);
1401+
PyTuple_SetItem(args, 2, PyLong_FromLong(rowspan));
1402+
PyTuple_SetItem(args, 3, PyLong_FromLong(colspan));
1403+
1404+
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_subplot2grid, args);
1405+
if(!res) throw std::runtime_error("Call to subplot2grid() failed.");
1406+
1407+
Py_DECREF(shape);
1408+
Py_DECREF(loc);
1409+
Py_DECREF(args);
1410+
Py_DECREF(res);
1411+
}
1412+
13821413
inline void title(const std::string &titlestr, const std::map<std::string, std::string> &keywords = {})
13831414
{
13841415
PyObject* pytitlestr = PyString_FromString(titlestr.c_str());

0 commit comments

Comments
 (0)