Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added 'set_aspect' and set_aspect_equal function #257

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
lava merged 1 commit into lava:master from pnarvor:aspect-control
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/modern.cpp
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ int main()
// y must either be callable (providing operator() const) or iterable.
plt::plot(x, y, "r-", x, [](double d) { return 12.5+abs(sin(d)); }, "k-");

//plt::set_aspect(0.5);
plt::set_aspect_equal();


// show plots
plt::show();
Expand Down
56 changes: 56 additions & 0 deletions matplotlibcpp.h
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1857,6 +1857,62 @@ inline void legend(const std::map<std::string, std::string>& keywords)
Py_DECREF(res);
}

template<typename Numeric>
inline void set_aspect(Numeric ratio)
{
detail::_interpreter::get();

PyObject* args = PyTuple_New(1);
PyTuple_SetItem(args, 0, PyFloat_FromDouble(ratio));
PyObject* kwargs = PyDict_New();

PyObject *ax =
PyObject_CallObject(detail::_interpreter::get().s_python_function_gca,
detail::_interpreter::get().s_python_empty_tuple);
if (!ax) throw std::runtime_error("Call to gca() failed.");
Py_INCREF(ax);

PyObject *set_aspect = PyObject_GetAttrString(ax, "set_aspect");
if (!set_aspect) throw std::runtime_error("Attribute set_aspect not found.");
Py_INCREF(set_aspect);

PyObject *res = PyObject_Call(set_aspect, args, kwargs);
if (!res) throw std::runtime_error("Call to set_aspect() failed.");
Py_DECREF(set_aspect);

Py_DECREF(ax);
Py_DECREF(args);
Py_DECREF(kwargs);
}

inline void set_aspect_equal()
{
// expect ratio == "equal". Leaving error handling to matplotlib.
detail::_interpreter::get();

PyObject* args = PyTuple_New(1);
PyTuple_SetItem(args, 0, PyString_FromString("equal"));
PyObject* kwargs = PyDict_New();

PyObject *ax =
PyObject_CallObject(detail::_interpreter::get().s_python_function_gca,
detail::_interpreter::get().s_python_empty_tuple);
if (!ax) throw std::runtime_error("Call to gca() failed.");
Py_INCREF(ax);

PyObject *set_aspect = PyObject_GetAttrString(ax, "set_aspect");
if (!set_aspect) throw std::runtime_error("Attribute set_aspect not found.");
Py_INCREF(set_aspect);

PyObject *res = PyObject_Call(set_aspect, args, kwargs);
if (!res) throw std::runtime_error("Call to set_aspect() failed.");
Py_DECREF(set_aspect);

Py_DECREF(ax);
Py_DECREF(args);
Py_DECREF(kwargs);
}

template<typename Numeric>
void ylim(Numeric left, Numeric right)
{
Expand Down

AltStyle によって変換されたページ (->オリジナル) /