Skip to content

Commit 7e1ab08

Browse files
authored
Merge pull request #14 from soblin/develop
Develop
2 parents 6f7fe55 + 8cc5165 commit 7e1ab08

File tree

13 files changed

+325
-9
lines changed

13 files changed

+325
-9
lines changed

CMakeLists.txt

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,29 @@ project(
1010
find_package(Python3 COMPONENTS Interpreter Development REQUIRED)
1111
find_package(pybind11 2.4.3 REQUIRED)
1212

13-
set(matplotlibcpp17_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/include")
13+
14+
# check matplotlib minor version
15+
execute_process(
16+
COMMAND
17+
"python3" "-c"
18+
"import matplotlib;
19+
print(str(matplotlib.__version__))"
20+
RESULT_VARIABLE MATPLOTLIB_VERSION_CHECKING
21+
OUTPUT_VARIABLE MATPLOTLIB_VERSION
22+
)
23+
24+
if(NOT MATPLOTLIB_VERSION_CHECKING MATCHES 0)
25+
message(FATAL_ERROR
26+
"Could not check matplotlib.__version__")
27+
endif()
28+
message(STATUS "Detected matplotlib version is ${MATPLOTLIB_VERSION}")
29+
if(${MATPLOTLIB_VERSION} VERSION_LESS 3.4)
30+
message(WARNING "Detected matplotlib version is < 3.4.0")
31+
set(MATPLOTLIB_MINOR_VER_GTE_4 0)
32+
else()
33+
set(MATPLOTLIB_MINOR_VER_GTE_4 1)
34+
endif()
35+
1436

1537
# gallery
1638
if(NOT DEFINED USE_GUI)
@@ -20,6 +42,8 @@ if(NOT DEFINED ADD_DEMO)
2042
set(ADD_DEMO 1)
2143
endif()
2244

45+
set(matplotlibcpp17_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/include")
46+
2347
function(add_demo name path)
2448
add_executable(${name} ${path})
2549
target_include_directories(${name} PUBLIC
@@ -31,9 +55,9 @@ endfunction()
3155

3256
if(${ADD_DEMO})
3357
find_package(Python3 COMPONENTS NumPy REQUIRED)
34-
find_package(xtensor REQUIRED)
58+
find_package(xtensor 0.24.0 REQUIRED)
3559
set(CMAKE_CXX_STANDARD 17)
36-
set(CMAKE_CXX_FLAGS "-Wall -g -DUSE_GUI=${USE_GUI}")
60+
set(CMAKE_CXX_FLAGS "-Wall -g -DUSE_GUI=${USE_GUI} -DMATPLOTLIB_MINOR_VER_GTE_4=${MATPLOTLIB_MINOR_VER_GTE_4}")
3761
add_subdirectory(gallery/lines_bars_and_markers)
3862
add_subdirectory(gallery/subplots_axes_and_figures)
3963
add_subdirectory(gallery/statistics)
@@ -96,6 +120,9 @@ install(EXPORT ${PROJECT_NAME}_Targets
96120
install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/${PROJECT_NAME}
97121
DESTINATION include
98122
)
123+
set(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/LICENSE")
124+
# create .deb
125+
include("${PROJECT_SOURCE_DIR}/cmake/package.cmake")
99126

100127
# uninstall target
101128
## actually it's just `xargs rm < install_manifest.txt`

cmake/package.cmake

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# https://decovar.dev/blog/2021/09/23/cmake-cpack-package-deb-apt/
2+
set(CPACK_PACKAGE_NAME ${PROJECT_NAME})
3+
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY ${CMAKE_PROJECT_DESCRIPTION})
4+
set(CPACK_VERBATIM_VARIABLES YES)
5+
set(CPACK_PACKAGE_INSTALL_DIRECTORY ${CPACK_PACKAGE_NAME})
6+
SET(CPACK_OUTPUT_FILE_PREFIX "${PROJECT_BINARY_DIR}/")
7+
set(CPACK_PACKAGING_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX})
8+
set(CPACK_PACKAGE_VERSION_MAJOR ${PROJECT_VERSION_MAJOR})
9+
set(CPACK_PACKAGE_VERSION_MINOR ${PROJECT_VERSION_MINOR})
10+
set(CPACK_PACKAGE_VERSION_PATCH ${PROJECT_VERSION_PATCH})
11+
set(CPACK_PACKAGE_CONTACT "example@example.com")
12+
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "Deb Example")
13+
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
14+
set(CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_SOURCE_DIR}/README.md")
15+
# package name for deb
16+
set(CPACK_DEBIAN_FILE_NAME DEB-DEFAULT)
17+
set(CPACK_COMPONENTS_GROUPING ALL_COMPONENTS_IN_ONE)
18+
# without this you won't be able to pack only specified component
19+
set(CPACK_DEB_COMPONENT_INSTALL YES)
20+
21+
include(CPack)
22+
23+
# run cpack -G DEB to create .deb

gallery/lines_bars_and_markers/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ add_demo(fill_between_demo fill_between_demo.cpp)
66
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)
9+
add_demo(errorbar_limits_simple errorbar_limits_simple.cpp)
10+
add_demo(errorbar_subsample errorbar_subsample.cpp)
911

10-
# TODO: macro for this!
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
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
@@ -18,6 +19,8 @@ add_custom_target(lines_bars_and_markers
1819
COMMAND fill_betweenx_demo
1920
COMMAND scatter_with_legend
2021
COMMAND scatter_hist
22+
COMMAND errorbar_limits_simple
23+
COMMAND errorbar_subsample
2124
COMMENT "running lines_bars_and_markers"
2225
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../images"
2326
)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// example
2+
// https://matplotlib.org/stable/gallery/lines_bars_and_markers/errorbar_limits_simple.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+
py::scoped_interpreter guard{};
21+
auto plt = pyplot::import();
22+
auto fig = plt.figure();
23+
auto x_ = xt::arange(0.0, 10.0, 1.0);
24+
auto y_ = 2.5 * xt::sin(x_ / 20 * M_PI);
25+
auto y1_ = y_ + 1.0, y2_ = y_ + 2.0, y3_ = y_ + 3.0;
26+
auto yerr_ = xt::linspace(0.05, 0.2, 10);
27+
vector<double> x(x_.begin(), x_.end()), y(y_.begin(), y_.end()),
28+
yerr(yerr_.begin(), yerr_.end()), y3(y3_.begin(), y3_.end()),
29+
y2(y2_.begin(), y2_.end()), y1(y1_.begin(), y1_.end());
30+
plt.errorbar(Args(x, y3),
31+
Kwargs("yerr"_a = yerr, "label"_a = "both limits (default)"));
32+
plt.errorbar(Args(x, y2), Kwargs("yerr"_a = yerr, "uplims"_a = true,
33+
"label"_a = "uplims=True"));
34+
plt.errorbar(Args(x, y1),
35+
Kwargs("yerr"_a = yerr, "uplims"_a = true, "lolims"_a = true,
36+
"label"_a = "uplims=True, lolims=True"));
37+
38+
vector<bool> upperlimits, lowerlimits;
39+
for (auto i : {0, 1, 2, 3, 4}) {
40+
upperlimits.push_back(true);
41+
upperlimits.push_back(false);
42+
lowerlimits.push_back(false);
43+
lowerlimits.push_back(true);
44+
}
45+
plt.errorbar(Args(x, y), Kwargs("yerr"_a = yerr, "uplims"_a = upperlimits,
46+
"lolims"_a = lowerlimits,
47+
"label"_a = "subsets of uplims and lolims"));
48+
plt.legend(Args(), Kwargs("loc"_a = "lower right"));
49+
#if USE_GUI
50+
plt.show();
51+
#else
52+
plt.savefig(Args("errorbar_limits_simple.png"));
53+
#endif
54+
}
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+
}

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(t_.shape()[0]), zuplims, zlolims;
32+
std::iota(i.begin(), i.end(), t_.shape()[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+
}

gallery/shapes_and_collections/patch_collection.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ int main() {
6060
py::list colors = py::cast(colors_);
6161
auto p = collections::PatchCollection(Args(patches), Kwargs("alpha"_a = 0.4));
6262
p.set_array(Args(colors));
63+
// NOTE: error in python3.6.9 ?
6364
ax.add_collection(Args(p.unwrap()));
6465
fig.colorbar(Args(p.unwrap()), Kwargs("ax"_a = ax.unwrap()));
6566
#if USE_GUI

gallery/statistics/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
add_demo(hist hist.cpp)
2+
add_demo(errorbar errorbar.cpp)
23

34
add_custom_target(statitics
4-
DEPENDS hist
5+
DEPENDS hist errorbar
56
COMMAND hist
7+
COMMAND errorbar
68
COMMENT "running hist"
79
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../images"
810
)

gallery/statistics/errorbar.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// example from https://matplotlib.org/stable/gallery/statistics/errorbar.html
2+
3+
#include <pybind11/embed.h>
4+
#include <pybind11/stl.h>
5+
6+
#include <matplotlibcpp17/pyplot.h>
7+
8+
#include <xtensor/xrandom.hpp>
9+
10+
#include <random>
11+
12+
namespace py = pybind11;
13+
using namespace py::literals;
14+
using namespace std;
15+
using namespace matplotlibcpp17;
16+
17+
int main() {
18+
py::scoped_interpreter guard{};
19+
auto plt = pyplot::import();
20+
auto x_ = xt::arange(0.1, 4.0, 0.5);
21+
auto y_ = xt::exp(-x_);
22+
vector<double> x(x_.begin(), x_.end()), y(y_.begin(), y_.end());
23+
24+
auto [fig, ax] = plt.subplots();
25+
ax.errorbar(Args(x, y), Kwargs("xerr"_a = 0.2, "yerr"_a = 0.4));
26+
#if USE_GUI
27+
plt.show();
28+
#else
29+
plt.savefig(Args("erorrbar.png"));
30+
#endif
31+
}

0 commit comments

Comments
 (0)