Skip to content
Snippets Groups Projects
Commit a6866a94 authored by soblin's avatar soblin
Browse files

added images_contours_and_fields/contourf_log.cpp, and ticker

parent c2dd7040
Branches
No related tags found
No related merge requests found
add_demo(quiver_demo quiver_demo.cpp) add_demo(quiver_demo quiver_demo.cpp)
add_demo(contourf_log contourf_log)
add_custom_target(images_contours_and_fields add_custom_target(images_contours_and_fields
DEPENDS quiver_demo DEPENDS quiver_demo contourf_log
COMMAND quiver_demo COMMAND quiver_demo
COMMENT "running quiver_demo" COMMAND contourf_log
COMMENT "running images_contours_and_fields"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../images" WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../images"
) )
// example from
// https://matplotlib.org/stable/gallery/images_contours_and_fields/contourf_log.html
#include <matplotlibcpp17/pyplot.h>
#include <matplotlibcpp17/cm.h>
#include <matplotlibcpp17/ticker.h>
#include <xtensor/xbuilder.hpp>
#include <xtensor/xview.hpp>
#include <xtensor/xmath.hpp>
#include <vector>
using namespace std;
using namespace matplotlibcpp17;
using namespace xt::placeholders;
using mesh2D = vector<vector<double>>;
int main() {
const int N = 100;
auto x_ = xt::linspace(-3.0, 3.0, N);
auto y_ = xt::linspace(-2.0, 2.0, N);
auto [X_, Y_] = xt::meshgrid(x_, y_);
auto Z1_ = xt::exp(-xt::pow(X_, 2) - xt::pow(Y_, 2));
auto Z2_ = xt::exp(-xt::pow(X_ * 10, 2) - xt::pow(Y_ * 10, 2));
xt::xarray<double> z_ = Z1_ + 50 * Z2_;
// instead of x[:5, :5] = -1.0
auto v = xt::view(z_, xt::range(_, 5), xt::range(_, 5));
v = 0.0;
// to vector<vector>>
const int xsz = x_.shape()[0], ysz = y_.shape()[0];
mesh2D X(xsz), Y(xsz), z(xsz);
for (int i = 0; i < xsz; ++i) {
X[i].resize(ysz);
Y[i].resize(ysz);
z[i].resize(ysz);
for (int j = 0; j < ysz; ++j) {
X[i][j] = X_(i, j);
Y[i][j] = Y_(i, j);
z[i][j] = z_(i, j);
}
}
py::scoped_interpreter guard{};
// to numpy array
auto Xpy = py::array(py::cast(std::move(X)));
auto Ypy = py::array(py::cast(std::move(Y)));
auto zpy = py::array(py::cast(std::move(z)));
auto plt = pyplot::import();
auto [fig, ax] = plt.subplots();
auto cs = ax.contourf(Args(Xpy, Ypy, zpy),
Kwargs("locator"_a = ticker::LogLocator().unwrap(),
"cmap"_a = cm::PuBu_r()));
fig.colorbar(Args(cs));
plt.show();
}
...@@ -69,6 +69,10 @@ public: ...@@ -69,6 +69,10 @@ public:
pybind11::object contour(const pybind11::tuple &args = pybind11::tuple(), pybind11::object contour(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict()); const pybind11::dict &kwargs = pybind11::dict());
// contourf
pybind11::object contourf(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
// errorbar // errorbar
pybind11::object errorbar(const pybind11::tuple &args = pybind11::tuple(), pybind11::object errorbar(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict()); const pybind11::dict &kwargs = pybind11::dict());
...@@ -226,6 +230,7 @@ private: ...@@ -226,6 +230,7 @@ private:
#endif #endif
LOAD_FUNC_ATTR(barh, self); LOAD_FUNC_ATTR(barh, self);
LOAD_FUNC_ATTR(contour, self); LOAD_FUNC_ATTR(contour, self);
LOAD_FUNC_ATTR(contourf, self);
LOAD_FUNC_ATTR(errorbar, self); LOAD_FUNC_ATTR(errorbar, self);
LOAD_FUNC_ATTR(fill, self); LOAD_FUNC_ATTR(fill, self);
LOAD_FUNC_ATTR(fill_between, self); LOAD_FUNC_ATTR(fill_between, self);
...@@ -276,6 +281,7 @@ private: ...@@ -276,6 +281,7 @@ private:
pybind11::object bar_label_attr; pybind11::object bar_label_attr;
pybind11::object barh_attr; pybind11::object barh_attr;
pybind11::object contour_attr; pybind11::object contour_attr;
pybind11::object contourf_attr;
pybind11::object errorbar_attr; pybind11::object errorbar_attr;
pybind11::object fill_attr; pybind11::object fill_attr;
pybind11::object fill_between_attr; pybind11::object fill_between_attr;
...@@ -375,6 +381,13 @@ pybind11::object Axes::contour(const pybind11::tuple &args, ...@@ -375,6 +381,13 @@ pybind11::object Axes::contour(const pybind11::tuple &args,
return obj; return obj;
} }
// contourf
pybind11::object Axes::contourf(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object obj = contourf_attr(*args, **kwargs);
return obj;
}
// errorbar // errorbar
pybind11::object Axes::errorbar(const pybind11::tuple &args, pybind11::object Axes::errorbar(const pybind11::tuple &args,
const pybind11::dict &kwargs) { const pybind11::dict &kwargs) {
......
...@@ -16,6 +16,12 @@ pybind11::object coolwarm() { ...@@ -16,6 +16,12 @@ pybind11::object coolwarm() {
return ret; return ret;
} }
pybind11::object PuBu_r() {
pybind11::object ret =
pybind11::module::import("matplotlib.cm").attr("PuBu_r");
return ret;
}
} // namespace matplotlibcpp17::cm } // namespace matplotlibcpp17::cm
#endif /* MATPLOTLIBCPP17_CM_H */ #endif /* MATPLOTLIBCPP17_CM_H */
/**
* @file ticker.h
* @brief corresponding header for matplotlib.ticker
**/
#ifndef MATPLOTLIBCPP17_TICKER_H
#define MATPLOTLIBCPP17_TICKER_H
#include <pybind11/pybind11.h>
#include <matplotlibcpp17/common.h>
namespace matplotlibcpp17::ticker {
struct DECL_STRUCT_ATTR LogLocator : public BaseWrapper {
public:
LogLocator() {
pybind11::object attr =
pybind11::module::import("matplotlib.ticker").attr("LogLocator");
self = attr();
}
};
} // namespace matplotlibcpp17::ticker
#endif /* MATPLOTLIBCPP17_ANIMATION_H */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment