gnuplotでグラフ描画してみた

## 参考

* [gnuplotで棒グラフを書く - 米澤進吾 ホームページ](http://www.ss.scphys.kyoto-u.ac.jp/person/yonezawa/contents/program/gnuplot/bar-graph.html)

```
apt install -y gnuplot
```

# サンプルデータ1

```bash:a.txt
00 00
01 00
02 00
03 00
04 01
05 02
06 10
07 66
08 74
09 60
10 33
11 08
12 08
13 21
14 30
15 79
16 59
17 52
18 10
19 00
20 01
21 05
22 06
23 01
```

## 命令

```gnuplot:a.plot
set datafile separator " "
set terminal dumb 80, 24

set xdata time
set timefmt "%H"
set format x "%H"

set xrange ["00":"23"]
set yrange [0:100]

plot "a.txt" using 1:2 with lines notitle
```

```bash:実行
gnuplot a.plot
```

```bash:結果
  100 +-++--+--+--+--+--+--+--+--+--+--+---+--+--+--+--+--+--+--+--+--+--++-+   
      +     +     +     +     +     +      +     +     +     +     +     +  |   
      |                                                                     |   
      |                                                                     |   
   80 +-+                                           *                     +-+   
      |                      **                     **                      |   
      |                    **  *                   *  *                     |   
      |                    *    *                  *  *                     |   
   60 +-+                 *      *                 *   ***                +-+   
      |                   *       *               *       *                 |   
      |                   *       *               *       *                 |   
   40 +-+                 *        *              *        *              +-+   
      |                  *         *             *         *                |   
      |                  *          *            *          *               |   
      |                  *           *         **           *               |   
   20 +-+                *            *       *             *             +-+   
      |                 *             *     **               *              |   
      |                 *              *****                 ***            |   
      +     +     +   **+     +     +      +     +     +     +     + *******|   
    0 ****************--+--+--+--+--+--+---+--+--+--+--+--+--+--*****-+--++-*   
     00    02    04    06    08    10     12    14    16    18    20    22      
```

----


# サンプルデータ2

```text:a.txt
00 00 00
01 00 00
02 00 00
03 00 00
04 01 00
05 02 03
06 10 12
07 66 64
08 74 76
09 60 50
10 33 13
11 08 16
12 08 08
13 21 16
14 30 47
15 79 64
16 59 66
17 52 32
18 10 16
19 00 04
20 01 02
21 05 05
22 06 01
23 01 04
```

## 命令

```gnuplot:a.plot
set datafile separator " "
set terminal dumb 80, 24

set xdata time
set timefmt "%H"
set format x "%H"

set xrange ["00":"23"]
set yrange [0:100]

plot \
  "a.txt" using 1:2 with lines title "1", \
  "a.txt" using 1:3 with lines title "2"
```

```bash:結果
  100 +-++--+--+--+--+--+--+--+--+--+--+---+--+--+--+--+--+--+--+--+--+--++-+   
      +     +     +     +     +     +      +     +     +     +     +     +  |   
      |                                                           1 ******* |   
      |                                                           2 ####### |   
   80 +-+                                           *                     +-+   
      |                      **                     **                      |   
      |                    **# *                   *  *#                    |   
      |                    *    *                  *##*#                    |   
   60 +-+                 *#    #*                 *   ***                +-+   
      |                   *      #*               *     # *                 |   
      |                   *      #*              #*      #*                 |   
   40 +-+                 *       #*             #*      # *              +-+   
      |                  *#       #*            #*        #*                |   
      |                  *         #*           #*        # *               |   
      |                  *         # *         **          #*               |   
   20 +-+                *         #  *       *#            *             +-+   
      |                 *           # *#### **#              *##            |   
      |                #*           ## *****#                ***            |   
      +     +     + ##**+     +     +      +     +     +     +  ###+ *******#   
    0 ****************--+--+--+--+--+--+---+--+--+--+--+--+--+--*****-+--##-*   
     00    02    04    06    08    10     12    14    16    18    20    22      
```

----

# サンプルデータ3

```text:a.txt
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
04 01 00 00 00 00 00 00 00 00 02 01 02 02 00 01
05 02 03 01 06 03 00 04 04 01 01 02 03 05 02 01
06 10 12 10 07 11 06 17 07 10 14 14 15 13 16 13
07 66 64 43 61 48 44 42 60 77 45 69 53 68 63 57
08 74 76 72 75 83 57 92 73 61 61 73 84 66 80 54
09 60 50 49 59 59 60 54 54 56 67 63 53 61 59 64
10 33 13 30 35 21 22 21 17 16 18 23 31 29 26 26
11 08 16 17 10 13 13 21 18 08 07 09 19 22 11 09
12 08 08 08 11 07 06 11 06 04 08 08 09 10 03 12
13 21 16 18 12 31 24 23 18 22 18 24 14 15 10 23
14 30 47 34 35 41 28 33 40 41 39 39 42 51 43 42
15 79 64 72 59 74 68 72 83 54 76 66 71 63 59 71
16 59 66 68 55 72 64 55 55 62 71 70 68 56 58 66
17 52 32 35 38 43 35 33 47 40 35 42 30 39 38 31
18 10 16 07 08 10 09 05 14 15 10 13 11 12 04 09
19 00 04 00 04 07 02 04 05 07 05 04 02 07 03 05
20 01 02 10 02 04 08 07 06 10 08 03 02 02 04 10
21 05 05 04 02 01 06 05 02 11 08 05 02 04 05 02
22 06 01 03 02 05 05 05 04 03 07 02 04 06 06 02
23 01 04 05 02 03 03 05 05 00 04 06 06 05 01 03
```

## 命令

```gnuplot:a.plot
set datafile separator " "
set terminal dumb 80, 24

set xdata time
set timefmt "%H"
set format x "%H"

set xrange ["00":"23"]
set yrange [0:100]

plot \
  "a.txt" using 1:2 with lines title "1", \
  "a.txt" using 1:3 with lines title "2", \
  "a.txt" using 1:4 with lines title "3", \
  "a.txt" using 1:5 with lines title "4", \
  "a.txt" using 1:6 with lines title "5", \
  "a.txt" using 1:7 with lines title "6", \
  "a.txt" using 1:8 with lines title "7", \
  "a.txt" using 1:9 with lines title "8", \
  "a.txt" using 1:10 with lines title "9", \
  "a.txt" using 1:11 with lines title "10", \
  "a.txt" using 1:12 with lines title "11", \
  "a.txt" using 1:13 with lines title "12", \
  "a.txt" using 1:14 with lines title "13", \
  "a.txt" using 1:15 with lines title "14", \
  "a.txt" using 1:16 with lines title "15"
```

```bash:結果
  100 +-++--+--+--+--+--+--+--+--+--+--+---+--+--+--+--+--+--+--+--+--+--++-+   
      +     +     +     +     +     +      +     +     +     +     +     +  |   
      |                       =                                   1 ******* |   
      |                       @                     *             2 ####### |   
   80 +-+                  * =@@                    **            3 $$$$$$$-+   
      |                    ****@$                  ***$@          4 %%%%%%% |   
      |                    **#$*@#                 *$&*#@         5 @@@@@@@ |   
      |                   **@$*$*#                 *##*#$         6 &&&&&&& |   
   60 +-+                 *#@$&&#*#               **%%%***        7 =======-+   
      |                   *@$&= $#*              @*$*  %#$*       8 ******* |   
      |                   *@$    #*%             #*%    %#*       9 ******* |   
   40 +-+                **$      #*            @#*      #$*     10 #######-+   
      |                  *#$      #*%          @#*&      =#*     11 $$$$$$$ |   
      |                  *$        #*%%       @$#*        #$*    12 %%%%%%% |   
      |                  *$        #@*       @&**&         #*    13 @@@@@@@ |   
   20 +-+               **         #@@*===   @*#           =**   14 &&&&&&&-+   
      |                 *$          #**#### **#%            $*## 15 ======= |   
      |               =#*           ## *****#%%             =***  *$$$***   |   
      +     +     + ##**%     +     +  ##  @&    +     +     $$$###@@*******#   
    0 ****************&-+--+--+--+--+--+---+--+--+--+--+--+--+--*****%%%%##%*   
     00    02    04    06    08    10     12    14    16    18    20    22      
```

----

# メール送信時の工夫

## そのままメールを送ると、先頭に大量の空白が入る

```bash:
gnuplot a.plot | mail user@mymailaddr.com
```

![スクリーンショット_2016-09-14_08-25-30.png](https://qiita-image-store.s3.amazonaws.com/0/25728/3d24a4de-3c4d-a13f-945d-6c803855ce04.png)


## 空白だけの行を削除

```
gnuplot a.plot  | sed 's/^\s*$//g' | mail user@mymailaddr.com
```

![ok.png](https://qiita-image-store.s3.amazonaws.com/0/25728/481cc296-851a-6da6-ff9d-28272b9eb1bc.png)