diff --git a/python/port/genhdr/qstrdefs.in.h b/python/port/genhdr/qstrdefs.in.h index 71a87feee..19b007e12 100644 --- a/python/port/genhdr/qstrdefs.in.h +++ b/python/port/genhdr/qstrdefs.in.h @@ -384,6 +384,7 @@ Q(axis) Q(bar) Q(grid) Q(grid) +Q(head_width) Q(hist) Q(plot) Q(matplotlib) diff --git a/python/port/mod/matplotlib/pyplot/modpyplot.cpp b/python/port/mod/matplotlib/pyplot/modpyplot.cpp index 1a144fbcb..7685611f0 100644 --- a/python/port/mod/matplotlib/pyplot/modpyplot.cpp +++ b/python/port/mod/matplotlib/pyplot/modpyplot.cpp @@ -50,7 +50,18 @@ static size_t extractArgumentAndValidateSize(mp_obj_t arg, size_t requiredlength return itemLength; } +// Get color from keyword arguments if possible + +KDColor colorFromKeywordArgument(mp_map_elem_t * elemColor) { + if (elemColor != nullptr) { + return MicroPython::Color::Parse(elemColor->value); + } else { + return Palette::nextDataColor(&paletteIndex); + } +} + // Get color from arguments if possible +// TODO DELETE AFTER REPLACEMENT KDColor colorFromOptionalArgumentAtIndex(size_t n_args, const mp_obj_t * args, size_t colorIndex) { if (n_args > colorIndex) { @@ -89,20 +100,25 @@ void modpyplot_flush_used_heap() { } } -/* arrow(x,y,dx,dy, head_width, color) +/* arrow(x,y,dx,dy, KW : head_width, color) * x, y, dx, dy scalars * */ -mp_obj_t modpyplot_arrow(size_t n_args, const mp_obj_t *args) { +mp_obj_t modpyplot_arrow(size_t n_args, const mp_obj_t *args, mp_map_t* kw_args) { assert(n_args >= 4); assert(sPlotStore != nullptr); sPlotStore->setShow(true); - mp_obj_t arrowWidth = mp_obj_new_float(0.003); // Default value - if (n_args >= 5) { - arrowWidth = args[4]; - } - KDColor color = colorFromOptionalArgumentAtIndex(n_args, args, 5); + mp_map_elem_t * elem; + // Setting arrow width + elem = mp_map_lookup(kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_head_width), MP_MAP_LOOKUP); + mp_obj_t arrowWidth = (elem == nullptr) ? mp_obj_new_float(0.003) : elem->value; + + // Setting arrow color + elem = mp_map_lookup(kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_color), MP_MAP_LOOKUP); + KDColor color = colorFromKeywordArgument(elem); + + // Adding the object to the plot sPlotStore->addSegment(args[0], args[1], mp_obj_new_float(mp_obj_get_float(args[0]) + mp_obj_get_float(args[2])), mp_obj_new_float(mp_obj_get_float(args[1]) + mp_obj_get_float(args[3])), color, arrowWidth); return mp_const_none; } diff --git a/python/port/mod/matplotlib/pyplot/modpyplot.h b/python/port/mod/matplotlib/pyplot/modpyplot.h index a32e0905e..d28e8f4e1 100644 --- a/python/port/mod/matplotlib/pyplot/modpyplot.h +++ b/python/port/mod/matplotlib/pyplot/modpyplot.h @@ -4,7 +4,7 @@ mp_obj_t modpyplot___init__(); void modpyplot_gc_collect(); void modpyplot_flush_used_heap(); -mp_obj_t modpyplot_arrow(size_t n_args, const mp_obj_t *args); +mp_obj_t modpyplot_arrow(size_t n_args, const mp_obj_t *args, mp_map_t* kw_args); mp_obj_t modpyplot_axis(size_t n_args, const mp_obj_t *args); mp_obj_t modpyplot_bar(size_t n_args, const mp_obj_t *args); mp_obj_t modpyplot_grid(size_t n_args, const mp_obj_t *args); diff --git a/python/port/mod/matplotlib/pyplot/modpyplot_table.c b/python/port/mod/matplotlib/pyplot/modpyplot_table.c index c841bf284..3a398a05c 100644 --- a/python/port/mod/matplotlib/pyplot/modpyplot_table.c +++ b/python/port/mod/matplotlib/pyplot/modpyplot_table.c @@ -1,7 +1,7 @@ #include "modpyplot.h" STATIC MP_DEFINE_CONST_FUN_OBJ_0(modpyplot___init___obj, modpyplot___init__); -STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modpyplot_arrow_obj, 4, 6, modpyplot_arrow); +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(modpyplot_arrow_obj, 4, modpyplot_arrow); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modpyplot_axis_obj, 0, 1, modpyplot_axis); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modpyplot_bar_obj, 2, 5, modpyplot_bar); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modpyplot_grid_obj, 0, 1, modpyplot_grid);