[python] matplotlib: implement "plot(y)'

This commit is contained in:
Émilie Feral
2020-03-27 14:43:32 +01:00
parent dc58b9692f
commit bf7c3b1aab
3 changed files with 21 additions and 5 deletions

View File

@@ -269,12 +269,28 @@ mp_obj_t modpyplot_scatter(mp_obj_t x, mp_obj_t y) {
return mp_const_none;
}
mp_obj_t modpyplot_plot(mp_obj_t x, mp_obj_t y) {
/* plot(x, y) plots the curve (x, y)
* plot(y) plots the curve x as index array ([0,1,2...],y)
* */
mp_obj_t modpyplot_plot(size_t n_args, const mp_obj_t *args) {
assert(sPlotStore != nullptr);
// Input parameter validation
mp_obj_t * xItems, * yItems;
size_t length = extractAndValidatePlotInput(x, y, &xItems, &yItems);
size_t length;
if (n_args == 1) {
mp_obj_get_array(args[0], &length, &yItems);
// Create the default xItems: [0, 1, 2,...]
mp_obj_t x = mp_obj_new_list(length, nullptr);
for (int i = 0; i < length; i++) {
mp_obj_list_store(x, mp_obj_new_int(i), mp_obj_new_float((float)i));
}
mp_obj_get_array(x, &length, &xItems);
} else {
assert(n_args == 2);
length = extractAndValidatePlotInput(args[0], args[1], &xItems, &yItems);
}
KDColor color = Palette::nextDataColor(&paletteIndex);
for (size_t i=0; i<length-1; i++) {

View File

@@ -9,7 +9,7 @@ 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);
mp_obj_t modpyplot_hist(size_t n_args, const mp_obj_t *args);
mp_obj_t modpyplot_plot(mp_obj_t x, mp_obj_t y);
mp_obj_t modpyplot_plot(size_t n_args, const mp_obj_t *args);
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 modpyplot_show();

View File

@@ -6,7 +6,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modpyplot_axis_obj, 0, 1, modpyplot_a
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modpyplot_bar_obj, 2, 4, modpyplot_bar);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modpyplot_grid_obj, 0, 1, modpyplot_grid);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modpyplot_hist_obj, 1, 2, modpyplot_hist);
STATIC MP_DEFINE_CONST_FUN_OBJ_2(modpyplot_plot_obj, modpyplot_plot);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modpyplot_plot_obj, 1, 2, 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);
STATIC MP_DEFINE_CONST_FUN_OBJ_3(modpyplot_text_obj, modpyplot_text);