Skip to content

Commit ec08f73

Browse files
committed
Merge branch 'master' into develop
2 parents 1f7f292 + dc423d4 commit ec08f73

File tree

6 files changed

+77
-3
lines changed

6 files changed

+77
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# matplotlibcpp17
22

3-
A C++ header-only plotting library based on pybind11 and matplotlib
3+
A C++ header-only library for matplotlib based on pybind
44

55
-----
66

7-
This project aims to replace [matplotlibcpp](https://github.com/lava/matplotlib-cpp) using [pybind11](https://github.com/pybind/pybind11) as backend.
7+
[matplotlibcpp17](https://github.com/soblin/matplotlibcpp17) is an yet another C++ library for matplotlib featuring more functionalities than matplotlibcpp.
88

99
It is supposed to provide the user with almost full access to matplotlib features in C++, by implementing as many *wrapper classes* of matplotlib module as possible (like `axes::Axes`, `figure::Figure`). And its primary advantage over conventional matplotlibcpp is that the user can pass a variety of arguments as in the form of *args* and *kwargs* thanks to pybind11, without the need for coversion to `map<string, string>`, thus leading to more flexibility.
1010

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
add_demo(align_labels_demo align_labels_demo.cpp)
22
add_demo(gridspec_multicolumn gridspec_multicolumn.cpp)
33
add_demo(multiple_figs_demo multiple_figs_demo.cpp)
4+
add_demo(colorbar_placement colorbar_placement.cpp)
45
add_demo(subplots subplots.cpp)
56

67
add_custom_target(subplots_axes_and_figures
7-
DEPENDS align_labels_demo gridspec_multicolumn multiple_figs_demo
8+
DEPENDS align_labels_demo gridspec_multicolumn multiple_figs_demo colorbar_placement
89
COMMAND align_labels_demo
910
COMMAND gridspec_multicolumn
1011
COMMAND multiple_figs_demo
12+
COMMAND colorbar_placement
1113
COMMENT "subplots_axes_and_figures"
1214
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
1315
)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// example from
2+
// https://matplotlib.org/stable/gallery/subplots_axes_and_figures/colorbar_placement.html
3+
4+
#include <pybind11/embed.h>
5+
#include <pybind11/stl.h>
6+
7+
#include <matplotlibcpp17/pyplot.h>
8+
9+
#include <xtensor/xrandom.hpp>
10+
11+
#include <vector>
12+
13+
namespace py = pybind11;
14+
using namespace py::literals;
15+
using namespace std;
16+
using namespace matplotlibcpp17;
17+
18+
int main1() {
19+
auto plt = matplotlibcpp17::pyplot::import();
20+
auto [fig, axs] = plt.subplots(2, 2);
21+
const vector<string> cmaps = {"RdBu_r", "viridis"};
22+
for (auto col : {0, 1}) {
23+
for (auto row : {0, 1}) {
24+
auto x_ = xt::random::randn<double>({20, 20}) * (col + 1.0);
25+
vector<vector<double>> x(20);
26+
for (int i = 0; i < 20; ++i) {
27+
x[i].resize(20);
28+
for (int j = 0; j < 20; ++j)
29+
x[i][j] = x_(i, j);
30+
}
31+
auto &ax = axs[col + row * 2];
32+
auto pcm = ax.pcolormesh(Args(x), Kwargs("cmap"_a = cmaps[col]));
33+
fig.colorbar(Args(pcm.unwrap()),
34+
Kwargs("ax"_a = ax.unwrap(), "shrink"_a = 0.6));
35+
}
36+
}
37+
#if USE_GUI
38+
plt.show();
39+
#else
40+
plt.savefig(Args("colorbar_placement1.png"));
41+
#endif
42+
return 0;
43+
}
44+
45+
int main() {
46+
py::scoped_interpreter guard{};
47+
main1();
48+
}

include/matplotlibcpp17/axes.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ struct DECL_STRUCT_ATTR Axes : public BaseWrapper {
123123
legend::Legend legend(const pybind11::tuple &args = pybind11::tuple(),
124124
const pybind11::dict &kwargs = pybind11::dict());
125125

126+
// pcolormesh
127+
collections::QuadMesh
128+
pcolormesh(const pybind11::tuple &args = pybind11::tuple(),
129+
const pybind11::dict &kwargs = pybind11::dict());
130+
126131
// plot
127132
pybind11::object plot(const pybind11::tuple &args = pybind11::tuple(),
128133
const pybind11::dict &kwargs = pybind11::dict());
@@ -234,6 +239,7 @@ struct DECL_STRUCT_ATTR Axes : public BaseWrapper {
234239
LOAD_FUNC_ATTR(hist2d, self);
235240
LOAD_FUNC_ATTR(invert_yaxis, self);
236241
LOAD_FUNC_ATTR(legend, self);
242+
LOAD_FUNC_ATTR(pcolormesh, self);
237243
LOAD_FUNC_ATTR(plot, self);
238244
// NOTE: only when called with projection='3d', `plot_surface`, `plot_wireframe`, `set_zlabel` prop exists.
239245
try {
@@ -283,6 +289,7 @@ struct DECL_STRUCT_ATTR Axes : public BaseWrapper {
283289
pybind11::object hist2d_attr;
284290
pybind11::object invert_yaxis_attr;
285291
pybind11::object legend_attr;
292+
pybind11::object pcolormesh_attr;
286293
pybind11::object plot_attr;
287294
pybind11::object plot_surface_attr;
288295
pybind11::object plot_wireframe_attr;
@@ -484,6 +491,13 @@ legend::Legend Axes::legend(const pybind11::tuple &args,
484491
return legend::Legend(obj);
485492
}
486493

494+
// pcolormesh
495+
collections::QuadMesh Axes::pcolormesh(const pybind11::tuple &args,
496+
const pybind11::dict &kwargs) {
497+
pybind11::object ret = pcolormesh_attr(*args, **kwargs);
498+
return collections::QuadMesh(ret);
499+
}
500+
487501
// plot
488502
pybind11::object Axes::plot(const pybind11::tuple &args,
489503
const pybind11::dict &kwargs) {

include/matplotlibcpp17/collections.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ pybind11::object PatchCollection::set_array(const pybind11::tuple &args,
7373
pybind11::object ret = set_array_attr(*args, **kwargs);
7474
return ret;
7575
}
76+
77+
/**
78+
* @brief A wrapper class for matplotlib.collections.QuadMesh
79+
**/
80+
struct DECL_STRUCT_ATTR QuadMesh : public BaseWrapper {
81+
public:
82+
QuadMesh(pybind11::object quadmesh) { self = quadmesh; }
83+
};
84+
7685
} // namespace matplotlibcpp17::collections
7786

7887
#endif /* MATPLOTLIBCPP17_COLLECTIONS_H */

include/matplotlibcpp17/pyplot.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ std::tuple<figure::Figure, std::vector<axes::Axes>>
302302
PyPlot::subplots(int r, int c, const pybind11::dict &kwargs) {
303303
// subplots() returns [][] (if r > 1 && c > 1) else []
304304
// return []axes in row-major
305+
// NOTE: equal to Axes.flat
305306
pybind11::tuple args = pybind11::make_tuple(r, c);
306307
pybind11::list ret = subplots_attr(*args, **kwargs);
307308
std::vector<axes::Axes> axes;

0 commit comments

Comments
 (0)