Skip to content

Commit

Permalink
issues/81: rows and cols deduced from py array
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Feb 16, 2023
1 parent 9feb9b2 commit 0002a33
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
3 changes: 1 addition & 2 deletions bindings/imgui_bundle/demos_python/demo_implot.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ def demo_heatmap():
xx = np.outer(x, x)
yy = np.sinc(xx)

rows, cols = yy.shape
n_ticks = 5
x_ticks = [str(x) for x in np.linspace(-4, 4, n_ticks)]
y_ticks = x_ticks
Expand All @@ -108,7 +107,7 @@ def demo_heatmap():
implot.setup_axes(None,None, axis_flags, axis_flags)
implot.setup_axis_ticks(implot.ImAxis_.x1, 0, 1,n_ticks, x_ticks, False)
implot.setup_axis_ticks(implot.ImAxis_.y1, 0, 1, n_ticks, y_ticks, False)
implot.plot_heatmap("##heatmap", yy, rows, cols, yy.min(), yy.max(), None, [0,1], [1,0], 0)
implot.plot_heatmap("##heatmap", yy, yy.min(), yy.max(), None, [0,1], [1,0], 0)
implot.end_plot()
imgui.end_group()
imgui.same_line()
Expand Down
13 changes: 9 additions & 4 deletions external/implot/bindings/pybind_implot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2300,9 +2300,9 @@ void py_init_module_implot(py::module& m)

// PlotHeatmap, cf https://github.com/pthom/imgui_bundle/issues/81
m.def("plot_heatmap",
[](const char * label_id, const py::array & values, int rows, int cols, double scale_min = 0, double scale_max=0, const char * label_fmt="%.1f", const ImPlotPoint& bounds_min=ImPlotPoint(0,0), const ImPlotPoint& bounds_max=ImPlotPoint(1,1), ImPlotHeatmapFlags flags = 0)
[](const char * label_id, const py::array & values, double scale_min = 0, double scale_max=0, const char * label_fmt="%.1f", const ImPlotPoint& bounds_min=ImPlotPoint(0,0), const ImPlotPoint& bounds_max=ImPlotPoint(1,1), ImPlotHeatmapFlags flags = 0)
{
auto PlotHeatmap_adapt_c_buffers = [](const char * label_id, const py::array & values, int rows, int cols, double scale_min = 0, double scale_max=0, const char * label_fmt="%.1f", const ImPlotPoint& bounds_min=ImPlotPoint(0,0), const ImPlotPoint& bounds_max=ImPlotPoint(1,1), ImPlotHeatmapFlags flags = 0)
auto PlotHeatmap_adapt_c_buffers = [](const char * label_id, const py::array & values, double scale_min = 0, double scale_max=0, const char * label_fmt="%.1f", const ImPlotPoint& bounds_min=ImPlotPoint(0,0), const ImPlotPoint& bounds_max=ImPlotPoint(1,1), ImPlotHeatmapFlags flags = 0)
{
// convert py::array to C standard buffer (const)
const void * values_from_pyarray = values.data();
Expand All @@ -2314,6 +2314,11 @@ void py_init_module_implot(py::module& m)
using np_uint_l = uint64_t;
using np_int_l = int64_t;
#endif
if (values.ndim() != 2)
throw std::runtime_error("plot_heatmap expects a numpy bidimensional array ");
int rows = static_cast<int>(values.shape()[0]);
int cols = static_cast<int>(values.shape()[1]);

// call the correct template version by casting
char values_type = values.dtype().char_();
if (values_type == 'B')
Expand Down Expand Up @@ -2345,9 +2350,9 @@ void py_init_module_implot(py::module& m)
throw std::runtime_error(std::string("Bad array type ('") + values_type + "') for param values");
};

PlotHeatmap_adapt_c_buffers(label_id, values, rows, cols, scale_min, scale_max, label_fmt, bounds_min, bounds_max, flags);
PlotHeatmap_adapt_c_buffers(label_id, values, scale_min, scale_max, label_fmt, bounds_min, bounds_max, flags);
},
py::arg("label_id"), py::arg("values"), py::arg("rows"), py::arg("cols"), py::arg("scale_min")= 0 , py::arg("scale_max") = 0, py::arg("label_fmt")="%.1f", py::arg("bounds_min")=ImPlotPoint(0,0), py::arg("bounds_max")=ImPlotPoint(1,1), py::arg("flags")=0
py::arg("label_id"), py::arg("values"), py::arg("scale_min")= 0 , py::arg("scale_max") = 0, py::arg("label_fmt")="%.1f", py::arg("bounds_min")=ImPlotPoint(0,0), py::arg("bounds_max")=ImPlotPoint(1,1), py::arg("flags")=0
);


Expand Down

0 comments on commit 0002a33

Please sign in to comment.