diff --git a/python/port/genhdr/qstrdefs.in.h b/python/port/genhdr/qstrdefs.in.h index 6d5a6f0fe..8168d8e69 100644 --- a/python/port/genhdr/qstrdefs.in.h +++ b/python/port/genhdr/qstrdefs.in.h @@ -75,6 +75,7 @@ Q(set_pixel) // Matplotlib QSTRs Q(axis) +Q(grid) Q(plot) Q(pyplot) Q(scatter) diff --git a/python/port/mod/matplotlib/modpyplot.cpp b/python/port/mod/matplotlib/modpyplot.cpp index 8aaaa0528..cb572ad08 100644 --- a/python/port/mod/matplotlib/modpyplot.cpp +++ b/python/port/mod/matplotlib/modpyplot.cpp @@ -8,7 +8,7 @@ extern "C" { Matplotlib::PlotStore * sPlotStore = nullptr; Matplotlib::PlotController * sPlotController = nullptr; -static int paletteIndex = 0; // FIXME: Needs to be reset at some point +static int paletteIndex = 0; // Internal functions @@ -18,6 +18,7 @@ mp_obj_t modpyplot___init__() { sPlotStore = &plotStore; sPlotController = &plotController; sPlotStore->flush(); + paletteIndex = 0; return mp_const_none; } @@ -41,7 +42,7 @@ mp_obj_t modpyplot_axis(mp_obj_t arg) { #warning Use mp_obj_is_bool when upgrading uPy if (mp_obj_is_type(arg, &mp_type_bool)) { - sPlotStore->setGrid(mp_obj_is_true(arg)); + sPlotStore->setAxesRequested(mp_obj_is_true(arg)); } else { mp_obj_t * items; mp_obj_get_array_fixed_n(arg, 4, &items); @@ -60,6 +61,15 @@ mp_obj_t modpyplot_axis(mp_obj_t arg) { return mp_obj_new_tuple(4, coords); } +mp_obj_t modpyplot_grid(mp_obj_t b) { + if (mp_obj_is_type(b, &mp_type_bool)) { + sPlotStore->setGridRequested(mp_obj_is_true(b)); + } else { + sPlotStore->setGridRequested(!sPlotStore->gridRequested()); + } + return mp_const_none; +} + mp_obj_t modpyplot_scatter(mp_obj_t x, mp_obj_t y) { assert(sPlotStore != nullptr); diff --git a/python/port/mod/matplotlib/modpyplot.h b/python/port/mod/matplotlib/modpyplot.h index 8dad90017..9652a637a 100644 --- a/python/port/mod/matplotlib/modpyplot.h +++ b/python/port/mod/matplotlib/modpyplot.h @@ -4,10 +4,11 @@ mp_obj_t modpyplot___init__(); void modpyplot_gc_collect(); mp_obj_t modpyplot_axis(mp_obj_t arg); +mp_obj_t modpyplot_grid(mp_obj_t b); mp_obj_t modpyplot_plot(mp_obj_t x, mp_obj_t y); mp_obj_t modpyplot_scatter(mp_obj_t x, mp_obj_t y); mp_obj_t modpyplot_text(mp_obj_t x, mp_obj_t y, mp_obj_t s); -//mp_obj_t grid(); +//mp_obj_t arrow(); //mp_obj_t bar(); //mp_obj_t hist(); mp_obj_t modpyplot_show(); diff --git a/python/port/mod/matplotlib/modpyplot_table.c b/python/port/mod/matplotlib/modpyplot_table.c index a4780152a..8300fb92c 100644 --- a/python/port/mod/matplotlib/modpyplot_table.c +++ b/python/port/mod/matplotlib/modpyplot_table.c @@ -2,6 +2,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(modpyplot___init___obj, modpyplot___init__); STATIC MP_DEFINE_CONST_FUN_OBJ_1(modpyplot_axis_obj, modpyplot_axis); +STATIC MP_DEFINE_CONST_FUN_OBJ_1(modpyplot_grid_obj, modpyplot_grid); STATIC MP_DEFINE_CONST_FUN_OBJ_2(modpyplot_plot_obj, modpyplot_plot); STATIC MP_DEFINE_CONST_FUN_OBJ_2(modpyplot_scatter_obj, modpyplot_scatter); STATIC MP_DEFINE_CONST_FUN_OBJ_0(modpyplot_show_obj, modpyplot_show); @@ -11,6 +12,7 @@ STATIC const mp_rom_map_elem_t modpyplot_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_pyplot) }, { MP_ROM_QSTR(MP_QSTR___init__), MP_ROM_PTR(&modpyplot___init___obj) }, { MP_ROM_QSTR(MP_QSTR_axis), MP_ROM_PTR(&modpyplot_axis_obj) }, + { MP_ROM_QSTR(MP_QSTR_grid), MP_ROM_PTR(&modpyplot_grid_obj) }, { MP_ROM_QSTR(MP_QSTR_plot), MP_ROM_PTR(&modpyplot_plot_obj) }, { MP_ROM_QSTR(MP_QSTR_scatter), MP_ROM_PTR(&modpyplot_scatter_obj) }, { MP_ROM_QSTR(MP_QSTR_show), MP_ROM_PTR(&modpyplot_show_obj) }, diff --git a/python/port/mod/matplotlib/plot_store.cpp b/python/port/mod/matplotlib/plot_store.cpp index 8c815843f..0f2cf3ccb 100644 --- a/python/port/mod/matplotlib/plot_store.cpp +++ b/python/port/mod/matplotlib/plot_store.cpp @@ -3,7 +3,8 @@ namespace Matplotlib { PlotStore::PlotStore() : Shared::InteractiveCurveViewRange(), - m_grid(false) + m_axesRequested(true), + m_gridRequested(false) { flush(); } diff --git a/python/port/mod/matplotlib/plot_store.h b/python/port/mod/matplotlib/plot_store.h index 32af94b55..8d16840f1 100644 --- a/python/port/mod/matplotlib/plot_store.h +++ b/python/port/mod/matplotlib/plot_store.h @@ -96,15 +96,18 @@ public: void addLabel(mp_obj_t x, mp_obj_t y, mp_obj_t string); Iterable> labels() { return Iterable>(m_labels); } - void setGrid(bool grid) { m_grid = grid; } - bool grid() { return m_grid; } + void setAxesRequested(bool b) { m_axesRequested = b; } + bool axesRequested() const { return m_axesRequested; } + + void setGridRequested(bool b) { m_gridRequested = b; } + bool gridRequested() const { return m_gridRequested; } private: mp_obj_t m_dots; // List of (x, y, color) mp_obj_t m_labels; // List of (x, y, string) mp_obj_t m_segments; // List of (x, y, dx, dy, style, color) mp_obj_t m_rects; // List of (x, y, w, h, color) - - bool m_grid; + bool m_axesRequested; + bool m_gridRequested; }; } diff --git a/python/port/mod/matplotlib/plot_view.cpp b/python/port/mod/matplotlib/plot_view.cpp index 4b72a7010..96aa2465f 100644 --- a/python/port/mod/matplotlib/plot_view.cpp +++ b/python/port/mod/matplotlib/plot_view.cpp @@ -4,12 +4,16 @@ namespace Matplotlib { void PlotView::drawRect(KDContext * ctx, KDRect rect) const { ctx->fillRect(rect, KDColorWhite); - if (m_store->grid()) { + + if (m_store->gridRequested()) { drawGrid(ctx, rect); } - drawAxes(ctx, rect); - drawLabelsAndGraduations(ctx, rect, Axis::Vertical, true); - drawLabelsAndGraduations(ctx, rect, Axis::Horizontal, true); + + if (m_store->axesRequested()) { + drawAxes(ctx, rect); + drawLabelsAndGraduations(ctx, rect, Axis::Vertical, true); + drawLabelsAndGraduations(ctx, rect, Axis::Horizontal, true); + } for (PlotStore::Dot dot : m_store->dots()) { drawDot(ctx, rect, dot.x(), dot.y(), dot.color()); @@ -32,7 +36,6 @@ void PlotView::drawRect(KDContext * ctx, KDRect rect) const { segment.color() ); } - } }