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

Basic Heat map


In [2]:
auto data = randn(10, 15);

In [3]:
xpl::linear_scale xs, ys;
xpl::color_scale cs;

In [4]:
xpl::grid_heat_map grid_map(data, xs, ys, cs);

In [5]:
xpl::figure fig1;
fig1.add_mark(grid_map);
fig1.padding_y = 0.;
fig1.display()


Heat map with axes


In [6]:
auto ys_r = ys;
ys_r.reverse = true;

In [7]:
xpl::axis ax_x(xs), ax_y(ys_r);
ax_y.orientation = "vertical";

In [8]:
xpl::grid_heat_map grid_map_r(data, xs, ys_r, cs);

In [9]:
xpl::figure fig2;
fig2.add_mark(grid_map_r);
fig2.add_axis(ax_x);
fig2.add_axis(ax_y);
fig2.padding_y = 0.;
fig2.display()


Non Uniform Heat map


In [10]:
auto data_s = randn(10, 10);

In [11]:
xpl::linear_scale xs_nu, ys_nu;
ys_nu.reverse = true;
xpl::color_scale cs_nu;

In [12]:
xpl::axis ax_x_nu(xs_nu), ax_y_nu(ys_nu);
ax_y_nu.orientation = "vertical";

In [13]:
std::vector<double> row_data{0, 1, 2, 3, 4, 6, 7, 8, 9, 10};
std::vector<double> column_data{10, 11, 12, 13, 14, 15, 16, 17, 18, 19};

In [14]:
xpl::grid_heat_map grid_map_nu(row_data, column_data, data_s, xs_nu, ys_nu, cs_nu);

In [15]:
xpl::figure fig3;
fig3.add_mark(grid_map_nu);
fig3.add_axis(ax_x_nu);
fig3.add_axis(ax_y_nu);
fig3.padding_y = 0.;
fig3.display()


Alignment of the data with respect to the grid

For a N-by-N matrix, N+1 points along the row or the column are assumed to be end points.


In [16]:
xpl::linear_scale xs_a, ys_a;
xpl::color_scale cs_a;
ys_a.reverse = true;

In [17]:
xpl::axis ax_x_a(xs_a), ax_y_a(ys_a);
ax_y_a.orientation = "vertical";

In [18]:
std::vector<double> row_data_a{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::vector<double> column_data_a{10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20};

In [19]:
xpl::grid_heat_map grid_map_a(row_data_a, column_data_a, data_s, xs_a, ys_a, cs_a);

In [20]:
xpl::figure fig4;
fig4.add_mark(grid_map_a);
fig4.add_axis(ax_x_a);
fig4.add_axis(ax_y_a);
fig4.padding_y = 0.;
fig4.display()


By default, for N points along any dimension, data aligns to the start of the rectangles in the grid. The grid extends infinitely in the other direction. By default, the grid extends infintely towards the bottom and the right.


In [21]:
xpl::linear_scale xs_extend, ys_extend;
xpl::color_scale cs_extend;
ys_extend.reverse = true;
ys_extend.max = 15;

In [22]:
xpl::axis ax_x_extend(xs_extend), ax_y_extend(ys_extend);
ax_y_extend.orientation = "vertical";

In [23]:
std::vector<double> row_data_extend{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::vector<double> column_data_extend{10, 11, 12, 13, 14, 15, 16, 17, 18, 19};

In [24]:
xpl::grid_heat_map grid_map_extend(row_data_extend, column_data_extend, data_s, xs_extend, ys_extend, cs_extend);

In [25]:
xpl::figure fig5;
fig5.add_mark(grid_map_extend);
fig5.add_axis(ax_x_extend);
fig5.add_axis(ax_y_extend);
fig5.padding_y = 0.;
fig5.display()


By changing the row_align and column_align properties, the grid can extend in the opposite direction


In [26]:
xpl::linear_scale xs_extend2, ys_extend2;
xpl::color_scale cs_extend2;
ys_extend2.reverse = true;
ys_extend2.min = -5;
ys_extend2.max = 15;

In [27]:
xpl::axis ax_x_extend2(xs_extend2), ax_y_extend2(ys_extend2);
ax_y_extend2.orientation = "vertical";

In [28]:
xpl::grid_heat_map grid_map_extend2(row_data_extend, column_data_extend, data_s, xs_extend2, ys_extend2, cs_extend2);
grid_map_extend2.row_align = "end";

In [29]:
xpl::figure fig6;
fig6.add_mark(grid_map_extend2);
fig6.add_axis(ax_x_extend2);
fig6.add_axis(ax_y_extend2);
fig6.padding_y = 0.;
fig6.display()


For N+1 points on any direction, the grid extends infintely in both directions


In [30]:
xpl::linear_scale xs_extend3, ys_extend3;
xpl::color_scale cs_extend3;
ys_extend3.reverse = true;
ys_extend3.min = -5;
ys_extend3.max = 15;

In [31]:
xpl::axis ax_x_extend3(xs_extend3), ax_y_extend3(ys_extend3);
ax_y_extend3.orientation = "vertical";

In [32]:
std::vector<double> row_data_extend3{0, 1, 2, 3, 4, 5, 6, 7, 8};
std::vector<double> column_data_extend3{10, 11, 12, 13, 14, 15, 16, 17, 18, 19};

In [33]:
xpl::grid_heat_map grid_map_extend3(row_data_extend3, column_data_extend3, data_s, xs_extend3, ys_extend3, cs_extend3);
grid_map_extend3.row_align = "end";

In [34]:
xpl::figure fig7;
fig7.add_mark(grid_map_extend3);
fig7.add_axis(ax_x_extend3);
fig7.add_axis(ax_y_extend3);
fig7.padding_y = 0.;
fig7.display()


Changing opacity and stroke


In [35]:
xpl::linear_scale xs_o, ys_o;
xpl::color_scale cs_o;

In [36]:
xpl::grid_heat_map grid_map_o(data_s, xs_o, ys_o, cs_o);
grid_map_o.opacity = .3;
grid_map_o.stroke = "white";

In [37]:
xpl::figure fig8;
fig8.add_mark(grid_map_o);
fig8.padding_y = 0.;
fig8.display()


Selections on the grid map

Selection on the GridHeatMap works similar to excel. Clicking on a cell selects the cell, and deselects the previous selection. Using the Ctrl key allows multiple cells to be selected, while the Shift key selects the range from the last cell in the selection to the current cell.


In [38]:
xpl::linear_scale xs_s, ys_s;
xpl::color_scale cs_s;

In [39]:
xpl::grid_heat_map grid_map_s(data_s, xs_s, ys_s, cs_s);

In [40]:
grid_map_s.selected_style = ::xeus::xjson::parse(R"({"opacity": "1.0"})");
grid_map_s.unselected_style = ::xeus::xjson::parse(R"({"opacity": "0.4"})");

In [41]:
grid_map_s.interactions = ::xeus::xjson::parse(R"({"click": "select"})");

In [42]:
xpl::figure fig9;
fig9.add_mark(grid_map_s);
fig9.padding_y = 0.;
fig9.display()



In [43]:
for(auto& v: grid_map_s.selected())
{
    std::cout << v[0] << " " << v[1] << "\n";
}

The selected trait of a GridHeatMap contains a list of lists, with each sub-list containing the row and column index of a selected cell.


In [44]:
// TODO

In [ ]: