xplot

  • Interactive plotting for Jupyter notebook
  • Based on bqplot javascript side

In [ ]:
#include "./random.hpp"
#include "xwidgets/xlabel.hpp"
#include "xplot/xfigure.hpp"
#include "xplot/xmarks.hpp"
#include "xplot/xaxes.hpp"

#include "xtensor/xtensor.hpp"
#include "xtensor/xrandom.hpp"

std::size_t size = 20;
// xt::xtensor<double, 1> x_data = xt::xtensor<double, 1>::from_shape({size});
std::vector<double> x_data(size);
std::iota(x_data.begin(), x_data.end(), 0);

std::vector<double> y_data = randn(size);
// xt::xtensor<double, 1> y_data(xt::random::randn<double>({size}));
xpl::linear_scale sx, sy;

In [ ]:
auto ax_x = xpl::axis_generator(sx)
    .label("x")
    .finalize();

auto ax_y = xpl::axis_generator(sy)
    .label("y")
    .orientation("vertical")
    .side("left")
    .finalize();

In [ ]:
auto scatter1 = xpl::scatter_generator(sx, sy)
   .x(x_data)
   .y(y_data)
   .enable_move(true)
   .finalize();

In [ ]:
xpl::lines line(sx, sy);
line.x = std::vector<double>({0, double(size)});
line.y = std::vector<double>({5, 8});

In [ ]:
auto fig1 = xpl::figure_generator()
    .padding_x(0.025)
    .padding_y(0.025)
    .finalize();

fig1.add_mark(scatter1);
fig1.add_mark(line);
fig1.add_axis(ax_x);
fig1.add_axis(ax_y);
fig1

In [ ]:
scatter1.colors = std::vector<xtl::xoptional<std::string>>{"red"};

In [ ]:
scatter1.marker = "cross";

In [ ]:
scatter1.default_opacities = std::vector<double>({0.3, 0.5, 1.});

In [ ]:
xw::label label;
label

In [ ]:
inline void info(const ::xeus::xjson& content) {
    label.value = std::to_string(static_cast<double>(content["point"]["x"])) 
                + " " 
                + std::to_string(static_cast<double>(content["point"]["y"]));
    
    const std::vector<double>& ref = scatter1.y();
    double mean = std::accumulate(ref.begin(), ref.end(), 0.0) / double(ref.size());
    line.y = std::vector<double>({mean, mean});
};

In [ ]:
scatter1.on_drag(info);
scatter1.on_drag_start(info);
scatter1.on_drag_end(info);

In [ ]:
fig1

In [ ]: