Saturday, February 8, 2020

psql and gnuplot

psql from PostgreSQL 12 can produces CSV format. This format is well readable by gnuplot. So anybody can use it together:

\pset format csv
select i, sin(i) from generate_series(0, 6.3, 0.5) g(i) \g |gnuplot -p -e "set datafile separator ','; set key autotitle columnhead; plot '-'with boxes"

    1 +-+---------+-*******--+-----------+----------+-----------+--------+-+   
      +           + *     *******        +          +           +          +   
  0.8 +-+      ******     *     *                              sin *******-+   
      |        *    *     *     *                                          |   
  0.6 +-+      *    *     *     *******                                  +-+   
      |  *******    *     *     *     *                                    |   
  0.4 +-+*     *    *     *     *     *                                  +-+   
      |  *     *    *     *     *     *                                    |   
  0.2 +-+*     *    *     *     *     ******                             +-+   
    0 **********************************************************************   
      |                                    *     *     *     *    *     *  *   
 -0.2 +-+                                  *     *     *     *    *     *+-*   
      |                                    *     *     *     *    *     ****   
 -0.4 +-+                                  *******     *     *    *     *+-+   
      |                                          *     *     *    *     *  |   
 -0.6 +-+                                        *     *     *    *     *+-+   
      |                                          *     *     *    *******  |   
 -0.8 +-+                                        *******     *    *      +-+   
      +           +          +           +          +  *     *  + *        +   
   -1 +-+---------+----------+-----------+----------+--************------+-+   
      0           1          2           3          4           5          6  

postgres=# select i, sin(i) from generate_series(0, 6.3, 0.05) g(i) \g |gnuplot -p -e "set datafile separator ','; set key autotitle columnhead; set terminal dumb enhanced; plot '-'with boxes" 

                                                                               
                                                                               
    1 +-+-------+-*******-+---------+--------+---------+---------+-------+-+   
      +         ************        +        +         +         +         +   
  0.8 +-+     ****************                                 sin *******-+   
      |      ******************                                            |   
  0.6 +-+   ********************                                         +-+   
      |   ************************                                         |   
  0.4 +-+**************************                                      +-+   
      | ****************************                                       |   
  0.2 +******************************                                    +-+   
    0 ***************************************************************    +-+   
      |                              ********************************      |   
 -0.2 +-+                             ******************************     +-+   
      |                                ****************************        |   
 -0.4 +-+                               **************************       +-+   
      |                                  ***********************           |   
 -0.6 +-+                                 *********************          +-+   
      |                                     ******************             |   
 -0.8 +-+                                    ****************            +-+   
      +         +         +         +        + ************      +         +   
   -1 +-+-------+---------+---------+--------+---********--------+-------+-+   
      0         1         2         3        4         5         6         7   
                                                                               

postgres=# select i, sin(i) from generate_series(0, 6.3, 0.05) g(i) \g |gnuplot -p -e "set datafile separator ','; set key autotitle columnhead; set terminal dumb enhanced; plot '-'with lines ls 1" 

                                                                               
                                                                               
    1 +-+-------+-*******-+---------+--------+---------+---------+-------+-+   
      +         ***      ***        +        +         +         +         +   
  0.8 +-+     ***          ***                                 sin *******-+   
      |      **              **                                            |   
  0.6 +-+   *                  *                                         +-+   
      |   **                    **                                         |   
  0.4 +-+**                      **                                      +-+   
      | **                        **                                       |   
  0.2 +**                          **                                    +-+   
    0 **+                           **                              *    +-+   
      |                              **                            **      |   
 -0.2 +-+                             **                          **     +-+   
      |                                 *                        **        |   
 -0.4 +-+                                *                      **       +-+   
      |                                  **                    *           |   
 -0.6 +-+                                 **                 **          +-+   
      |                                     **              **             |   
 -0.8 +-+                                    **           ***            +-+   
      +         +         +         +        + ***     +***      +         +   
   -1 +-+-------+---------+---------+--------+---*******---------+-------+-+   
      0         1         2         3        4         5         6         7 



This can be little bit simplified with psql macros

postgres=# \set gnuplot '\\g | gnuplot -p -e "set datafile separator \',\'; set key autotitle columnhead; set terminal dumb enhanced; plot \'-\'with boxes"' 

postgres=# \pset format csv

postgres=# select i, sin(i) from generate_series(0, 6.3, 0.05) g(i) :gnuplot

                                                                               
                                                                               
    1 +-+-------+-*******-+---------+--------+---------+---------+-------+-+   
      +         ************        +        +         +         +         +   
  0.8 +-+     ****************                                 sin *******-+   
      |      ******************                                            |   
  0.6 +-+   ********************                                         +-+   
      |   ************************                                         |   
  0.4 +-+**************************                                      +-+   
      | ****************************                                       |   
  0.2 +******************************                                    +-+   
    0 ***************************************************************    +-+   
      |                              ********************************      |   
 -0.2 +-+                             ******************************     +-+   
      |                                ****************************        |   
 -0.4 +-+                               **************************       +-+   
      |                                  ***********************           |   
 -0.6 +-+                                 *********************          +-+   
      |                                     ******************             |   
 -0.8 +-+                                    ****************            +-+   
      +         +         +         +        + ************      +         +   
   -1 +-+-------+---------+---------+--------+---********--------+-------+-+   
      0         1         2         3        4         5         6         7   
                                                                               

1 Comments:

At February 12, 2020 at 6:12 PM , Blogger Unknown said...

Hi, thanks for this awesome post. How do we make this to print letters /numbers ?
ex: Hello

 

Post a Comment

Subscribe to Post Comments [Atom]

<< Home