Преглед изворни кода

subplot2grid implementation and example

Trevor Keller пре 6 година
родитељ
комит
1ac91f7593
2 измењених фајлова са 76 додато и 1 уклоњено
  1. 44 0
      examples/subplot2grid.cpp
  2. 32 1
      matplotlibcpp.h

+ 44 - 0
examples/subplot2grid.cpp

@@ -0,0 +1,44 @@
+#define _USE_MATH_DEFINES
+#include <cmath>
+#include "../matplotlibcpp.h"
+
+using namespace std;
+namespace plt = matplotlibcpp;
+
+int main()
+{
+    // Prepare data
+	int n = 500;
+	std::vector<double> x(n), u(n), v(n), w(n);
+	for(int i=0; i<n; ++i) {
+		x.at(i) = i;
+		u.at(i) = sin(2*M_PI*i/500.0);
+		v.at(i) = 100.0 / i;
+		w.at(i) = sin(2*M_PI*i/1000.0);
+	}
+
+    // Set the "super title"
+    plt::suptitle("My plot");
+
+    const long nrows=3, ncols=3;
+    long row = 2, col = 2;
+
+    plt::subplot2grid(nrows, ncols, row, col);
+	plt::plot(x, w, "g-");
+
+    long spanr = 1, spanc = 2;
+    col = 0;
+    plt::subplot2grid(nrows, ncols, row, col, spanr, spanc);
+	plt::plot(x, v, "r-");
+
+    spanr = 2, spanc = 3;
+    row = 0, col = 0;
+    plt::subplot2grid(nrows, ncols, row, col, spanr, spanc);
+    plt::plot(x, u, "b-");
+    // Add some text to the plot
+    plt::text(100., -0.5, "Hello!");
+
+
+    // Show plots
+	plt::show();
+}

+ 32 - 1
matplotlibcpp.h

@@ -61,6 +61,7 @@ struct _interpreter {
     PyObject *s_python_function_imshow;
     PyObject *s_python_function_scatter;
     PyObject *s_python_function_subplot;
+    PyObject *s_python_function_subplot2grid;
     PyObject *s_python_function_legend;
     PyObject *s_python_function_xlim;
     PyObject *s_python_function_ion;
@@ -193,6 +194,7 @@ private:
         s_python_function_hist = safe_import(pymod,"hist");
         s_python_function_scatter = safe_import(pymod,"scatter");
         s_python_function_subplot = safe_import(pymod, "subplot");
+        s_python_function_subplot2grid = safe_import(pymod, "subplot2grid");
         s_python_function_legend = safe_import(pymod, "legend");
         s_python_function_ylim = safe_import(pymod, "ylim");
         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
     return res;
 }
 
+// TODO - it should be possible to make this work by implementing
+// a non-numpy alternative for `get_2darray()`.
+#ifndef WITHOUT_NUMPY
 template <typename Numeric>
 void plot_surface(const std::vector<::std::vector<Numeric>> &x,
                   const std::vector<::std::vector<Numeric>> &y,
@@ -453,6 +458,8 @@ void plot_surface(const std::vector<::std::vector<Numeric>> &x,
   Py_DECREF(kwargs);
   if (res) Py_DECREF(res);
 }
+#endif // WITHOUT_NUMPY
+
 
 template<typename Numeric>
 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
     PyTuple_SetItem(plot_args, 0, xarray);
     PyTuple_SetItem(plot_args, 1, yarray);
     PyTuple_SetItem(plot_args, 2, pystring);
-
     PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_loglog, plot_args, kwargs);
 
     Py_DECREF(kwargs);
@@ -1379,6 +1385,31 @@ inline void subplot(long nrows, long ncols, long plot_number)
     Py_DECREF(res);
 }
 
+void subplot2grid(long nrows, long ncols, long rowid=0, long colid=0, long rowspan=1, long colspan=1)
+{
+    PyObject* shape = PyTuple_New(2);
+    PyTuple_SetItem(shape, 0, PyLong_FromLong(nrows));
+    PyTuple_SetItem(shape, 1, PyLong_FromLong(ncols));
+
+    PyObject* loc = PyTuple_New(2);
+    PyTuple_SetItem(loc, 0, PyLong_FromLong(rowid));
+    PyTuple_SetItem(loc, 1, PyLong_FromLong(colid));
+
+    PyObject* args = PyTuple_New(4);
+    PyTuple_SetItem(args, 0, shape);
+    PyTuple_SetItem(args, 1, loc);
+    PyTuple_SetItem(args, 2, PyLong_FromLong(rowspan));
+    PyTuple_SetItem(args, 3, PyLong_FromLong(colspan));
+
+    PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_subplot2grid, args);
+    if(!res) throw std::runtime_error("Call to subplot2grid() failed.");
+
+    Py_DECREF(shape);
+    Py_DECREF(loc);
+    Py_DECREF(args);
+    Py_DECREF(res);
+}
+
 inline void title(const std::string &titlestr, const std::map<std::string, std::string> &keywords = {})
 {
     PyObject* pytitlestr = PyString_FromString(titlestr.c_str());