Skip to content

Commit 717e98e

Browse files
QuangTung97lava
authored andcommitted
Add figure_size
1 parent 1d23b28 commit 717e98e

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ int main()
4141
z.at(i) = log(i);
4242
}
4343
44+
// Set the size of output image = 1200x780 pixels
45+
plt::figure_size(1200, 780);
4446
// Plot line from given x and y data. Color is selected automatically.
4547
plt::plot(x, y);
4648
// Plot a red dashed line from given x and y data.

examples/basic.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ int main()
1515
y.at(i) = sin(2*M_PI*i/360.0);
1616
z.at(i) = log(i);
1717
}
18-
19-
// Plot line from given x and y data. Color is selected automatically.
20-
plt::plot(x, y);
18+
19+
// Set the size of output image = 1200x780 pixels
20+
plt::figure_size(1200, 780);
21+
// Plot line from given x and y data. Color is selected automatically.
22+
plt::plot(x, y);
2123
// Plot a red dashed line from given x and y data.
2224
plt::plot(x, w,"r--");
2325
// Plot a line whose name will show up as "log(x)" in the legend.

examples/basic.png

13.2 KB
Loading

matplotlibcpp.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,26 @@ inline void figure()
751751
Py_DECREF(res);
752752
}
753753

754+
inline void figure_size(size_t w, size_t h)
755+
{
756+
const size_t dpi = 100;
757+
PyObject* size = PyTuple_New(2);
758+
PyTuple_SetItem(size, 0, PyFloat_FromDouble((double)w / dpi));
759+
PyTuple_SetItem(size, 1, PyFloat_FromDouble((double)h / dpi));
760+
761+
PyObject* kwargs = PyDict_New();
762+
PyDict_SetItemString(kwargs, "figsize", size);
763+
PyDict_SetItemString(kwargs, "dpi", PyLong_FromSize_t(dpi));
764+
765+
PyObject* res = PyObject_Call(detail::_interpreter::get().s_python_function_figure,
766+
detail::_interpreter::get().s_python_empty_tuple, kwargs);
767+
768+
Py_DECREF(kwargs);
769+
770+
if(!res) throw std::runtime_error("Call to figure_size() failed.");
771+
Py_DECREF(res);
772+
}
773+
754774
inline void legend()
755775
{
756776
PyObject* res = PyObject_CallObject(detail::_interpreter::get().s_python_function_legend, detail::_interpreter::get().s_python_empty_tuple);

0 commit comments

Comments
 (0)