Browse Source

Add figure_size

quangtung97 7 years ago
parent
commit
717e98e752
4 changed files with 27 additions and 3 deletions
  1. 2 0
      README.md
  2. 5 3
      examples/basic.cpp
  3. BIN
      examples/basic.png
  4. 20 0
      matplotlibcpp.h

+ 2 - 0
README.md

@@ -41,6 +41,8 @@ int main()
         z.at(i) = log(i);
     }
 
+    // Set the size of output image = 1200x780 pixels
+    plt::figure_size(1200, 780);
     // Plot line from given x and y data. Color is selected automatically.
     plt::plot(x, y);
     // Plot a red dashed line from given x and y data.

+ 5 - 3
examples/basic.cpp

@@ -15,9 +15,11 @@ int main()
 		y.at(i) = sin(2*M_PI*i/360.0);
 		z.at(i) = log(i);
 	}
-
-	// Plot line from given x and y data. Color is selected automatically.
-	plt::plot(x, y);
+    
+    // Set the size of output image = 1200x780 pixels
+    plt::figure_size(1200, 780);
+    // Plot line from given x and y data. Color is selected automatically.
+    plt::plot(x, y);
 	// Plot a red dashed line from given x and y data.
 	plt::plot(x, w,"r--");
 	// Plot a line whose name will show up as "log(x)" in the legend.

BIN
examples/basic.png


+ 20 - 0
matplotlibcpp.h

@@ -751,6 +751,26 @@ inline void figure()
     Py_DECREF(res);
 }
 
+inline void figure_size(size_t w, size_t h)
+{
+    const size_t dpi = 100;
+    PyObject* size = PyTuple_New(2);
+    PyTuple_SetItem(size, 0, PyFloat_FromDouble((double)w / dpi));
+    PyTuple_SetItem(size, 1, PyFloat_FromDouble((double)h / dpi));
+
+    PyObject* kwargs = PyDict_New();
+    PyDict_SetItemString(kwargs, "figsize", size);
+    PyDict_SetItemString(kwargs, "dpi", PyLong_FromSize_t(dpi));
+
+    PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_figure, 
+            detail::_interpreter::get().s_python_empty_tuple, kwargs);
+
+    Py_DECREF(kwargs);
+
+    if(!res) throw std::runtime_error("Call to figure_size() failed.");
+    Py_DECREF(res);
+}
+
 inline void legend()
 {
     PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_legend, detail::_interpreter::get().s_python_empty_tuple);