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

Skip to content

Commit 1875696

Browse files
pnarvorlava
authored andcommitted
Added 'set_aspect' and set_aspect_equal function
1 parent cab80f3 commit 1875696

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

examples/modern.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ int main()
2424
// y must either be callable (providing operator() const) or iterable.
2525
plt::plot(x, y, "r-", x, [](double d) { return 12.5+abs(sin(d)); }, "k-");
2626

27+
//plt::set_aspect(0.5);
28+
plt::set_aspect_equal();
29+
2730

2831
// show plots
2932
plt::show();

matplotlibcpp.h

+56
Original file line numberDiff line numberDiff line change
@@ -1857,6 +1857,62 @@ inline void legend(const std::map<std::string, std::string>& keywords)
18571857
Py_DECREF(res);
18581858
}
18591859

1860+
template<typename Numeric>
1861+
inline void set_aspect(Numeric ratio)
1862+
{
1863+
detail::_interpreter::get();
1864+
1865+
PyObject* args = PyTuple_New(1);
1866+
PyTuple_SetItem(args, 0, PyFloat_FromDouble(ratio));
1867+
PyObject* kwargs = PyDict_New();
1868+
1869+
PyObject *ax =
1870+
PyObject_CallObject(detail::_interpreter::get().s_python_function_gca,
1871+
detail::_interpreter::get().s_python_empty_tuple);
1872+
if (!ax) throw std::runtime_error("Call to gca() failed.");
1873+
Py_INCREF(ax);
1874+
1875+
PyObject *set_aspect = PyObject_GetAttrString(ax, "set_aspect");
1876+
if (!set_aspect) throw std::runtime_error("Attribute set_aspect not found.");
1877+
Py_INCREF(set_aspect);
1878+
1879+
PyObject *res = PyObject_Call(set_aspect, args, kwargs);
1880+
if (!res) throw std::runtime_error("Call to set_aspect() failed.");
1881+
Py_DECREF(set_aspect);
1882+
1883+
Py_DECREF(ax);
1884+
Py_DECREF(args);
1885+
Py_DECREF(kwargs);
1886+
}
1887+
1888+
inline void set_aspect_equal()
1889+
{
1890+
// expect ratio == "equal". Leaving error handling to matplotlib.
1891+
detail::_interpreter::get();
1892+
1893+
PyObject* args = PyTuple_New(1);
1894+
PyTuple_SetItem(args, 0, PyString_FromString("equal"));
1895+
PyObject* kwargs = PyDict_New();
1896+
1897+
PyObject *ax =
1898+
PyObject_CallObject(detail::_interpreter::get().s_python_function_gca,
1899+
detail::_interpreter::get().s_python_empty_tuple);
1900+
if (!ax) throw std::runtime_error("Call to gca() failed.");
1901+
Py_INCREF(ax);
1902+
1903+
PyObject *set_aspect = PyObject_GetAttrString(ax, "set_aspect");
1904+
if (!set_aspect) throw std::runtime_error("Attribute set_aspect not found.");
1905+
Py_INCREF(set_aspect);
1906+
1907+
PyObject *res = PyObject_Call(set_aspect, args, kwargs);
1908+
if (!res) throw std::runtime_error("Call to set_aspect() failed.");
1909+
Py_DECREF(set_aspect);
1910+
1911+
Py_DECREF(ax);
1912+
Py_DECREF(args);
1913+
Py_DECREF(kwargs);
1914+
}
1915+
18601916
template<typename Numeric>
18611917
void ylim(Numeric left, Numeric right)
18621918
{

0 commit comments

Comments
 (0)