Skip to content

Commit

Permalink
added lines_bars_and_markers/step_demo.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
soblin committed Mar 13, 2022
1 parent bacbb4f commit f55c791
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
4 changes: 3 additions & 1 deletion gallery/lines_bars_and_markers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ add_demo(scatter_with_legend scatter_with_legend.cpp)
add_demo(scatter_hist scatter_hist.cpp)
add_demo(errorbar_limits_simple errorbar_limits_simple.cpp)
add_demo(errorbar_subsample errorbar_subsample.cpp)
add_demo(step_demo step_demo.cpp)

add_custom_target(lines_bars_and_markers
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
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 step_demo
COMMAND bar_label_demo
COMMAND fill
COMMAND simple_plot
Expand All @@ -21,6 +22,7 @@ add_custom_target(lines_bars_and_markers
COMMAND scatter_hist
COMMAND errorbar_limits_simple
COMMAND errorbar_subsample
COMMAND step_demo
COMMENT "running lines_bars_and_markers"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/../images"
)
37 changes: 37 additions & 0 deletions gallery/lines_bars_and_markers/step_demo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// example from
// https://matplotlib.org/stable/gallery/lines_bars_and_markers/step_demo.html

#include <matplotlibcpp17/pyplot.h>

#include <xtensor/xbuilder.hpp>
#include <xtensor/xmath.hpp>

#include <vector>

using namespace std;
using namespace matplotlibcpp17;

int main() {
py::scoped_interpreter guard{};
auto plt = pyplot::import();

auto x_ = xt::arange(14) * 1.0;
auto y_ = xt::sin(x_ / 2.0);
auto y1_ = y_ + 1.0, y2_ = y_ + 2.0;
vector<double> x(x_.begin(), x_.end()), y(y_.begin(), y_.end()),
y1(y1_.begin(), y1_.end()), y2(y2_.begin(), y2_.end());

plt.step(Args(x, y2), Kwargs("label"_a = "pre (default)"));
plt.plot(Args(x, y2, "o--"), Kwargs("color"_a = "grey", "alpha"_a = 0.3));

plt.step(Args(x, y1), Kwargs("where"_a = "mid", "label"_a = "mid"));
plt.plot(Args(x, y1, "o--"), Kwargs("color"_a = "grey", "alpha"_a = 0.3));

plt.step(Args(x, y), Kwargs("where"_a = "post", "label"_a = "post"));
plt.plot(Args(x, y, "o--"), Kwargs("color"_a = "grey", "alpha"_a = 0.3));

plt.grid(Args(), Kwargs("axis"_a = "x", "color"_a = "0.95"));
plt.legend(Args(), Kwargs("title"_a = "Parameter where:"));
plt.title(Args("plt.step(where...)"));
plt.show();
}
39 changes: 39 additions & 0 deletions include/matplotlibcpp17/pyplot.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ struct DECL_STRUCT_ATTR PyPlot {
figure::Figure gcf(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());

// grid
pybind11::object grid(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());

// legend
pybind11::object legend(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
Expand Down Expand Up @@ -93,6 +97,10 @@ struct DECL_STRUCT_ATTR PyPlot {
pybind11::object show(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());

// step
pybind11::object step(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());

// subplot
axes::Axes subplot(const pybind11::dict &kwargs = pybind11::dict());
axes::Axes subplot(int cri);
Expand All @@ -103,6 +111,10 @@ struct DECL_STRUCT_ATTR PyPlot {
std::tuple<figure::Figure, std::vector<axes::Axes>>
subplots(int r, int c, const pybind11::dict &kwargs = pybind11::dict());

// title
pybind11::object title(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());

// xlabel
pybind11::object xlabel(const pybind11::tuple &args = pybind11::tuple(),
const pybind11::dict &kwargs = pybind11::dict());
Expand Down Expand Up @@ -130,15 +142,18 @@ struct DECL_STRUCT_ATTR PyPlot {
LOAD_FUNC_ATTR(figure, mod);
LOAD_FUNC_ATTR(gca, mod);
LOAD_FUNC_ATTR(gcf, mod);
LOAD_FUNC_ATTR(grid, mod);
LOAD_FUNC_ATTR(legend, mod);
LOAD_FUNC_ATTR(pause, mod);
LOAD_FUNC_ATTR(plot, mod);
LOAD_FUNC_ATTR(quiver, mod);
LOAD_FUNC_ATTR(savefig, mod);
LOAD_FUNC_ATTR(scatter, mod);
LOAD_FUNC_ATTR(show, mod);
LOAD_FUNC_ATTR(step, mod);
LOAD_FUNC_ATTR(subplot, mod);
LOAD_FUNC_ATTR(subplots, mod);
LOAD_FUNC_ATTR(title, mod);
LOAD_FUNC_ATTR(xlabel, mod);
LOAD_FUNC_ATTR(xlim, mod);
LOAD_FUNC_ATTR(ylabel, mod);
Expand All @@ -154,15 +169,18 @@ struct DECL_STRUCT_ATTR PyPlot {
pybind11::object figure_attr;
pybind11::object gca_attr;
pybind11::object gcf_attr;
pybind11::object grid_attr;
pybind11::object legend_attr;
pybind11::object pause_attr;
pybind11::object plot_attr;
pybind11::object quiver_attr;
pybind11::object savefig_attr;
pybind11::object scatter_attr;
pybind11::object show_attr;
pybind11::object step_attr;
pybind11::object subplot_attr;
pybind11::object subplots_attr;
pybind11::object title_attr;
pybind11::object xlabel_attr;
pybind11::object xlim_attr;
pybind11::object ylabel_attr;
Expand Down Expand Up @@ -233,6 +251,13 @@ figure::Figure PyPlot::gcf(const pybind11::tuple &args,
return figure::Figure(obj);
}

// grid
pybind11::object PyPlot::grid(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object obj = grid_attr(*args, **kwargs);
return obj;
}

// legend
pybind11::object PyPlot::legend(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
Expand Down Expand Up @@ -282,6 +307,13 @@ pybind11::object PyPlot::show(const pybind11::tuple &args,
return ret;
}

// step
pybind11::object PyPlot::step(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object ret = step_attr(*args, **kwargs);
return ret;
}

// subplot
axes::Axes PyPlot::subplot(const pybind11::dict &kwargs) {
return axes::Axes(subplot_attr(**kwargs));
Expand Down Expand Up @@ -331,6 +363,13 @@ PyPlot::subplots(int r, int c, const pybind11::dict &kwargs) {
return {figure, axes};
}

// title
pybind11::object PyPlot::title(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
pybind11::object ret = title_attr(*args, **kwargs);
return ret;
}

// xlabel
pybind11::object PyPlot::xlabel(const pybind11::tuple &args,
const pybind11::dict &kwargs) {
Expand Down

0 comments on commit f55c791

Please sign in to comment.