Skip to content

Commit b72adfa

Browse files
author
MamoruSobue
committed
added mplot3d/errorbar3d
1 parent a4ffc57 commit b72adfa

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

gallery/mplot3d/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ add_demo(lines3d lines3d.cpp)
22
add_demo(lorenz_attractor lorenz_attractor.cpp)
33
add_demo(contour3d contour3d.cpp)
44
add_demo(subplot3d subplot3d.cpp)
5+
add_demo(errorbar3d errorbar3d.cpp)
56

67
add_custom_target(mplot3d
7-
DEPENDS lines3d lorenz_attractor contour3d subplot3d
8+
DEPENDS lines3d lorenz_attractor contour3d subplot3d errorbar3d
89
COMMAND lines3d
910
COMMAND lorenz_attractor
1011
COMMAND contour3d
1112
COMMAND subplot3d
13+
COMMAND errorbar3d
1214
COMMENT "running mplot3d"
1315
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../images"
1416
)

gallery/mplot3d/errorbar3d.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// example from https://matplotlib.org/stable/gallery/mplot3d/errorbar3d.html
2+
3+
#include <pybind11/embed.h>
4+
#include <pybind11/stl.h>
5+
6+
#include <matplotlibcpp17/pyplot.h>
7+
8+
#include <xtensor/xbuilder.hpp>
9+
#include <xtensor/xmath.hpp>
10+
11+
#include <vector>
12+
#include <algorithm>
13+
14+
namespace py = pybind11;
15+
using namespace py::literals;
16+
using namespace std;
17+
using namespace matplotlibcpp17;
18+
19+
int main() {
20+
py::scoped_interpreter guard{};
21+
auto plt = pyplot::import();
22+
auto ax = plt.figure().add_subplot(Args(), Kwargs("projection"_a = "3d"));
23+
auto t_ = xt::arange(0.0, 2 * M_PI + 0.1, 0.01);
24+
auto x_ = xt::sin(1.0 * t_);
25+
auto y_ = xt::cos(3.0 * t_);
26+
auto z_ = xt::sin(5.0 * t_);
27+
vector<double> t(t_.begin(), t_.end()), x(x_.begin(), x_.end()),
28+
y(y_.begin(), y_.end()), z(z_.begin(), z_.end());
29+
30+
const int estep = 15;
31+
vector<int> i, zuplims, zlolims;
32+
std::iota(i.begin(), i.end(), 0);
33+
std::transform(i.begin(), i.end(), std::back_inserter(zuplims), [](int i) {
34+
return (i % 15 == 0) and ((i / estep) % 3 == 0);
35+
});
36+
std::transform(i.begin(), i.end(), std::back_inserter(zlolims), [](int i) {
37+
return (i % 15 == 0) and ((i / estep) % 3 == 2);
38+
});
39+
40+
ax.errorbar(Args(x, y, z, 0.2),
41+
Kwargs("zuplims"_a = zuplims, "zlolims"_a = zlolims,
42+
"errorevery"_a = estep));
43+
44+
ax.set_xlabel(Args("X label"));
45+
ax.set_ylabel(Args("Y label"));
46+
ax.set_zlabel(Args("Z label"));
47+
48+
plt.show();
49+
}

include/matplotlibcpp17/axes.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,10 @@ struct DECL_STRUCT_ATTR Axes : public BaseWrapper {
237237
plot_wireframe_attr = self.attr("plot_wireframe");
238238
set_zlabel_attr = self.attr("set_zlabel");
239239
INFO_MSG("Loaded Axes3D");
240+
projection_3d = true;
241+
} catch (...) {
242+
projection_3d = false;
240243
}
241-
catch(...) {}
242244
LOAD_FUNC_ATTR(quiver, self);
243245
LOAD_FUNC_ATTR(quiverkey, self);
244246
LOAD_FUNC_ATTR(scatter, self);
@@ -295,6 +297,7 @@ struct DECL_STRUCT_ATTR Axes : public BaseWrapper {
295297
pybind11::object set_zlabel_attr;
296298
pybind11::object text_attr;
297299
pybind11::object tick_params_attr;
300+
bool projection_3d;
298301
};
299302

300303
// add_artist
@@ -362,8 +365,14 @@ pybind11::object Axes::contour(const pybind11::tuple &args,
362365
// errorbar
363366
pybind11::object Axes::errorbar(const pybind11::tuple &args,
364367
const pybind11::dict &kwargs) {
365-
pybind11::object obj = errorbar_attr(*args, **kwargs);
366-
return obj;
368+
if (not projection_3d) {
369+
pybind11::object obj = errorbar_attr(*args, **kwargs);
370+
return obj;
371+
} else {
372+
ERROR_MSG("Call to errorbar with projection='3d' is invalid because "
373+
"matplotlib version is < 3.4.0");
374+
std::exit(0);
375+
}
367376
}
368377

369378
// fill

0 commit comments

Comments
 (0)