RE: Python Matplotl usage
mathplotl is one of the MicroPython modules I wrote for Numworks/Casio/TI Nspire CX and was ported by Cyrille to the Prime. Unfortunately the port was done very fast, and there a few bugs remaining, like the show() problem.
As for help, there is online help for many instructions if you press the Help key from the Commands menu from Python shell. matplotl commands were added last, and do not have online help. They emulate the simplest commands of the Python matplotl commands : a matplot command is translated to an Xcas equivalent. Then show() is supposed to display the collection of Xcas geometric objects but as far as I remember, there is no wait instruction and the display is overwritten. I would therefore suggest to try the following workaround: add after matplotl.show() a prime.eval instruction with a WAIT instruction inside. If that does not work I'm afraid you're stuck until a new firmware is published.
The source code of the GPL version (with a few fixes with respect to HP version) that runs inside Xcas follows.
Code:
/* MATPLOTL */
static mp_obj_t matplotl_show(size_t n_args, const mp_obj_t *args) { // Prime source code is different
const char * val=caseval("show()");
return mp_obj_new_str(val,strlen(val));
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(matplotl_show_obj, 0, 0, matplotl_show);
static mp_obj_t matplotl_clf(size_t n_args, const mp_obj_t *args) {
const char * val=caseval("erase()");
return mp_obj_new_str(val,strlen(val));
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(matplotl_clf_obj, 0, 0, matplotl_clf);
static mp_obj_t matplotl_axis(size_t n_args, const mp_obj_t *args) {
if (n_args==0)
turtle_ret("axis: Autoscale");
double *x=0; size_t n=0,m=0;
if (mp_array2doubletab(args[0],&x,&n,&m)){
char * ptr=printtab(x,n,m);
free(x);
char buf[strlen(ptr)+256];
sprintf(buf,"axis(%s):;",ptr);
free(ptr);
const char * val=caseval(buf);
return turtle_ret(val);
}
if (n_args==4){
double a=0,b=0,c=0,d=0;
if (mp_obj_is_float(args[0]))
a=mp_obj_get_float(args[0]);
if (MP_OBJ_IS_INT(args[0]))
a=mp_obj_get_int(args[0]);
if (mp_obj_is_float(args[1]))
b=mp_obj_get_float(args[1]);
if (MP_OBJ_IS_INT(args[1]))
b=mp_obj_get_int(args[1]);
if (mp_obj_is_float(args[2]))
c=mp_obj_get_float(args[2]);
if (MP_OBJ_IS_INT(args[2]))
c=mp_obj_get_int(args[2]);
if (mp_obj_is_float(args[3]))
d=mp_obj_get_float(args[3]);
if (MP_OBJ_IS_INT(args[3]))
d=mp_obj_get_int(args[3]);
if (a<b && c<d){
char buf[256]="";
#ifdef NUMWORKS
strcat(buf,"axis(");
strcat_double(buf,a);
strcat(buf,",");
strcat_double(buf,b);
strcat(buf,",");
strcat_double(buf,c);
strcat(buf,",");
strcat_double(buf,d);
strcat(buf,"):;");
#else
sprintf(buf,"axis(%.14g,%.14g,%.14g,%.14g):;",a,b,c,d);
#endif
const char * val=caseval(buf);
return turtle_ret(val);
}
}
return turtle_ret("axis: Bad argument type");
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(matplotl_axis_obj, 0, 4, matplotl_axis);
static mp_obj_t matplotl_text(size_t n_args, const mp_obj_t *args) {
double a=0,b=0;
if (mp_obj_is_float(args[0]))
a=mp_obj_get_float(args[0]);
if (MP_OBJ_IS_INT(args[0]))
a=mp_obj_get_int(args[0]);
if (mp_obj_is_float(args[1]))
b=mp_obj_get_float(args[1]);
if (MP_OBJ_IS_INT(args[1]))
b=mp_obj_get_int(args[1]);
if (MP_OBJ_IS_STR(args[2])){
char buf[256]="";
#ifdef NUMWORKS
strcat(buf,"legend(point(");
strcat_double(buf,a);
strcat(buf,",");
strcat_double(buf,b);
char buf2[64];
sprintf(buf2,"),%s):;",mp_obj_str_get_str(args[2]));
strcat(buf,buf2);
#else
sprintf(buf,"legend(point(%.14g,%.14g),%s):;",a,b,mp_obj_str_get_str(args[2]));
#endif
const char * val=caseval(buf);
return turtle_ret(val);
}
return turtle_ret("text: Bad argument type");
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(matplotl_text_obj, 3, 3, matplotl_text);
static mp_obj_t matplotl_plot(size_t n_args, const mp_obj_t *args) {
double *x=0,*y=0; size_t n=0,m=0,n1=0,m1=0;
int col=0;
if (n_args>=3)
col=mp_get_color(args[2]);
if (mp_array2doubletab(args[0],&x,&n,&m)){
char * ptrx=printtab(x,n,m);
free(x);
char bufx[strlen(ptrx)+1];
strcpy(bufx,ptrx);
free(ptrx);
if (mp_array2doubletab(args[1],&y,&n1,&m1)){
char * ptry=printtab(y,n1,m1);
free(y);
char bufy[strlen(ptry)+1];
strcpy(bufy,ptry);
free(ptry);
if (n1==n && m==0 && m1==0){
char buf2[strlen(bufx)+strlen(bufy)+256];
sprintf(buf2,"polygonplot(%s,%s,color=%i):;",bufx,bufy,col>=0?col:0);
const char * val=caseval(buf2);
return turtle_ret(val);
}
}
}
double xx=0,yy=0;
if (n_args>=2 && mp_int_float(args[0],&xx) && mp_int_float(args[1],&yy)){
char buf[512]="";
#ifdef NUMWORKS
strcat(buf,"legend(point(");
strcat_double(buf,xx);
strcat(buf,",");
strcat_double(buf,yy);
#else
sprintf(buf,"point(%.14g,%.14g",xx,yy);
#endif
if (col==-1)
sprintf(buf,",display=\"%s\"):;",translate_point_type(mp_obj_str_get_str(args[2])));
else
sprintf(buf,",color=%i):;",col);
const char * val=caseval(buf);
return turtle_ret(val);
}
return turtle_ret("polygonplot: Bad argument type");
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(matplotl_plot_obj, 2, 3, matplotl_plot);
static mp_obj_t matplotl_bar_hist(size_t n_args, const mp_obj_t *args,bool bar) {
double *x=0; size_t n=0,m=0;
double largeur=0.8;
if (mp_array2doubletab(args[0],&x,&n,&m)){
char * ptr=printtab(x,n,m);
free(x);
char buf[strlen(ptr)+256];
if (n_args==1)
sprintf(buf,bar?"barplot(%s):;":"histogram(%s):;",ptr);
else
sprintf(buf,bar?"barplot(%s":"histogram(%s",ptr);
free(ptr);
if (n_args>=2){
if (mp_obj_is_float(args[1]))
largeur=mp_obj_get_float(args[1]);
if (n_args==3 && mp_obj_is_float(args[2]))
largeur=mp_obj_get_float(args[2]);
if (mp_array2doubletab(args[1],&x,&n,&m)){
ptr=printtab(x,n,m);
free(x);
char buf2[strlen(buf)+strlen(ptr)+256];
#ifdef NUMWORKS
sprintf(buf2,"%s,%s,",buf,ptr);
strcat_double(buf2,largeur);
strcat(buf2,"):;");
#else
sprintf(buf2,"%s,%s,%.3g):;",buf,ptr,largeur);
#endif
free(ptr);
const char * val=caseval(buf2);
return turtle_ret(val);
}
char buf2[strlen(buf)+256];
#ifdef NUMWORKS
sprintf(buf2,"%s,",buf);
strcat_double(buf2,largeur);
strcat(buf2,"):;");
#else
sprintf(buf2,"%s,%.3g):;",buf,largeur);
#endif
const char * val=caseval(buf2);
return turtle_ret(val);
}
const char * val=caseval(buf);
return turtle_ret(val);
}
return turtle_ret(bar?"barplot: Bad argument type":"histogram: Bad argument type");
}
static mp_obj_t matplotl_bar(size_t n_args, const mp_obj_t *args) {
return matplotl_bar_hist(n_args,args,true);
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(matplotl_bar_obj, 1, 3, matplotl_bar);
static mp_obj_t matplotl_hist(size_t n_args, const mp_obj_t *args) {
return matplotl_bar_hist(n_args,args,false);
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(matplotl_hist_obj, 1, 3, matplotl_hist);
static mp_obj_t matplotl_boxplot(size_t n_args, const mp_obj_t *args) {
double *x=0; size_t n=0,m=0;
int col=0;
if (n_args==2)
col=mp_get_color(args[1]);
if (mp_array2doubletab(args[0],&x,&n,&m)){
char * ptr=printtab(x,n,m);
free(x);
char buf[strlen(ptr)+256];
sprintf(buf,"moustache(%s,color=%i):;",ptr,col>=0?col:0);
free(ptr);
const char * val=caseval(buf);
return turtle_ret(val);
}
return turtle_ret("moustache: Bad argument type");
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(matplotl_boxplot_obj, 1, 2, matplotl_boxplot);
static mp_obj_t matplotl_arrow(size_t n_args, const mp_obj_t *args) {
double a=0,b=0,c=0,d=0;
int col=0;
if (n_args==5)
col=mp_get_color(args[4]);
if (mp_int_float(args[0],&a) && mp_int_float(args[1],&b) &&
mp_int_float(args[2],&c) && mp_int_float(args[3],&d)){
char buf[512]="";
#ifdef NUMWORKS
strcat(buf,"vector([");
strcat_double(buf,a);
strcat(buf,",");
strcat_double(buf,b);
strcat(buf,"],[");
strcat_double(buf,c);
strcat(buf,",");
strcat_double(buf,d);
char buf2[64];
sprintf(buf2,"],color=%i):;",col);
strcat(buf,buf2);
#else
sprintf(buf,"vector([%.14g,%.14g],[%.14g,%.14g],color=%i):;",a,b,c,d,col);
#endif
const char * val=caseval(buf);
return turtle_ret(val);
}
return turtle_ret("vector: Bad argument type");
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(matplotl_arrow_obj, 4, 5, matplotl_arrow);
static mp_obj_t matplotl_grid(size_t n_args, const mp_obj_t *args) {
bool b=true;
if (n_args==1 && MP_OBJ_IS_SMALL_INT(args[0]) && MP_OBJ_SMALL_INT_VALUE(args[0])==0)
b=false;
char buf[64];
sprintf(buf,"axes=%i",b?1:0);
const char * val=caseval(buf);
return turtle_ret(val);
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(matplotl_grid_obj, 0, 1, matplotl_grid);
static mp_obj_t matplotl_scatter_reg(size_t n_args, const mp_obj_t *args,bool scatter) {
int col=0;
if (n_args==3)
col=mp_get_color(args[2]);
double *x=0,*y=0; size_t n=0,m=0,n1=0,m1=0;
if (mp_array2doubletab(args[0],&x,&n,&m)){
char * ptrx=printtab(x,n,m);
free(x);
char bufx[strlen(ptrx)+1];
strcpy(bufx,ptrx);
free(ptrx);
if (mp_array2doubletab(args[1],&y,&n1,&m1)){
char * ptry=printtab(y,n1,m1);
free(y);
char bufy[strlen(ptry)+1];
strcpy(bufy,ptry);
free(ptry);
if (n1==n && m==0 && m1==0){
char buf2[strlen(bufx)+strlen(bufy)+256];
if (scatter)
sprintf(buf2,"scatterplot(%s,%s,color=%i):;",bufx,bufy,col);
else
sprintf(buf2,"linear_regression_plot(%s,%s,color=%i):;",bufx,bufy,col);
const char * val=caseval(buf2);
return turtle_ret(val);
}
}
}
return turtle_ret("scatterplot: Bad argument type");
}
static mp_obj_t matplotl_scatter(size_t n_args, const mp_obj_t *args) {
return matplotl_scatter_reg(n_args,args,true);
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(matplotl_scatter_obj, 2, 3, matplotl_scatter);
static mp_obj_t matplotl_linear_regression_plot(size_t n_args, const mp_obj_t *args) {
return matplotl_scatter_reg(n_args,args,false);
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(matplotl_linear_regression_plot_obj, 2, 3, matplotl_linear_regression_plot);
//
static const mp_map_elem_t matplotl_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_show), (mp_obj_t) &matplotl_show_obj },
{ MP_ROM_QSTR(MP_QSTR_clf), (mp_obj_t) &matplotl_clf_obj },
{ MP_ROM_QSTR(MP_QSTR_axis), (mp_obj_t) &matplotl_axis_obj },
{ MP_ROM_QSTR(MP_QSTR_text), (mp_obj_t) &matplotl_text_obj },
{ MP_ROM_QSTR(MP_QSTR_plot), (mp_obj_t) &matplotl_plot_obj },
{ MP_ROM_QSTR(MP_QSTR_bar), (mp_obj_t) &matplotl_bar_obj },
{ MP_ROM_QSTR(MP_QSTR_hist), (mp_obj_t) &matplotl_hist_obj },
{ MP_ROM_QSTR(MP_QSTR_histogram), (mp_obj_t) &matplotl_hist_obj },
{ MP_ROM_QSTR(MP_QSTR_boxplot), (mp_obj_t) &matplotl_boxplot_obj },
{ MP_ROM_QSTR(MP_QSTR_boxwhisker), (mp_obj_t) &matplotl_boxplot_obj },
{ MP_ROM_QSTR(MP_QSTR_arrow), (mp_obj_t) &matplotl_arrow_obj },
{ MP_ROM_QSTR(MP_QSTR_grid), (mp_obj_t) &matplotl_grid_obj },
{ MP_ROM_QSTR(MP_QSTR_vector), (mp_obj_t) &matplotl_arrow_obj },
{ MP_ROM_QSTR(MP_QSTR_barplot), (mp_obj_t) &matplotl_bar_obj },
{ MP_ROM_QSTR(MP_QSTR_scatter), (mp_obj_t) &matplotl_scatter_obj },
{ MP_ROM_QSTR(MP_QSTR_linear_regression_plot), (mp_obj_t) &matplotl_linear_regression_plot_obj },
{ MP_ROM_QSTR(MP_QSTR_scatterplot), (mp_obj_t) &matplotl_scatter_obj },
};
static MP_DEFINE_CONST_DICT(matplotl_locals_dict, matplotl_locals_dict_table);
const mp_obj_type_t matplotl_type = {
{ &mp_type_type },
.name = MP_QSTR_matplotl,
.locals_dict = (mp_obj_t)&matplotl_locals_dict
};
STATIC const mp_map_elem_t mp_module_matplotl_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__matplotl) },
{ MP_ROM_QSTR(MP_QSTR_show), (mp_obj_t) &matplotl_show_obj },
{ MP_ROM_QSTR(MP_QSTR_clf), (mp_obj_t) &matplotl_clf_obj },
{ MP_ROM_QSTR(MP_QSTR_axis), (mp_obj_t) &matplotl_axis_obj },
{ MP_ROM_QSTR(MP_QSTR_text), (mp_obj_t) &matplotl_text_obj },
{ MP_ROM_QSTR(MP_QSTR_plot), (mp_obj_t) &matplotl_plot_obj },
{ MP_ROM_QSTR(MP_QSTR_boxplot), (mp_obj_t) &matplotl_boxplot_obj },
{ MP_ROM_QSTR(MP_QSTR_boxwhisker), (mp_obj_t) &matplotl_boxplot_obj },
{ MP_ROM_QSTR(MP_QSTR_arrow), (mp_obj_t) &matplotl_arrow_obj },
{ MP_ROM_QSTR(MP_QSTR_grid), (mp_obj_t) &matplotl_grid_obj },
{ MP_ROM_QSTR(MP_QSTR_vector), (mp_obj_t) &matplotl_arrow_obj },
{ MP_ROM_QSTR(MP_QSTR_bar), (mp_obj_t) &matplotl_bar_obj },
{ MP_ROM_QSTR(MP_QSTR_hist), (mp_obj_t) &matplotl_hist_obj },
{ MP_ROM_QSTR(MP_QSTR_histogram), (mp_obj_t) &matplotl_hist_obj },
{ MP_ROM_QSTR(MP_QSTR_barplot), (mp_obj_t) &matplotl_bar_obj },
{ MP_ROM_QSTR(MP_QSTR_scatter), (mp_obj_t) &matplotl_scatter_obj },
{ MP_ROM_QSTR(MP_QSTR_linear_regression_plot), (mp_obj_t) &matplotl_linear_regression_plot_obj },
{ MP_ROM_QSTR(MP_QSTR_scatterplot), (mp_obj_t) &matplotl_scatter_obj },
};
STATIC const mp_obj_dict_t mp_module_matplotl_globals = {
.base = {&mp_type_dict},
.map = {
.all_keys_are_qstrs = 1,
.is_fixed = 1,
.used = MP_ARRAY_SIZE(mp_module_matplotl_globals_table),
.alloc = MP_ARRAY_SIZE(mp_module_matplotl_globals_table),
.table = (mp_map_elem_t*)mp_module_matplotl_globals_table,
},
};
const mp_obj_module_t mp_module_matplotl = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&mp_module_matplotl_globals,
};
|