HP Forums
Python Matplotl usage - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Python Matplotl usage (/thread-18992.html)



Python Matplotl usage - Ioncubekh - 10-20-2022 06:06 PM

I have upgraded my HP Prime firmware & got python. I tried running stuff & its works. But I cannot display plot. Steps while in Python Numeric View (Apps)

Code:
from math import *
from matplotl import *
uk_x= [0.1*i for i in range(-10,10)]
uk_x #displays correct list of values
uk_y= [cos(i) for i in uk_x]
uk_y #displays correct list of values
plot(uk_x,uk_y) # no error but nothing happens as well
plot.show() #nothing
show() #nothing

After reading this thread I realized how to transfer variables from python to ppl https://www.hpmuseum.org/forum/thread-18380.html

Now If i want to assign values (list) stored in uk_x & uk_y to native HP Prime list variables say L1 & L2 how should I proceed? These then can be used in Stats APP for plotting & further stuff


RE: Python Matplotl usage - Ioncubekh - 10-23-2022 07:33 PM

In this video of Casio Education matplot library is used to plot a graph with syntax very similar to PC python.

At about 01:20 you can see in mere x3 line code a graph was plotted. Same commands are available in HP Prime but it doesn't show graph??






RE: Python Matplotl usage - Guenter Schink - 10-23-2022 10:08 PM

The sad reality is that the PYTHON implementation on the Prime is extremely incomplete and buggy, read unreliable. To my knowledge there is no list at all of functions that are implemented. Therefore it's almost impossible to determine whether you made an error in your code or the function you try to use simply doesn't do anything.

There is no help available. I've really done a lot of investigation, but finally I almost gave up.

Sorry, Günter


RE: Python Matplotl usage - Eddie W. Shore - 10-24-2022 02:01 AM

I agree, Guenter. I think the documentation needs to be more complete.


RE: Python Matplotl usage - parisse - 10-24-2022 01:08 PM

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,
};



RE: Python Matplotl usage - Ioncubekh - 10-24-2022 03:27 PM

@Edward
Quote:add after matplotl.show() a prime.eval instruction with a WAIT
It seems working but the result is attached Sad The axis & all other stuff is missing through I invoked them in code explicitly
I think some code from graphics() library is to be used which I don't know
Code:

EXPORT ukchart()
BEGIN
PYTHON(ukchart0);
WAIT(0);
END;

#PYTHON ukchart0()
from matplotl import *
from linalg import *
from math import *

x=linspace(0,90,100)
y=[cos(j) for j in x]
clf()
plot(x,y)
axis(0,90,-1,1) #also tried axis('on') | axis('square')
show()

#end



RE: Python Matplotl usage - parisse - 10-25-2022 10:04 AM

On your screenshot it seems the screen is not cleared, you might try any other Python clearing screen instruction, but no warranty...
Some Xcas commands called from matplotl are currently missing (means hidden) on the Prime. For example, if you look at the clf code, it calls erase() but erase is not available on the Prime.


RE: Python Matplotl usage - Ioncubekh - 10-25-2022 05:45 PM

@parisee

I modified & add PRINT();
Code:
EXPORT ukchart()
BEGIN
PYTHON(ukchart0);
WAIT(0);
END;

It did reduce line clutter but axis aren't available nor the background is solid


RE: Python Matplotl usage - parisse - 10-25-2022 08:03 PM

(10-25-2022 05:45 PM)Ioncubekh Wrote:  @parisse

I modified & add PRINT();
Code:
EXPORT ukchart()
BEGIN
PYTHON(ukchart0);
WAIT(0);
END;

It did reduce line clutter but axis aren't available nor the background is solid
I suggest you to add an HPPL clearing screen instruction before calling your python code.
Axes can not work since it is translated to axes, and the keyword axes is not exposed in the HP CAS parser. That's a good observation, I must add some keywords in the HP Prime CAS parser/lexer if we want to have matplotl working some day in the future on the Prime (like on other KhiCAS platforms)


RE: Python Matplotl usage - Ioncubekh - 10-25-2022 08:35 PM

Quote:I suggest you to add an HPPL clearing screen instruction before calling your python code
Sir please guide; I am well versed in python but only started reading about HPPL


RE: Python Matplotl usage - parisse - 10-26-2022 10:38 AM

According to online help, RECT_P() clears the sreen.