Skip to content
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

Add additional functions #23

Merged
merged 5 commits into from
Oct 17, 2022
Merged

Add additional functions #23

merged 5 commits into from
Oct 17, 2022

Conversation

jurihock
Copy link
Contributor

@jurihock jurihock commented Oct 16, 2022

There are two colorbar functions in matplotlib: Figure.colorbar (as already implemented) and pyplot.colorbar (proposed in this pr). The mappable argument is mandatory in Figure.colorbar but optional in pyplot.colorbar, which internally defaults to pyplot.gci. See also matplotlib.pyplot.colorbar.

@jurihock jurihock changed the title Add imshow function Add additional functions Oct 16, 2022
@soblin soblin self-requested a review October 16, 2022 14:09
@soblin
Copy link
Owner

soblin commented Oct 16, 2022

Hi @jurihock thanks for you PR !
Do you have example codes using these functions ? Or do you know any related examples in this page ?

https://matplotlib.org/stable/gallery/index.html

I want to add them in gallery/contrib folder for better coverage if possible.

@jurihock
Copy link
Contributor Author

I plan to use it here instead of the current matplotlibcpp binding. But for that I need at least imshow and clim.

I'm not familiar with xtensor, but numcpp works fine to draw a 2D sombrero:

#include <matplotlibcpp17/pyplot.h>
#include <NumCpp.hpp>

namespace py = pybind11;

void test()
{
  py::scoped_interpreter guard{};

  const int size = 1000;
  const double sigma = 100;

  const auto i = nc::arange<double>(size) - (0.5 * size);
  const auto [x, y] = nc::meshgrid<double>(i, i);

  const auto xy = (nc::power(x, 2) + nc::power(y, 2)) / (-2.0 * nc::power(sigma, 2));

  const auto sombrero = nc::exp(xy) * (xy + 1.0) / (std::acos(-1.0) * nc::power(sigma, 4));
  const auto pysombrero = nc::pybindInterface::nc2pybind(sombrero);

  auto plot = matplotlibcpp17::pyplot::import();

  plot.imshow(Args(pysombrero), Kwargs(
    "extent"_a = py::make_tuple(-1, +1, -1, +1),
    "cmap"_a = "inferno"));

  plot.colorbar();

  plot.clim(py::make_tuple(-4e-10, +3e-9));

  plot.show();
}

sombrero

Maybe something like that would be a suitable example for the gallery...

@soblin
Copy link
Owner

soblin commented Oct 16, 2022

LGTM.

Then you can rename test() to main() and just push that code to gallery/contrib/sombrero,cpp without CMakeLists.txt.
Can you tell me the version or current commit hash of your numcpp ? Maybe I'll use numcpp instead of xtensor in the next release.
Also you can add that image to gallery/contrib/sombrero.png (maybe you need to add -f). Then I will review and merge this PR to latest main !

I think I'll add some examples from the official matplotlib gallery using imshow and other new functions later. Then I'll release next version.

@jurihock
Copy link
Contributor Author

Okay, thanks!

I'm using the tagged NumCpp 2.8.0 version.

In order to build the provided example you may have to introduce following defines:

// disable boost
#define NUMCPP_NO_USE_BOOST

// enable nc::pybindInterface::nc2pybind
#define NUMCPP_INCLUDE_PYBIND_PYTHON_INTERFACE

@soblin soblin merged commit bb9e692 into soblin:master Oct 17, 2022
@soblin soblin mentioned this pull request Oct 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants