Skip to content

Commit 042bea7

Browse files
author
MamoruSobue
committed
added lines_bars_and_markers/errorbar_subsample
1 parent a3bdf38 commit 042bea7

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

gallery/lines_bars_and_markers/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ add_demo(fill_betweenx_demo fill_betweenx_demo.cpp)
77
add_demo(scatter_with_legend scatter_with_legend.cpp)
88
add_demo(scatter_hist scatter_hist.cpp)
99
add_demo(errorbar_limits_simple errorbar_limits_simple.cpp)
10+
add_demo(errorbar_subsample errorbar_subsample.cpp)
1011

1112
add_custom_target(lines_bars_and_markers
12-
DEPENDS bar_label_demo fill simple_plot scatter_symbol fill_between_demo fill_betweenx_demo scatter_with_legend scatter_hist errorbar_limits_simple
13+
DEPENDS bar_label_demo fill simple_plot scatter_symbol fill_between_demo fill_betweenx_demo scatter_with_legend scatter_hist errorbar_limits_simple errorbar_subsample
1314
COMMAND bar_label_demo
1415
COMMAND fill
1516
COMMAND simple_plot
@@ -19,6 +20,7 @@ add_custom_target(lines_bars_and_markers
1920
COMMAND scatter_with_legend
2021
COMMAND scatter_hist
2122
COMMAND errorbar_limits_simple
23+
COMMAND errorbar_subsample
2224
COMMENT "running lines_bars_and_markers"
2325
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../images"
2426
)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// example from
2+
// https://matplotlib.org/stable/gallery/lines_bars_and_markers/errorbar_subsample.html
3+
4+
#include <pybind11/embed.h>
5+
#include <pybind11/stl.h>
6+
7+
#include <matplotlibcpp17/pyplot.h>
8+
9+
#include <xtensor/xbuilder.hpp>
10+
#include <xtensor/xmath.hpp>
11+
12+
#include <vector>
13+
14+
namespace py = pybind11;
15+
using namespace py::literals;
16+
using namespace std;
17+
using namespace matplotlibcpp17;
18+
19+
int main() {
20+
auto x_ = xt::arange(0.1, 4.0, 0.1);
21+
auto y1_ = xt::exp(-1.0 * x_);
22+
auto y2_ = xt::exp(-0.5 * x_);
23+
auto y1err_ = 0.1 + 0.1 * xt::sqrt(x_);
24+
auto y2err_ = 0.1 + 0.1 * xt::sqrt(x_ / 2.0);
25+
vector<double> x(x_.begin(), x_.end()), y1(y1_.begin(), y1_.end()),
26+
y2(y2_.begin(), y2_.end()), y1err(y1err_.begin(), y1err_.end()),
27+
y2err(y2err_.begin(), y2err_.end());
28+
29+
py::scoped_interpreter guard{};
30+
auto plt = pyplot::import();
31+
auto [fig, axs] = plt.subplots(
32+
1, 3, Kwargs("sharex"_a = true, "figsize"_a = py::make_tuple(12, 6)));
33+
auto ax0 = axs[0], ax1 = axs[1], ax2 = axs[2];
34+
ax0.set_title(Args("all errorbars"));
35+
ax0.errorbar(Args(x, y1), Kwargs("yerr"_a = y1err));
36+
ax0.errorbar(Args(x, y1), Kwargs("yerr"_a = y2err));
37+
38+
ax1.set_title(Args("only every 6th errorbar"));
39+
ax1.errorbar(Args(x, y1), Kwargs("yerr"_a = y1err, "errorevery"_a = 6));
40+
ax1.errorbar(Args(x, y2), Kwargs("yerr"_a = y2err, "errorevery"_a = 6));
41+
42+
ax2.set_title(Args("second seris shifted by 3"));
43+
ax2.errorbar(Args(x, y1),
44+
Kwargs("yerr"_a = y1err, "errorevery"_a = py::make_tuple(0, 6)));
45+
ax2.errorbar(Args(x, y2),
46+
Kwargs("yerr"_a = y2err, "errorevery"_a = py::make_tuple(3, 6)));
47+
48+
fig.suptitle(Args("Errorbar subsampling"));
49+
#if USE_GUI
50+
plt.show();
51+
#else
52+
plt.savefig(Args("erorbar_subsample.png"));
53+
#endif
54+
}

include/matplotlibcpp17/axes.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ struct DECL_STRUCT_ATTR Axes : public BaseWrapper {
6969
pybind11::object contour(const pybind11::tuple &args = pybind11::tuple(),
7070
const pybind11::dict &kwargs = pybind11::dict());
7171

72+
// errorbar
73+
pybind11::object errorbar(const pybind11::tuple &args = pybind11::tuple(),
74+
const pybind11::dict &kwargs = pybind11::dict());
75+
7276
// fill
7377
pybind11::object fill(const pybind11::tuple &args = pybind11::tuple(),
7478
const pybind11::dict &kwargs = pybind11::dict());
@@ -213,6 +217,7 @@ struct DECL_STRUCT_ATTR Axes : public BaseWrapper {
213217
#endif
214218
LOAD_FUNC_ATTR(barh, self);
215219
LOAD_FUNC_ATTR(contour, self);
220+
LOAD_FUNC_ATTR(errorbar, self);
216221
LOAD_FUNC_ATTR(fill, self);
217222
LOAD_FUNC_ATTR(fill_between, self);
218223
LOAD_FUNC_ATTR(fill_betweenx, self);
@@ -258,6 +263,7 @@ struct DECL_STRUCT_ATTR Axes : public BaseWrapper {
258263
pybind11::object bar_label_attr;
259264
pybind11::object barh_attr;
260265
pybind11::object contour_attr;
266+
pybind11::object errorbar_attr;
261267
pybind11::object fill_attr;
262268
pybind11::object fill_between_attr;
263269
pybind11::object fill_betweenx_attr;
@@ -353,6 +359,13 @@ pybind11::object Axes::contour(const pybind11::tuple &args,
353359
return obj;
354360
}
355361

362+
// errorbar
363+
pybind11::object Axes::errorbar(const pybind11::tuple &args,
364+
const pybind11::dict &kwargs) {
365+
pybind11::object obj = errorbar_attr(*args, **kwargs);
366+
return obj;
367+
}
368+
356369
// fill
357370
pybind11::object Axes::fill(const pybind11::tuple &args,
358371
const pybind11::dict &kwargs) {

0 commit comments

Comments
 (0)