Skip to content

Commit 931858a

Browse files
committed
debugging
1 parent 65b6a6f commit 931858a

5 files changed

Lines changed: 82 additions & 1 deletion

File tree

gallery/shapes_and_collections/patch_collection.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,55 @@
1313
namespace py = pybind11;
1414
using namespace py::literals;
1515
using namespace std;
16+
using namespace matplotlibcpp17;
1617
using namespace matplotlibcpp17::util;
1718

1819
int main() {
1920
py::scoped_interpreter guard{};
2021
auto plt = matplotlibcpp17::pyplot::import();
22+
auto [fig, ax] = plt.subplots();
23+
24+
const int N = 3;
25+
vector<double> x = {0.7003673, 0.74275081, 0.70928001},
26+
y = {0.56674552, 0.97778533, 0.70633485},
27+
radii = {0.02479158, 0.01578834, 0.06976985};
28+
py::list patches; // instead of patches = []
29+
for (int i = 0; i < N; ++i) {
30+
const double x1 = x[i], y1 = y[i], r = radii[i];
31+
auto circle = patches::Circle(args_(py::make_tuple(x1, y1), r));
32+
patches.append(circle.unwrap());
33+
}
34+
x = {0.71995667, 0.25774443, 0.34154678};
35+
y = {0.96876117, 0.6945071, 0.46638326};
36+
radii = {0.07028127, 0.05117859, 0.09287414};
37+
vector<double> theta1 = {266.3169476, 224.07805212, 234.5563688},
38+
theta2 = {142.85074015, 195.56618216, 287.96383014};
39+
for (int i = 0; i < N; ++i) {
40+
const double x1 = x[i], y1 = y[i], r = radii[i], th1 = theta1[i],
41+
th2 = theta2[i];
42+
auto wedge = patches::Wedge(args_(py::make_tuple(x1, y1), r, th1, th2));
43+
patches.append(wedge.unwrap());
44+
}
45+
patches.append(
46+
patches::Wedge(args_(py::make_tuple(0.3, 0.7), 0.1, 0, 360)).unwrap());
47+
patches.append(patches::Wedge(args_(py::make_tuple(0.7, 0.8), 0.2, 0, 360),
48+
kwargs_("width"_a = 0.05))
49+
.unwrap());
50+
patches.append(
51+
patches::Wedge(args_(py::make_tuple(0.8, 0.3), 0.2, 0, 45)).unwrap());
52+
patches.append(patches::Wedge(args_(py::make_tuple(0.8, 0.3), 0.2, 45, 90),
53+
kwargs_("width"_a = 0.10))
54+
.unwrap());
55+
// NOTE: Polygon take numpy array as argument, so skip it
56+
vector<double> colors_ = {90.63036451, 16.10182093, 74.36211347, 63.29741618,
57+
32.41800177, 92.23765324, 23.72264387, 82.39455709,
58+
75.06071403, 11.37844527};
59+
py::list colors = py::cast(colors_);
60+
auto p =
61+
collections::PathCollection(args_(patches), kwargs_("alpha"_a = 0.4));
62+
p.set_array(colors);
63+
ax.add_collection(p.unwrap()); // erroneous
64+
// fig.colorbar(p.unwrap(), "ax"_a = ax.unwrap());
65+
plt.show();
66+
return 0;
2167
}

include/matplotlibcpp17/axes.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ struct DECL_STRUCT_ATTR Axes {
77
}
88
void load_attrs() {
99
LOAD_VOID_ATTR(add_artist, self);
10+
LOAD_VOID_ATTR(add_collection, self);
1011
LOAD_VOID_ATTR(add_patch, self);
1112
LOAD_VOID_ATTR(axhline, self);
1213
LOAD_NONVOID_ATTR(bar, self);
@@ -45,6 +46,9 @@ struct DECL_STRUCT_ATTR Axes {
4546
// add_artist
4647
pybind11::object add_artist;
4748

49+
// add_collection
50+
pybind11::object add_collection;
51+
4852
// add_patch
4953
pybind11::object add_patch;
5054

include/matplotlibcpp17/collections.h

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
namespace collections {
22

33
struct DECL_STRUCT_ATTR PathCollection {
4+
PathCollection(const pybind11::tuple &args = pybind11::tuple(),
5+
const pybind11::dict &kwargs = pybind11::dict()) {
6+
pybind11::object attr = pybind11::module::import("matplotlib.collections")
7+
.attr("PathCollection");
8+
self = attr(*args, **kwargs);
9+
load_attrs();
10+
}
411
PathCollection(pybind11::object pathcollection) {
512
self = pathcollection;
613
load_attrs();
714
}
8-
void load_attrs() { LOAD_NONVOID_ATTR(legend_elements, self); }
15+
void load_attrs() {
16+
LOAD_NONVOID_ATTR(legend_elements, self);
17+
LOAD_VOID_ATTR(set_array, self);
18+
}
919
pybind11::object self;
1020

1121
pybind11::object unwrap() { return self; }
@@ -14,6 +24,9 @@ struct DECL_STRUCT_ATTR PathCollection {
1424
pybind11::object legend_elements_attr;
1525
std::pair<pybind11::object, pybind11::object>
1626
legend_elements(const pybind11::tuple &args, const pybind11::dict &kwargs);
27+
28+
// set_array
29+
pybind11::object set_array;
1730
};
1831

1932
// legend_elements

include/matplotlibcpp17/figure.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ struct DECL_STRUCT_ATTR Figure {
1010
LOAD_NONVOID_ATTR(add_gridspec, self);
1111
LOAD_NONVOID_ATTR(add_subplot, self);
1212
LOAD_VOID_ATTR(align_labels, self);
13+
LOAD_VOID_ATTR(colorbar, self);
1314
LOAD_VOID_ATTR(savefig, self);
1415
LOAD_VOID_ATTR(suptitle, self);
1516
LOAD_VOID_ATTR(tight_layout, self);
@@ -37,6 +38,9 @@ struct DECL_STRUCT_ATTR Figure {
3738
// align_labels
3839
pybind11::object align_labels;
3940

41+
// colorbar
42+
pybind11::object colorbar;
43+
4044
// savefig
4145
pybind11::object savefig;
4246

include/matplotlibcpp17/patches.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,18 @@ struct DECL_STRUCT_ATTR Rectangle {
4444
pybind11::object rectangle_attr;
4545
};
4646

47+
struct DECL_STRUCT_ATTR Wedge {
48+
Wedge(const pybind11::tuple &args = pybind11::tuple(),
49+
const pybind11::dict &kwargs = pybind11::dict()) {
50+
wedge_attr = pybind11::module::import("matplotlib.patches").attr("Wedge");
51+
self = wedge_attr(*args, **kwargs);
52+
}
53+
pybind11::object self;
54+
55+
// for passing as python object
56+
pybind11::object unwrap() { return self; }
57+
58+
pybind11::object wedge_attr;
59+
};
60+
4761
} // namespace patches

0 commit comments

Comments
 (0)