├── COPYRIGHT.txt ├── README.md ├── doc ├── common.md ├── custom.md ├── file.md ├── index.md ├── plot3dpoints.md ├── plot_filled.png ├── plot_hist.png ├── plot_imagesc.png ├── plot_labels.png ├── plot_raw.png ├── plot_sincos.png ├── plot_splot.png ├── plot_splot2.png ├── plot_x.png ├── plot_xyf.png ├── plothistogram.md ├── plotline.md ├── plotmatrix.md ├── plotsurface.md ├── scatter3.png └── scatter3_helix.png ├── gnuplot.lua ├── hist.lua ├── init.lua ├── mkdocs.yml ├── rocks └── gnuplot-scm-1.rockspec └── test.lua /COPYRIGHT.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2011-2014 Idiap Research Institute (Ronan Collobert) 2 | Copyright (c) 2012-2014 Deepmind Technologies (Koray Kavukcuoglu) 3 | Copyright (c) 2011-2012 NEC Laboratories America (Koray Kavukcuoglu) 4 | Copyright (c) 2011-2013 NYU (Clement Farabet) 5 | Copyright (c) 2006-2010 NEC Laboratories America (Ronan Collobert, Leon Bottou, Iain Melvin, Jason Weston) 6 | Copyright (c) 2006 Idiap Research Institute (Samy Bengio) 7 | Copyright (c) 2001-2004 Idiap Research Institute (Ronan Collobert, Samy Bengio, Johnny Mariethoz) 8 | 9 | All rights reserved. 10 | 11 | Redistribution and use in source and binary forms, with or without 12 | modification, are permitted provided that the following conditions are met: 13 | 14 | 1. Redistributions of source code must retain the above copyright 15 | notice, this list of conditions and the following disclaimer. 16 | 17 | 2. Redistributions in binary form must reproduce the above copyright 18 | notice, this list of conditions and the following disclaimer in the 19 | documentation and/or other materials provided with the distribution. 20 | 21 | 3. Neither the names of Deepmind Technologies, NYU, NEC Laboratories America 22 | and IDIAP Research Institute nor the names of its contributors may be 23 | used to endorse or promote products derived from this software without 24 | specific prior written permission. 25 | 26 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 27 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 30 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 | POSSIBILITY OF SUCH DAMAGE. 37 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Plotting Package Manual with Gnuplot # 3 | 4 | A plotting package to visualize [Tensor](https://github.com/torch/torch7/blob/master/doc/tensor.md#tensor) objects. 5 | Functions fall into several types of categories: 6 | 7 | * [Plotting lines](doc/plotline.md#gnuplot.line.dok) 8 | * [Plotting matrices](doc/plotmatrix.md#gnuplot.image.dok) 9 | * [Plotting surfaces](doc/plotsurface.md#gnuplot.surface.dok) 10 | * [Plotting histograms](doc/plothistogram.md#gnuplot.histogram.dok) 11 | * [Plotting 3D points](doc/plot3dpoints.md#gnuplot.scatter3.dok) 12 | * [Saving to Files](doc/file.md#gnuplot.files.dok) 13 | * [Common operations](doc/common.md) 14 | 15 | The plotting package currently uses [gnuplot](http://gnuplot.info) as 16 | a backend to display data. In particular, `Gnuplot` version `4.4` 17 | or above is suggested for full support of all functionality. 18 | 19 | By default, the plot package will search for terminal in following order: 20 | 21 | * ` windows ` terminal if operating system is windows 22 | * ` wxt `, ` qt `, ` x11 ` terminal if operating system is linux 23 | * ` aqua `, `wxt`, ` qt `, ` x11 ` terminal if operating system is mac 24 | 25 | It is also possible to manually set any terminal type using 26 | [gnuplot.setterm](#gnuplot.setterm). Interactivity is 27 | dependent on the terminal being used. By default, `x11` and ''wxt 28 | support'' different interactive operations like, zooming, panning, 29 | rotating... 30 | 31 | The `Gnuplot` port uses `pipes` to communicate with `gnuplot`, 32 | therefore each plotting session is persistent and additional commands 33 | can be sent. For advanced users [gnuplot.raw](doc/common.md#gnuplot.raw) 34 | provides a free form interface to gnuplot. 35 | -------------------------------------------------------------------------------- /doc/common.md: -------------------------------------------------------------------------------- 1 | 2 | ## Common Operations ## 3 | 4 | It is possible to manage multiple plots at a time, printing plots to 5 | png, eps or pdf files or creating plots directly on png or eps or pdf files. 6 | 7 | There are also several handy operations for decorating plots which are 8 | common to many of the plotting functions. 9 | 10 | 11 | ### gnuplot.setgnuplotexe(exe) ### 12 | 13 | Manually set the location of gnuplot executable. 14 | 15 | 16 | ### gnuplot.setterm(teerm) ### 17 | 18 | Manually set the gnuplot terminal. 19 | 20 | 21 | ### gnuplot.closeall() ### 22 | 23 | Close all gnuplot active connections. This will not be able to close 24 | open windows, since on the backend gnuplot also can not close windows. 25 | 26 | 27 | ### gnuplot.figure([n]) ### 28 | 29 | Select (or create) a new figure with id `n`. Note that until a plot 30 | command is given, the window will not be created. When `n` is 31 | skipped, a new figure is created with the next consecutive id. 32 | 33 | 34 | ### gnuplot.xlabel(label) ### 35 | 36 | Sets the label of `x` axis to `label`. Only supported for gnuplot 37 | version 4.4 and above. 38 | 39 | 40 | ### gnuplot.ylabel(label) ### 41 | 42 | Sets the label of `y` axis to `label`. Only supported for gnuplot 43 | version 4.4 and above. 44 | 45 | 46 | ### gnuplot.zlabel(label) ### 47 | 48 | Sets the label of `z` axis to `label`. Only supported for gnuplot 49 | version 4.4 and above. 50 | 51 | 52 | ### gnuplot.title(title) ### 53 | 54 | Sets the title of the plot to `title`. Only supported for gnuplot 55 | version 4.4 and above. 56 | 57 | 58 | ### gnuplot.grid(toggle) ### 59 | 60 | If `toggle` is `true` then a grid is displayed, else it is 61 | hidden. Only supported for gnuplot version 4.4 and above. 62 | 63 | 64 | ### gnuplot.movelegend(hloc,vloc) ### 65 | 66 | Set the location of legend key. `hloc` can be `left`, `right` or 67 | `center`. `vloc` can be `top`, `bottom` or `middle`. Only 68 | supported for gnuplot version 4.4 and above. 69 | 70 | 71 | ### gnuplot.axis(axis) ### 72 | 73 | Sets the properties of axis for the current plot. 74 | 75 | * `auto` : auto scales the axis to fit data and plot canvas 76 | * `image` : scales the axis aspect ratio so that a circle is drawn as circle. 77 | * `equal` : same as `image`. 78 | * `fill` : resets the aspect ratio of the plot to original values so that it fills up the canvas as good as possible. 79 | * `{xmin,xmax,ymin,ymax}` : Sets the limits of x and y axes. Use an empty string (2 apostophes in a row) if you want to keep the current value. 80 | 81 | 82 | ### gnuplot.raw(command) ### 83 | 84 | This command is useful for advanced users of gnuplot. `command` is 85 | directly passed to gnuplot without any formatting. 86 | 87 | Let's see an example, by plotting labels for the `xtic` 88 | 89 | ```lua 90 | LondonTemp = torch.Tensor{{9, 10, 12, 15, 18, 21, 23, 23, 20, 16, 12, 9}, 91 | {5, 5, 6, 7, 10, 13, 15, 15, 13, 10, 7, 5}} 92 | gnuplot.plot({'High [°C]',LondonTemp[1]},{'Low [°C]',LondonTemp[2]}) 93 | gnuplot.raw('set xtics ("Jan" 1, "Feb" 2, "Mar" 3, "Apr" 4, "May" 5, "Jun" 6, "Jul" 7, "Aug" 8, "Sep" 9, "Oct" 10, "Nov" 11, "Dec" 12)') 94 | gnuplot.plotflush() 95 | gnuplot.axis{0,13,0,''} 96 | gnuplot.grid(true) 97 | gnuplot.title('London average temperature') 98 | ``` 99 | ![](plot_raw.png) 100 | 101 | We can show another example of its usage by labeling data points. Here, we generate two clusters of data with two Gaussian distributions, write it to a text file in three columns of (label, x_coor, y_coor), and plot the points with labels. 102 | 103 | ```lua 104 | labels_tab = {} 105 | for i=1,20 do 106 | table.insert(labels_tab, math.ceil(i/10)) 107 | end 108 | x = torch.cat(torch.Tensor(10):normal(2, 0.05), torch.Tensor(10):normal(3, 0.05), 1) 109 | y = torch.cat(torch.Tensor(10):normal(2, 0.05), torch.Tensor(10):normal(3, 0.05), 1) 110 | 111 | file = io.open('gaussians.txt', 'w') 112 | io.output(file) 113 | for i=1,20 do 114 | io.write(string.format('%d %f %f\n', labels_tab[i], x[i], y[i])) 115 | end 116 | io.close(file) 117 | 118 | gnuplot.pngfigure('plot_labels.png') 119 | gnuplot.title('A Tale of Two Gaussians') 120 | gnuplot.raw("plot 'gaussians.txt' using 2:3:(sprintf('%d', $1)) with labels point pt 7 offset char 0.5,0.5 notitle") 121 | gnuplot.xlabel('x') 122 | gnuplot.ylabel('y') 123 | gnuplot.grid(true) 124 | gnuplot.plotflush() 125 | ``` 126 | ![](plot_labels.png) 127 | -------------------------------------------------------------------------------- /doc/custom.md: -------------------------------------------------------------------------------- 1 | 2 | ## Customizing Gnuplot Defaults ## 3 | 4 | The default styles of gnuplot can be customized using a personal `.gnuplot` file 5 | located in the users home directory. A sample file is given below as a sample. You can 6 | paste the contents into `$HOME/.gnuplot` file and gnuplot will use the styles 7 | specified in this file. 8 | 9 | ``` 10 | ##### Modified version of the sample given in 11 | ##### http://www.guidolin.net/blog/files/2010/03/gnuplot 12 | 13 | 14 | set macro 15 | 16 | ##### Color Palette by Color Scheme Designer 17 | ##### Palette URL: http://colorschemedesigner.com/#3K40zsOsOK-K- 18 | 19 | 20 | blue_000 = "#A9BDE6" # = rgb(169,189,230) 21 | blue_025 = "#7297E6" # = rgb(114,151,230) 22 | blue_050 = "#1D4599" # = rgb(29,69,153) 23 | blue_075 = "#2F3F60" # = rgb(47,63,96) 24 | blue_100 = "#031A49" # = rgb(3,26,73) 25 | 26 | green_000 = "#A6EBB5" # = rgb(166,235,181) 27 | green_025 = "#67EB84" # = rgb(103,235,132) 28 | green_050 = "#11AD34" # = rgb(17,173,52) 29 | green_075 = "#2F6C3D" # = rgb(47,108,61) 30 | green_100 = "#025214" # = rgb(2,82,20) 31 | 32 | red_000 = "#F9B7B0" # = rgb(249,183,176) 33 | red_025 = "#F97A6D" # = rgb(249,122,109) 34 | red_050 = "#E62B17" # = rgb(230,43,23) 35 | red_075 = "#8F463F" # = rgb(143,70,63) 36 | red_100 = "#6D0D03" # = rgb(109,13,3) 37 | 38 | brown_000 = "#F9E0B0" # = rgb(249,224,176) 39 | brown_025 = "#F9C96D" # = rgb(249,201,109) 40 | brown_050 = "#E69F17" # = rgb(230,159,23) 41 | brown_075 = "#8F743F" # = rgb(143,116,63) 42 | brown_100 = "#6D4903" # = rgb(109,73,3) 43 | 44 | grid_color = "#d5e0c9" 45 | text_color = "#222222" 46 | 47 | my_font = "SVBasic Manual, 12" 48 | my_export_sz = "1024,768" 49 | 50 | my_line_width = "2" 51 | my_axis_width = "1" 52 | my_ps = "1.5" 53 | my_font_size = "14" 54 | 55 | # must convert font fo svg and ps 56 | # set term svg size @my_export_sz fname my_font fsize my_font_size enhanced dynamic rounded 57 | # set term png size @my_export_sz large font my_font 58 | # set term jpeg size @my_export_sz large font my_font 59 | # set term wxt enhanced font my_font 60 | 61 | set style data linespoints 62 | set style function lines 63 | set pointsize my_ps 64 | 65 | set style line 1 linecolor rgbcolor blue_050 linewidth @my_line_width pt 7 66 | set style line 2 linecolor rgbcolor green_050 linewidth @my_line_width pt 5 67 | set style line 3 linecolor rgbcolor red_050 linewidth @my_line_width pt 9 68 | set style line 4 linecolor rgbcolor brown_050 linewidth @my_line_width pt 13 69 | set style line 5 linecolor rgbcolor blue_025 linewidth @my_line_width pt 11 70 | set style line 6 linecolor rgbcolor green_025 linewidth @my_line_width pt 7 71 | set style line 7 linecolor rgbcolor red_025 linewidth @my_line_width pt 5 72 | set style line 8 linecolor rgbcolor brown_025 linewidth @my_line_width pt 9 73 | set style line 9 linecolor rgbcolor blue_075 linewidth @my_line_width pt 13 74 | set style line 10 linecolor rgbcolor green_075 linewidth @my_line_width pt 11 75 | set style line 11 linecolor rgbcolor red_075 linewidth @my_line_width pt 7 76 | set style line 12 linecolor rgbcolor brown_075 linewidth @my_line_width pt 5 77 | set style line 13 linecolor rgbcolor blue_100 linewidth @my_line_width pt 9 78 | set style line 14 linecolor rgbcolor green_100 linewidth @my_line_width pt 13 79 | set style line 15 linecolor rgbcolor red_100 linewidth @my_line_width pt 11 80 | set style line 16 linecolor rgbcolor brown_100 linewidth @my_line_width pt 7 81 | set style line 17 linecolor rgbcolor "#224499" linewidth @my_line_width pt 5 82 | 83 | ## plot 1,2,3,4,5,6,7,8,9 84 | set style increment user 85 | set style arrow 1 filled 86 | 87 | ## used for bar chart borders 88 | set style fill solid 0.5 89 | 90 | set size noratio 91 | set samples 300 92 | 93 | set border 31 lw @my_axis_width lc rgb text_color 94 | ``` 95 | -------------------------------------------------------------------------------- /doc/file.md: -------------------------------------------------------------------------------- 1 | 2 | ## Saving Plots to Files ## 3 | 4 | Any of the above plotting utilities can also be used for directly plotting 5 | into `eps` or `png` files, or `pdf` files if your gnuplot installation 6 | allows. A final gnuplot.plotflush() command ensures that all output is 7 | written to the file properly. 8 | 9 | ```lua 10 | gnuplot.epsfigure('test.eps') 11 | gnuplot.plot({'Sin Curve',torch.sin(torch.linspace(-5,5))}) 12 | gnuplot.xlabel('X') 13 | gnuplot.ylabel('Y') 14 | gnuplot.plotflush() 15 | ``` 16 | 17 | 18 | ### gnuplot.epsfigure(fname) ### 19 | 20 | Creates a figure directly on the `eps` file given with 21 | `fname`. This uses `Gnuplot` terminal `postscript eps enhanced color`. 22 | 23 | 24 | ### gnuplot.pdffigure(fname) ### 25 | 26 | Only available if your installation of gnuplot has been compiled 27 | with `pdf` support enabled. 28 | 29 | Creates a figure directly on the `pdf` file given with 30 | `fname`. This uses `Gnuplot` terminal `pdf enhanced color`, 31 | or `pdfcairo enhanced color` if available. 32 | 33 | 34 | ### gnuplot.pngfigure(fname) ### 35 | 36 | Creates a figure directly on the `png` file given with 37 | `fname`. This uses `Gnuplot` terminal `png`, or `pngcairo` if available. 38 | 39 | 40 | ### gnuplot.svgfigure(fname) ### 41 | 42 | Creates a figure directly on the `svg` file given with `fname`. This uses 43 | `Gnuplot` terminal `svg`. 44 | 45 | 46 | ### gnuplot.figprint(fname) ### 47 | 48 | Prints the current figure to the given file with name `fname`. Only `png` 49 | or `eps` files are supported by default. If your gnuplot installation 50 | allows, `pdf` files are also supported. 51 | 52 | 53 | ### gnuplot.plotflush([n]) ### 54 | 55 | This command sends `unset output` to underlying gnuplot. Useful for 56 | flushing file based terminals. 57 | 58 | 59 | ### gnuplot.close() ### 60 | 61 | Closes open file handles. Prevents too many handles staying open if creating lots of plots. 62 | -------------------------------------------------------------------------------- /doc/index.md: -------------------------------------------------------------------------------- 1 | 2 | # Plotting Package Manual with Gnuplot # 3 | 4 | A plotting package to visualize [Tensor](https://github.com/torch/torch7/blob/master/doc/tensor.md#tensor) objects. 5 | Functions fall into several types of categories: 6 | 7 | * [Plotting lines](plotline.md#gnuplot.line.dok) 8 | * [Plotting matrices](plotmatrix.md#gnuplot.image.dok) 9 | * [Plotting surfaces](plotsurface.md#gnuplot.surface.dok) 10 | * [Plotting histograms](plothistogram.md#gnuplot.histogram.dok) 11 | * [Plotting 3D points](plot3dpoints.md#gnuplot.scatter3.dok) 12 | * [Saving to Files](file.md#gnuplot.files.dok) 13 | * [Common operations](common.md) 14 | 15 | The plotting package currently uses [gnuplot](http://gnuplot.info) as 16 | a backend to display data. In particular, `Gnuplot` version `4.4` 17 | or above is suggested for full support of all functionality. 18 | 19 | By default, the plot package will search for terminal in following order: 20 | 21 | * ` windows ` terminal if operating system is windows 22 | * ` wxt `, ` qt `, ` x11 ` terminal if operating system is linux 23 | * ` aqua `, `wxt`, ` qt `, ` x11 ` terminal if operating system is mac 24 | 25 | It is also possible to manually set any terminal type using 26 | [gnuplot.setterm](#gnuplot.setterm). Interactivity is 27 | dependent on the terminal being used. By default, `x11` and ''wxt 28 | support'' different interactive operations like, zooming, panning, 29 | rotating... 30 | 31 | The `Gnuplot` port uses `pipes` to communicate with `gnuplot`, 32 | therefore each plotting session is persistent and additional commands 33 | can be sent. For advanced users [gnuplot.raw](common.md#gnuplot.raw) 34 | provides a free form interface to gnuplot. 35 | -------------------------------------------------------------------------------- /doc/plot3dpoints.md: -------------------------------------------------------------------------------- 1 | 2 | ## Plotting 3D Points ## 3 | 4 | Arbitrary 3D point constellations can be plotted using an API similar to the 5 | `scatter3` function in Matalb. 6 | 7 | 8 | ### gnuplot.scatter3(x, y, z) ### 9 | Plot `(x_i, y_i, z_i)` triplets in 3D. 10 | 11 | ```lua 12 | z = torch.linspace(-2 * math.pi, 2 * math.pi) 13 | x = z:clone():cos() 14 | y = z:clone():sin() 15 | gnuplot.scatter3(x, y, z) 16 | ``` 17 | ![](scatter3.png) 18 | 19 | It is also possible to specify a header, as well as multiple scatter plot sets 20 | on the same axis. 21 | 22 | ```lua 23 | z1 = torch.linspace(-2 * math.pi, 2 * math.pi) 24 | x = z1:clone():cos() 25 | y = z1:clone():sin() 26 | z2 = z1:clone():add(math.pi) 27 | gnuplot.scatter3({'pntsA', x, y, z1}, {'pntsB', x, y, z2}) 28 | ``` 29 | ![](scatter3_helix.png) 30 | 31 | -------------------------------------------------------------------------------- /doc/plot_filled.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/torch/gnuplot/a75e6782bdd5a7aa3ed5c2a0a88b63a916472c66/doc/plot_filled.png -------------------------------------------------------------------------------- /doc/plot_hist.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/torch/gnuplot/a75e6782bdd5a7aa3ed5c2a0a88b63a916472c66/doc/plot_hist.png -------------------------------------------------------------------------------- /doc/plot_imagesc.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/torch/gnuplot/a75e6782bdd5a7aa3ed5c2a0a88b63a916472c66/doc/plot_imagesc.png -------------------------------------------------------------------------------- /doc/plot_labels.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/torch/gnuplot/a75e6782bdd5a7aa3ed5c2a0a88b63a916472c66/doc/plot_labels.png -------------------------------------------------------------------------------- /doc/plot_raw.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/torch/gnuplot/a75e6782bdd5a7aa3ed5c2a0a88b63a916472c66/doc/plot_raw.png -------------------------------------------------------------------------------- /doc/plot_sincos.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/torch/gnuplot/a75e6782bdd5a7aa3ed5c2a0a88b63a916472c66/doc/plot_sincos.png -------------------------------------------------------------------------------- /doc/plot_splot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/torch/gnuplot/a75e6782bdd5a7aa3ed5c2a0a88b63a916472c66/doc/plot_splot.png -------------------------------------------------------------------------------- /doc/plot_splot2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/torch/gnuplot/a75e6782bdd5a7aa3ed5c2a0a88b63a916472c66/doc/plot_splot2.png -------------------------------------------------------------------------------- /doc/plot_x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/torch/gnuplot/a75e6782bdd5a7aa3ed5c2a0a88b63a916472c66/doc/plot_x.png -------------------------------------------------------------------------------- /doc/plot_xyf.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/torch/gnuplot/a75e6782bdd5a7aa3ed5c2a0a88b63a916472c66/doc/plot_xyf.png -------------------------------------------------------------------------------- /doc/plothistogram.md: -------------------------------------------------------------------------------- 1 | 2 | ## Histograms ## 3 | 4 | Given a tensor, the distribution of values can be plotted using 5 | `gnuplot.hist` function. 6 | 7 | 8 | ### gnuplot.hist(x, [nbins, min, max]) ### 9 | 10 | Plot the histogram of values in `N-D` tensor `x`, optionally using `nbins` 11 | number of bins and only using values between `min` and `max`. 12 | 13 | ```lua 14 | gnuplot.hist(torch.randn(100000),100) 15 | ``` 16 | ![](plot_hist.png) 17 | -------------------------------------------------------------------------------- /doc/plotline.md: -------------------------------------------------------------------------------- 1 | 2 | ## Plotting Lines ## 3 | 4 | Line plotting functionality covers many configurations from simplest 5 | case of plotting a single vector to displaying multiple lines at once 6 | with custom line specifictions. 7 | 8 | 9 | ### gnuplot.plot(x) ### 10 | Plot vector ` x ` using dots of first default `Gnuplot` type. 11 | 12 | ```lua 13 | x=torch.linspace(-2*math.pi,2*math.pi) 14 | gnuplot.plot(torch.sin(x)) 15 | ``` 16 | ![](plot_x.png) 17 | 18 | In more general form, plot vector `y` vs `x` using the format 19 | specified. The possible entries of format string can be: 20 | * `.` for dots 21 | * `+` for points 22 | * `-` for lines 23 | * `+-` for points and lines 24 | * `~` for using smoothed lines with cubic interpolation 25 | * `|` for using boxes 26 | * `v` for drawing vector fields. (In this case, `x` and `y` have to be two column vectors `(x, xdelta)`, `(y, ydelta)`) 27 | * custom string, one can also pass custom strings to use full capability of gnuplot. 28 | 29 | ```lua 30 | x = torch.linspace(-2*math.pi,2*math.pi) 31 | gnuplot.plot('Sin',x/math.pi,torch.sin(x),'|') 32 | ``` 33 | ![](plot_xyf.png) 34 | 35 | To plot multiple curves at a time, one can pass each plot struct in a table. 36 | 37 | ```lua 38 | x = torch.linspace(-2*math.pi,2*math.pi) 39 | gnuplot.plot({'Cos',x/math.pi,torch.cos(x),'~'},{'Sin',x/math.pi,torch.sin(x),'|'}) 40 | ``` 41 | ![](plot_sincos.png) 42 | 43 | One can pass data with multiple columns and use custom gnuplot style strings too. When multi-column data 44 | is used, the first column is assumed to be the `x` values and the rest of the columns are separate `y` series. 45 | 46 | ```lua 47 | x = torch.linspace(-5,5) 48 | y = torch.sin(x) 49 | yp = y+0.3+torch.rand(x:size())*0.1 50 | ym = y-(torch.rand(x:size())*0.1+0.3) 51 | yy = torch.cat(x,ym,2) 52 | yy = torch.cat(yy,yp,2) 53 | gnuplot.plot({yy,'with filledcurves fill transparent solid 0.5'},{x,yp,'with lines ls 1'},{x,ym,'with lines ls 1'},{x,y,'with lines ls 1'}) 54 | ``` 55 | ![](plot_filled.png) 56 | -------------------------------------------------------------------------------- /doc/plotmatrix.md: -------------------------------------------------------------------------------- 1 | 2 | ## Plotting Matrices ## 3 | 4 | A given matrix can be plotted using 2D contour plot on a surface. 5 | 6 | 7 | ### gnuplot.imagesc(z, ['color' or 'gray']) ### 8 | 9 | Plot surface ` z ` using contour plot. The second argument defines 10 | the color palette for the display. By default, grayscale colors are 11 | used, however, one can also use any color palette available in 12 | `Gnuplot`. 13 | 14 | ```lua 15 | x = torch.linspace(-1,1) 16 | xx = torch.Tensor(x:size(1),x:size(1)):zero():addr(1,x,x) 17 | xx = xx*math.pi*6 18 | gnuplot.imagesc(torch.sin(xx),'color') 19 | ``` 20 | ![](plot_imagesc.png) 21 | -------------------------------------------------------------------------------- /doc/plotsurface.md: -------------------------------------------------------------------------------- 1 | 2 | ## Plotting 3D Surfaces ## 3 | 4 | Surface plotting creates a 3D surface plot of a given matrix `z`. Entries 5 | of `z` are used as height values. It is also possible to specify `x` and 6 | `y` locations corresponding to each point in `z` . If a terminal with 7 | interactive capabilities is being used by `Gnuplot` backend (like `x11` or 8 | `wxt` or `qt`), then rotating, zooming is also possible. 9 | 10 | 11 | ### gnuplot.splot(z) ### 12 | 13 | Plot surface ` z ` in 3D. 14 | ```lua 15 | x = torch.linspace(-1,1) 16 | xx = torch.Tensor(x:size(1),x:size(1)):zero():addr(1,x,x) 17 | xx = xx*math.pi*6 18 | gnuplot.splot(torch.sin(xx)) 19 | ``` 20 | ![](plot_splot.png) 21 | 22 | It is also possible to specify the `x` and `y` locations of each 23 | point in `z` by `gnuplot.splot(x,y,z)`. In this `x` and `y` has 24 | to be the same shape as `z`. 25 | 26 | One can also display multiple surfaces at a time. 27 | 28 | ```lua 29 | x = torch.linspace(-1,1) 30 | xx = torch.Tensor(x:size(1),x:size(1)):zero():addr(1,x,x) 31 | xx = xx*math.pi*2 32 | gnuplot.splot({torch.sin(xx)},{torch.sin(xx)+2}) 33 | ``` 34 | ![](plot_splot2.png) 35 | -------------------------------------------------------------------------------- /doc/scatter3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/torch/gnuplot/a75e6782bdd5a7aa3ed5c2a0a88b63a916472c66/doc/scatter3.png -------------------------------------------------------------------------------- /doc/scatter3_helix.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/torch/gnuplot/a75e6782bdd5a7aa3ed5c2a0a88b63a916472c66/doc/scatter3_helix.png -------------------------------------------------------------------------------- /gnuplot.lua: -------------------------------------------------------------------------------- 1 | 2 | require 'paths' 3 | 4 | local torchstyle = [[ 5 | ##### Modified version of the sample given in 6 | ##### http://www.guidolin.net/blog/files/2010/03/gnuplot 7 | 8 | 9 | set macro 10 | 11 | ##### Color Palette by Color Scheme Designer 12 | ##### Palette URL: http://colorschemedesigner.com/#3K40zsOsOK-K- 13 | 14 | 15 | blue_000 = "#A9BDE6" # = rgb(169,189,230) 16 | blue_025 = "#7297E6" # = rgb(114,151,230) 17 | blue_050 = "#1D4599" # = rgb(29,69,153) 18 | blue_075 = "#2F3F60" # = rgb(47,63,96) 19 | blue_100 = "#031A49" # = rgb(3,26,73) 20 | 21 | green_000 = "#A6EBB5" # = rgb(166,235,181) 22 | green_025 = "#67EB84" # = rgb(103,235,132) 23 | green_050 = "#11AD34" # = rgb(17,173,52) 24 | green_075 = "#2F6C3D" # = rgb(47,108,61) 25 | green_100 = "#025214" # = rgb(2,82,20) 26 | 27 | red_000 = "#F9B7B0" # = rgb(249,183,176) 28 | red_025 = "#F97A6D" # = rgb(249,122,109) 29 | red_050 = "#E62B17" # = rgb(230,43,23) 30 | red_075 = "#8F463F" # = rgb(143,70,63) 31 | red_100 = "#6D0D03" # = rgb(109,13,3) 32 | 33 | brown_000 = "#F9E0B0" # = rgb(249,224,176) 34 | brown_025 = "#F9C96D" # = rgb(249,201,109) 35 | brown_050 = "#E69F17" # = rgb(230,159,23) 36 | brown_075 = "#8F743F" # = rgb(143,116,63) 37 | brown_100 = "#6D4903" # = rgb(109,73,3) 38 | 39 | grid_color = "#d5e0c9" 40 | text_color = "#222222" 41 | 42 | my_font = "SVBasic Manual, 12" 43 | my_export_sz = "1024,768" 44 | 45 | my_line_width = "2" 46 | my_axis_width = "1" 47 | my_ps = "1" 48 | my_font_size = "14" 49 | 50 | # must convert font fo svg and ps 51 | # set term svg size @my_export_sz fname my_font fsize my_font_size enhanced dynamic rounded 52 | # set term png size @my_export_sz large font my_font 53 | # set term jpeg size @my_export_sz large font my_font 54 | # set term wxt enhanced font my_font 55 | 56 | set style data linespoints 57 | set style function lines 58 | set pointsize my_ps 59 | 60 | set style line 1 linecolor rgbcolor blue_050 linewidth @my_line_width pt 7 61 | set style line 2 linecolor rgbcolor green_050 linewidth @my_line_width pt 7 62 | set style line 3 linecolor rgbcolor red_050 linewidth @my_line_width pt 7 63 | set style line 4 linecolor rgbcolor brown_050 linewidth @my_line_width pt 7 64 | set style line 5 linecolor rgbcolor blue_025 linewidth @my_line_width pt 5 65 | set style line 6 linecolor rgbcolor green_025 linewidth @my_line_width pt 5 66 | set style line 7 linecolor rgbcolor red_025 linewidth @my_line_width pt 5 67 | set style line 8 linecolor rgbcolor brown_025 linewidth @my_line_width pt 5 68 | set style line 9 linecolor rgbcolor blue_075 linewidth @my_line_width pt 9 69 | set style line 10 linecolor rgbcolor green_075 linewidth @my_line_width pt 9 70 | set style line 11 linecolor rgbcolor red_075 linewidth @my_line_width pt 9 71 | set style line 12 linecolor rgbcolor brown_075 linewidth @my_line_width pt 9 72 | set style line 13 linecolor rgbcolor blue_100 linewidth @my_line_width pt 13 73 | set style line 14 linecolor rgbcolor green_100 linewidth @my_line_width pt 13 74 | set style line 15 linecolor rgbcolor red_100 linewidth @my_line_width pt 13 75 | set style line 16 linecolor rgbcolor brown_100 linewidth @my_line_width pt 13 76 | set style line 17 linecolor rgbcolor "#224499" linewidth @my_line_width pt 11 77 | 78 | ## plot 1,2,3,4,5,6,7,8,9 79 | set style increment user 80 | set style arrow 1 filled 81 | 82 | ## used for bar chart borders 83 | ## set style fill solid 0.5 84 | 85 | set size noratio 86 | set samples 300 87 | 88 | set border 31 lw @my_axis_width lc rgb text_color 89 | ]] 90 | 91 | local _gptable = {} 92 | _gptable.current = nil 93 | _gptable.defaultterm = nil 94 | _gptable.exe = nil 95 | _gptable.hasrefresh = true 96 | 97 | local function getexec() 98 | if not _gptable.exe then 99 | error('gnuplot executable is not set') 100 | end 101 | return _gptable.exe 102 | end 103 | 104 | local function findos() 105 | local s = paths.uname() 106 | if s and s:match("Windows") then 107 | return 'windows' 108 | elseif s and s:match('Darwin') then 109 | return 'mac' 110 | elseif s and s:match('Linux') then 111 | return 'linux' 112 | elseif s and s:match('FreeBSD') then 113 | return 'freebsd' 114 | else 115 | return '?' 116 | end 117 | end 118 | 119 | local function getfigure(n) 120 | local n = n 121 | if not n or n == nil then 122 | n = #_gptable+1 123 | end 124 | if _gptable[n] == nil then 125 | _gptable[n] = {} 126 | if _gptable.defaultterm == nil then 127 | error('Gnuplot terminal is not set') 128 | end 129 | local silent = '> /dev/null 2>&1' 130 | if paths.is_win() then 131 | silent = '> nul 2>&1' 132 | end 133 | _gptable[n].term = _gptable.defaultterm 134 | _gptable[n].pipe = torch.PipeFile(getexec() .. ' -persist ' .. silent,'w') 135 | end 136 | _gptable.current = n 137 | if not paths.filep(paths.concat(paths.home,'.gnuplot')) then 138 | _gptable[n].pipe:writeString(torchstyle .. '\n\n\n') 139 | _gptable[n].pipe:synchronize() 140 | end 141 | return _gptable[n] 142 | end 143 | 144 | local function gnuplothasterm(term) 145 | if not _gptable.exe then 146 | return false--error('gnuplot exe is not found, can not chcek terminal') 147 | end 148 | local tfni = os.tmpname() 149 | local tfno = os.tmpname() 150 | local fi = io.open(tfni,'w') 151 | fi:write('set terminal\n\n') 152 | fi:close() 153 | os.execute(getexec() .. ' < ' .. tfni .. ' > ' .. tfno .. ' 2>&1 ') 154 | os.remove(tfni) 155 | local tf = io.open(tfno,'r') 156 | local s = tf:read('*l') 157 | while s do 158 | if s:match('^.*%s+ '.. term .. ' ') then 159 | tf:close() 160 | os.remove(tfno) 161 | return true 162 | end 163 | s = tf:read('*l') 164 | end 165 | tf:close() 166 | os.remove(tfno) 167 | return false 168 | end 169 | 170 | local function findgnuplotversion(exe) 171 | local ff = io.popen(exe .. ' --version','r') 172 | local ss = ff:read('*l') 173 | ff:close() 174 | local v,vv = ss:match('(%d).(%d)') 175 | v=tonumber(v) 176 | vv=tonumber(vv) 177 | return v,vv 178 | end 179 | 180 | local function findgnuplotexe() 181 | local o = findos() 182 | local s = paths.findprogram('gnuplot') 183 | if type(s) == 'string' and s:match(' ') then 184 | s = '"' .. s .. '"' 185 | end 186 | _gptable.hasrefresh = true 187 | do -- preserve indentation to minimize merging issues 188 | if s and s:len() > 0 and s:match('gnuplot') then 189 | local v,vv = findgnuplotversion(s) 190 | if v < 4 then 191 | error('gnuplot version 4 is required') 192 | end 193 | if v == 4 and vv < 4 then 194 | -- try to find gnuplot44 195 | if o == 'linux' and paths.filep('/usr/bin/gnuplot44') then 196 | local ss = '/usr/bin/gnuplot44' 197 | v,vv = findgnuplotversion(ss) 198 | if v == 4 and vv == 4 then 199 | return ss 200 | end 201 | end 202 | _gptable.hasrefresh = false 203 | print('Gnuplot package working with reduced functionality.') 204 | print('Please install gnuplot version >= 4.4.') 205 | end 206 | return s 207 | else 208 | return nil 209 | end 210 | end 211 | end 212 | 213 | local function getgnuplotdefaultterm(os) 214 | if os == 'windows' then 215 | return 'windows' 216 | elseif os == 'linux' and gnuplothasterm('wxt') then 217 | return 'wxt' 218 | elseif os == 'linux' and gnuplothasterm('qt') then 219 | return 'qt' 220 | elseif os == 'linux' and gnuplothasterm('x11') then 221 | return 'x11' 222 | elseif os == 'freebsd' and gnuplothasterm('wxt') then 223 | return 'wxt' 224 | elseif os == 'freebsd' and gnuplothasterm('qt') then 225 | return 'qt' 226 | elseif os == 'freebsd' and gnuplothasterm('x11') then 227 | return 'x11' 228 | elseif os == 'mac' and gnuplothasterm('aqua') then 229 | return 'aqua' 230 | elseif os == 'mac' and gnuplothasterm('wxt') then 231 | return 'wxt' 232 | elseif os == 'mac' and gnuplothasterm('qt') then 233 | return 'qt' 234 | elseif os == 'mac' and gnuplothasterm('x11') then 235 | return 'x11' 236 | else 237 | print('Can not find any of the default terminals for ' .. os .. '. ' .. 238 | 'You can manually set terminal by gnuplot.setterm("terminal-name")') 239 | return nil 240 | end 241 | end 242 | 243 | local function findgnuplot() 244 | local exe = findgnuplotexe() 245 | local os = findos() 246 | if not exe then 247 | return nil 248 | end 249 | _gptable.exe = exe 250 | _gptable.defaultterm = getgnuplotdefaultterm(os) 251 | end 252 | 253 | 254 | function gnuplot.setgnuplotexe(exe) 255 | local oldexe = _gptable.exe 256 | 257 | if not paths.filep(exe) then 258 | error(exe .. ' does not exist') 259 | end 260 | 261 | _gptable.exe = exe 262 | local v,vv = findgnuplotversion(exe) 263 | if v < 4 then error('gnuplot version 4 is required') end 264 | if v == 4 and vv < 4 then 265 | _gptable.hasrefresh = false 266 | print('Some functionality like adding title, labels, ... will be disabled, it is better to install gnuplot version 4.4') 267 | else 268 | _gptable.hasrefresh = true 269 | end 270 | 271 | local os = findos() 272 | local term = getgnuplotdefaultterm(os) 273 | if term == nil then 274 | print('You have manually set the gnuplot exe and I can not find default terminals, run gnuplot.setterm("terminal-name") to set term type') 275 | end 276 | end 277 | 278 | function gnuplot.setterm(term) 279 | if gnuplothasterm(term) then 280 | _gptable.defaultterm = term 281 | else 282 | error('gnuplot does not seem to have this term') 283 | end 284 | end 285 | 286 | local function getCurrentPlot() 287 | if _gptable.current == nil then 288 | gnuplot.figure() 289 | end 290 | return _gptable[_gptable.current] 291 | end 292 | 293 | local function writeToPlot(gp,str) 294 | local pipe = gp.pipe 295 | pipe:writeString(str .. '\n\n\n') 296 | pipe:synchronize() 297 | end 298 | local function refreshPlot(gp) 299 | if gp.fname then 300 | writeToPlot(gp,'set output "' .. gp.fname .. '"') 301 | end 302 | writeToPlot(gp,'refresh') 303 | if gp.fname then 304 | writeToPlot(gp,'unset output') 305 | end 306 | end 307 | local function writeToCurrent(str) 308 | writeToPlot(getCurrentPlot(),str) 309 | end 310 | local function refreshCurrent() 311 | refreshPlot(getCurrentPlot()) 312 | end 313 | 314 | -- t is the arguments for one plot at a time 315 | local function getvars(t) 316 | local legend = nil 317 | local x = nil 318 | local y = nil 319 | local format = nil 320 | 321 | local function istensor(v) 322 | return type(v) == 'userdata' and torch.typename(v):sub(-6) == 'Tensor' 323 | end 324 | 325 | local function isstring(v) 326 | return type(v) == 'string' 327 | end 328 | 329 | if #t == 0 then 330 | error('empty argument list') 331 | end 332 | 333 | if #t >= 1 then 334 | if isstring(t[1]) then 335 | legend = t[1] 336 | elseif istensor(t[1]) then 337 | x = t[1] 338 | else 339 | error('expecting [string,] tensor [,tensor] [,string]') 340 | end 341 | end 342 | if #t >= 2 then 343 | if x and isstring(t[2]) then 344 | format = t[2] 345 | elseif x and istensor(t[2]) then 346 | y = t[2] 347 | elseif legend and istensor(t[2]) then 348 | x = t[2] 349 | else 350 | error('expecting [string,] tensor [,tensor] [,string]') 351 | end 352 | end 353 | if #t >= 3 then 354 | if legend and x and istensor(t[3]) then 355 | y = t[3] 356 | elseif legend and x and isstring(t[3]) then 357 | format = t[3] 358 | elseif x and y and isstring(t[3]) then 359 | format = t[3] 360 | else 361 | error('expecting [string,] tensor [,tensor] [,string]') 362 | end 363 | end 364 | if #t == 4 then 365 | if legend and x and y and isstring(t[4]) then 366 | format = t[4] 367 | else 368 | error('expecting [string,] tensor [,tensor] [,string]') 369 | end 370 | end 371 | legend = legend or '' 372 | format = format or '' 373 | if not x then 374 | error('expecting [string,] tensor [,tensor] [,string]') 375 | end 376 | if not y then 377 | if x:dim() == 2 and x:size(2) == 2 then 378 | y = x:select(2,2) 379 | x = x:select(2,1) 380 | elseif x:dim() == 2 and x:size(2) == 4 and format == 'v' then 381 | y = torch.Tensor(x:size(1),2) 382 | xx= torch.Tensor(x:size(1),2) 383 | y:select(2,1):copy(x:select(2,2)) 384 | y:select(2,2):copy(x:select(2,4)) 385 | xx:select(2,1):copy(x:select(2,1)) 386 | xx:select(2,2):copy(x:select(2,3)) 387 | x = xx 388 | elseif x:dim() == 2 and x:size(2) > 1 then 389 | y = x[{ {}, {2,-1} }] 390 | x = x:select(2,1) 391 | else 392 | y = x 393 | x = torch.range(1,y:size(1)) 394 | end 395 | end 396 | if x:dim() ~= 1 and x:dim() ~= 2 then 397 | error('x and y dims are wrong : x = ' .. x:nDimension() .. 'D y = ' .. y:nDimension() .. 'D') 398 | end 399 | if y:size(1) ~= x:size(1) then 400 | error('x and y dims are wrong : x = ' .. x:nDimension() .. 'D y = ' .. y:nDimension() .. 'D') 401 | end 402 | -- if x:dim() ~= y:dim() or x:nDimension() > 2 or y:nDimension() > 2 then 403 | -- error('x and y dims are wrong : x = ' .. x:nDimension() .. 'D y = ' .. y:nDimension() .. 'D') 404 | -- end 405 | -- print(x:size(),y:size()) 406 | return legend,x,y,format 407 | end 408 | 409 | -- t is the arguments for one plot at a time 410 | local function getsplotvars(t) 411 | local legend = nil 412 | local x = nil 413 | local y = nil 414 | local z = nil 415 | 416 | local function istensor(v) 417 | return type(v) == 'userdata' and torch.typename(v):sub(-6) == 'Tensor' 418 | end 419 | 420 | local function isstring(v) 421 | return type(v) == 'string' 422 | end 423 | 424 | if #t == 0 then 425 | error('empty argument list') 426 | end 427 | 428 | if #t >= 1 then 429 | if isstring(t[1]) then 430 | legend = t[1] 431 | elseif istensor(t[1]) then 432 | x = t[1] 433 | else 434 | error('expecting [string,] tensor [,tensor] [,tensor]') 435 | end 436 | end 437 | if #t >= 2 and #t <= 4 then 438 | if x and istensor(t[2]) and istensor(t[3]) then 439 | y = t[2] 440 | z = t[3] 441 | elseif legend and istensor(t[2]) and istensor(t[3]) and istensor(t[4]) then 442 | x = t[2] 443 | y = t[3] 444 | z = t[4] 445 | elseif legend and istensor(t[2]) then 446 | x = t[2] 447 | else 448 | error('expecting [string,] tensor [,tensor] [,tensor]') 449 | end 450 | elseif #t > 4 then 451 | error('expecting [string,] tensor [,tensor] [,tensor]') 452 | end 453 | legend = legend or '' 454 | if not x then 455 | error('expecting [string,] tensor [,tensor] [,tensor]') 456 | end 457 | if not z then 458 | z = x 459 | x = torch.Tensor(z:size()) 460 | y = torch.Tensor(z:size()) 461 | for i=1,x:size(1) do x:select(1,i):fill(i) end 462 | for i=1,y:size(2) do y:select(2,i):fill(i) end 463 | end 464 | if x:nDimension() ~= 2 or y:nDimension() ~= 2 or z:nDimension() ~= 2 then 465 | error('x and y and z are expected to be matrices x = ' .. x:nDimension() .. 'D y = ' .. y:nDimension() .. 'D z = '.. z:nDimension() .. 'D' ) 466 | end 467 | return legend,x,y,z 468 | end 469 | 470 | local function getscatter3vars(t) 471 | local legend = nil 472 | local x = nil 473 | local y = nil 474 | local z = nil 475 | 476 | local function istensor(v) 477 | return type(v) == 'userdata' and torch.typename(v):sub(-6) == 'Tensor' 478 | end 479 | 480 | local function isstring(v) 481 | return type(v) == 'string' 482 | end 483 | 484 | if #t ~= 3 and #t ~= 4 then 485 | error('expecting [string,] tensor, tensor, tensor') 486 | end 487 | 488 | if isstring(t[1]) then 489 | if #t ~= 4 then 490 | error('expecting [string,] tensor, tensor, tensor') 491 | end 492 | for i = 2, 4 do 493 | if not istensor(t[i]) then 494 | error('expecting [string,] tensor, tensor, tensor') 495 | end 496 | end 497 | legend = t[1] 498 | x = t[2] 499 | y = t[3] 500 | z = t[4] 501 | elseif istensor(t[1]) then 502 | if #t ~= 3 then 503 | error('expecting [string,] tensor, tensor, tensor') 504 | end 505 | for i = 2, 3 do 506 | if not istensor(t[i]) then 507 | error('expecting [string,] tensor, tensor, tensor') 508 | end 509 | end 510 | x = t[1] 511 | y = t[2] 512 | z = t[3] 513 | legend = '' 514 | else 515 | error('expecting [string,] tensor, tensor, tensor') 516 | end 517 | 518 | assert(x:dim() == 1 and y:dim() == 1 and z:dim() == 1, 519 | 'x, y and z must be 1D') 520 | assert(x:isSameSizeAs(y) and x:isSameSizeAs(z), 521 | 'x, y and z must be the same size') 522 | 523 | return legend, x, y, z 524 | end 525 | 526 | local function getimagescvars(t) 527 | local palette = nil 528 | local x = nil 529 | 530 | local function istensor(v) 531 | return type(v) == 'userdata' and torch.typename(v):sub(-6) == 'Tensor' 532 | end 533 | 534 | local function isstring(v) 535 | return type(v) == 'string' 536 | end 537 | 538 | if #t == 0 then 539 | error('empty argument list') 540 | end 541 | 542 | if #t >= 1 then 543 | if istensor(t[1]) then 544 | x = t[1] 545 | else 546 | error('expecting tensor [,string]') 547 | end 548 | end 549 | if #t == 2 then 550 | if x and isstring(t[2]) then 551 | palette = t[2] 552 | else 553 | error('expecting tensor [,string]' ) 554 | end 555 | elseif #t > 2 then 556 | error('expecting tensor [,string]') 557 | end 558 | legend = legend or '' 559 | if not x then 560 | error('expecting tensor [,string]') 561 | end 562 | if not palette then 563 | palette = 'gray' 564 | end 565 | if x:nDimension() ~= 2 then 566 | error('x is expected to be matrices x = ' .. x:nDimension() .. 'D') 567 | end 568 | return x,palette 569 | end 570 | 571 | local function gnuplot_string(legend,x,y,format) 572 | local hstr = 'plot ' 573 | local dstr = {''} 574 | local coef = {} 575 | local vecplot = {} 576 | local function gformat(f,i) 577 | if f ~= '~' and f:find('~') or f:find('acsplines') then 578 | coef[i] = f:gsub('~',''):gsub('acsplines','') 579 | coef[i] = tonumber(coef[i]) 580 | f = 'acsplines' 581 | end 582 | if f == '' or f == '' then return '' 583 | elseif f == '+' or f == 'points' then return 'with points' 584 | elseif f == '.' or f == 'dots' then return 'with dots' 585 | elseif f == '-' or f == 'lines' then return 'with lines' 586 | elseif f == '+-' or f == 'linespoints' then return 'with linespoints' 587 | elseif f == '|' or f == 'boxes' then return 'with boxes' 588 | elseif f == '~' or f == 'csplines' then return 'smooth csplines' 589 | elseif f == 'acsplines' then return 'smooth acsplines' 590 | elseif f == 'V' or f == 'v' or f == 'vectors' then vecplot[i]=true;return 'with vectors' 591 | else return f 592 | end 593 | end 594 | for i=1,#legend do 595 | if i > 1 then hstr = hstr .. ' , ' end 596 | hstr = hstr .. " '-' title '" .. legend[i] .. "' " .. gformat(format[i],i) 597 | end 598 | hstr = hstr .. '\n' 599 | for i=1,#legend do 600 | local xi = x[i] 601 | local yi = y[i] 602 | for j=1,xi:size(1) do 603 | if coef[i] then 604 | --print(i,coef) 605 | table.insert(dstr,string.format('%g %g %g\n',xi[j],yi[j],coef[i])) 606 | elseif vecplot[i] then 607 | --print(xi,yi) 608 | table.insert(dstr,string.format('%g %g %g %g\n',xi[j][1],yi[j][1],xi[j][2],yi[j][2])) 609 | elseif yi:dim() == 1 then 610 | table.insert(dstr,string.format('%g %g\n',xi[j],yi[j])) 611 | else 612 | table.insert(dstr,string.format(string.rep('%g ',1+yi:size(2)) .. '\n',xi[j],unpack(yi[j]:clone():storage():totable()))) 613 | end 614 | end 615 | collectgarbage() 616 | table.insert(dstr,'e\n') 617 | end 618 | return hstr,table.concat(dstr) 619 | end 620 | local function gnu_splot_string(legend,x,y,z) 621 | local hstr = string.format('%s\n','set contour base') 622 | hstr = string.format('%s%s\n',hstr,'set cntrparam bspline\n') 623 | hstr = string.format('%s%s\n',hstr,'set cntrparam levels auto\n') 624 | hstr = string.format('%s%s\n',hstr,'set style data lines\n') 625 | hstr = string.format('%s%s\n',hstr,'set hidden3d\n') 626 | 627 | hstr = hstr .. 'splot ' 628 | local dstr = {''} 629 | local coef 630 | for i=1,#legend do 631 | if i > 1 then hstr = hstr .. ' , ' end 632 | hstr = hstr .. " '-'title '" .. legend[i] .. "' " .. 'with lines' 633 | end 634 | hstr = hstr .. '\n' 635 | for i=1,#legend do 636 | local xi = x[i] 637 | local yi = y[i] 638 | local zi = z[i] 639 | for j=1,xi:size(1) do 640 | local xij = xi[j] 641 | local yij = yi[j] 642 | local zij = zi[j] 643 | for k=1,xi:size(2) do 644 | table.insert(dstr, string.format('%g %g %g\n',xij[k],yij[k],zij[k])) 645 | end 646 | table.insert(dstr,'\n') 647 | end 648 | table.insert(dstr,'e\n') 649 | end 650 | return hstr,table.concat(dstr) 651 | end 652 | 653 | local function gnu_scatter3_string(legend, x, y, z) 654 | local hstr = string.format('%s\n','set contour base') 655 | hstr = string.format('%s%s\n',hstr,'set style data points\n') 656 | hstr = string.format('%s%s\n',hstr,'set hidden3d\n') 657 | 658 | hstr = hstr .. 'splot ' 659 | local dstr = {''} 660 | local coef 661 | for i = 1, #legend do 662 | if i > 1 then hstr = hstr .. ' , ' end 663 | hstr = hstr .. " '-'title '" .. legend[i] .. "' " .. 'with points' 664 | end 665 | hstr = hstr .. '\n' 666 | for i = 1, #legend do 667 | local xi = x[i] 668 | local yi = y[i] 669 | local zi = z[i] 670 | for j = 1, xi:size(1) do 671 | local xij = xi[j] 672 | local yij = yi[j] 673 | local zij = zi[j] 674 | table.insert(dstr, 675 | string.format('%g %g %g\n', xij, yij, zij)) 676 | end 677 | table.insert(dstr, 'e\n') 678 | end 679 | return hstr, table.concat(dstr) 680 | end 681 | 682 | local function gnu_imagesc_string(x,palette) 683 | local hstr = string.format('%s\n','set view map') 684 | hstr = string.format('%s%s %s\n',hstr,'set palette',palette) 685 | hstr = string.format('%s%s\n',hstr,'set style data linespoints') 686 | hstr = string.format('%s%s%g%s\n',hstr,"set xrange [ -0.5 : ",x:size(2)-0.5,"] noreverse nowriteback") 687 | hstr = string.format('%s%s%g%s\n',hstr,"set yrange [ -0.5 : ",x:size(1)-0.5,"] reverse nowriteback") 688 | hstr = string.format('%s%s\n',hstr,"splot '-' matrix with image") 689 | local dstr = {''} 690 | for i=1,x:size(1) do 691 | local xi = x[i]; 692 | for j=1,x:size(2) do 693 | table.insert(dstr,string.format('%g ',xi[j])) 694 | end 695 | table.insert(dstr, string.format('\n')) 696 | end 697 | table.insert(dstr,string.format('e\ne\n')) 698 | return hstr,table.concat(dstr) 699 | end 700 | 701 | function gnuplot.close(n) 702 | if not n then 703 | n = _gptable.current 704 | end 705 | local gp = _gptable[n] 706 | if gp == nil then return end 707 | if type(n) == 'number' and torch.typename(gp.pipe) == 'torch.PipeFile' then 708 | _gptable.current = nil 709 | gnuplot.plotflush(n) 710 | writeToPlot(gp, 'quit') 711 | 712 | -- pipefile:close is buggy in TH 713 | --gp.pipe:close() 714 | gp.pipe=nil 715 | gp = nil 716 | _gptable[n] = nil 717 | end 718 | collectgarbage() 719 | end 720 | 721 | function gnuplot.closeall() 722 | for i,v in pairs(_gptable) do 723 | gnuplot.close(i) 724 | end 725 | _gptable = {} 726 | collectgarbage() 727 | findgnuplot() 728 | _gptable.current = nil 729 | end 730 | 731 | local function filefigure(fname,term,n) 732 | if not _gptable.hasrefresh then 733 | print('Plotting to files is disabled in gnuplot 4.2, install gnuplot 4.4') 734 | end 735 | 736 | -- Check whether the output directory can be written to here - torch.PipeFile 737 | -- has no read/write option so we can't read back any error messages from 738 | -- gnuplot, and writing to 'non_existent_dir/plot.png' fails silently. 739 | local outputDir = paths.dirname(fname) 740 | if outputDir == '' then 741 | outputDir = '.' 742 | end 743 | if not paths.dirp(outputDir) then 744 | error('cannot save to ' .. fname .. ': directory does not exist') 745 | end 746 | 747 | local gp = getfigure(n) 748 | gp.fname = fname 749 | gp.term = term 750 | writeToCurrent('set term '.. gp.term) 751 | --writeToCurrent('set output \'' .. gp.fname .. '\'') 752 | end 753 | function gnuplot.epsfigure(fname,n) 754 | filefigure(fname,'postscript eps enhanced color',n) 755 | return _gptable.current 756 | end 757 | 758 | function gnuplot.svgfigure(fname,n) 759 | filefigure(fname,'svg',n) 760 | return _gptable.current 761 | end 762 | 763 | function gnuplot.pngfigure(fname,n) 764 | local term = gnuplothasterm('pngcairo') and 'pngcairo' or 'png' 765 | filefigure(fname,term,n) 766 | return _gptable.current 767 | end 768 | 769 | function gnuplot.pdffigure(fname,n) 770 | local haspdf = gnuplothasterm('pdf') or gnuplothasterm('pdfcairo') 771 | if not haspdf then 772 | error('your installation of gnuplot does not have pdf support enabled') 773 | end 774 | local term = nil 775 | if gnuplothasterm('pdfcairo') then 776 | term = 'pdfcairo enhanced color' 777 | else 778 | term = 'pdf enhanced color' 779 | end 780 | filefigure(fname,term,n) 781 | return _gptable.current 782 | end 783 | 784 | function gnuplot.figprint(fname) 785 | local suffix = fname:match('.+%.(.+)') 786 | local term = nil 787 | local haspdf = gnuplothasterm('pdf') or gnuplothasterm('pdfcairo') 788 | if suffix == 'eps' then 789 | term = 'postscript eps enhanced color' 790 | elseif suffix == 'png' then 791 | term = gnuplothasterm('pngcairo') and 'pngcairo' or 'png' 792 | term = term .. ' size "1024,768"' 793 | elseif suffix == 'pdf' and haspdf then 794 | if not haspdf then 795 | error('your installation of gnuplot does not have pdf support enabled') 796 | end 797 | if gnuplothasterm('pdfcairo') then 798 | term = 'pdfcairo' 799 | else 800 | term = 'pdf' 801 | end 802 | term = term .. ' enhanced color' 803 | else 804 | local errmsg = 'only eps and png' 805 | if haspdf then 806 | errmsg = errmsg .. ' and pdf' 807 | end 808 | error(errmsg ' for figprint') 809 | end 810 | writeToCurrent('set term ' .. term) 811 | writeToCurrent('set output \''.. fname .. '\'') 812 | refreshCurrent() 813 | writeToCurrent('unset output') 814 | writeToCurrent('set term ' .. _gptable[_gptable.current].term .. ' ' .. _gptable.current .. '\n') 815 | end 816 | 817 | function gnuplot.figure(n, config) 818 | config = config or {} 819 | local gp = getfigure(n) 820 | writeToCurrent('set term ' .. _gptable[_gptable.current].term .. ' ' .. _gptable.current .. '\n') 821 | if not config.noraise then 822 | writeToCurrent('raise') 823 | end 824 | return _gptable.current 825 | end 826 | 827 | function gnuplot.plotflush(n) 828 | if not n then 829 | n = _gptable.current 830 | end 831 | if not n or _gptable[n] == nil then 832 | print('no figure ' .. tostring(n)) 833 | return 834 | end 835 | local gp = _gptable[n] 836 | --xprint(gp) 837 | refreshPlot(gp) 838 | -- if gp.fname then 839 | -- writeToPlot(gp,'set output "' .. gp.fname .. '"') 840 | -- writeToPlot(gp,'refresh') 841 | -- writeToPlot(gp,'unset output') 842 | -- end 843 | end 844 | 845 | local function gnulplot(legend,x,y,format) 846 | local hdr,data = gnuplot_string(legend,x,y,format) 847 | writeToCurrent(hdr) 848 | writeToCurrent(data) 849 | end 850 | local function gnusplot(legend,x,y,z) 851 | local hdr,data = gnu_splot_string(legend,x,y,z) 852 | writeToCurrent(hdr) 853 | writeToCurrent(data) 854 | end 855 | local function gnuscatter3(legend, x, y, z) 856 | local hdr, data = gnu_scatter3_string(legend, x, y, z) 857 | writeToCurrent(hdr) 858 | writeToCurrent(data) 859 | end 860 | local function gnuimagesc(x,palette) 861 | local hdr,data = gnu_imagesc_string(x,palette) 862 | writeToCurrent(hdr) 863 | writeToCurrent(data) 864 | end 865 | 866 | function gnuplot.xlabel(label) 867 | if not _gptable.hasrefresh then 868 | print('gnuplot.xlabel disabled') 869 | return 870 | end 871 | writeToCurrent('set xlabel "' .. label .. '"') 872 | refreshCurrent() 873 | end 874 | function gnuplot.ylabel(label) 875 | if not _gptable.hasrefresh then 876 | print('gnuplot.ylabel disabled') 877 | return 878 | end 879 | writeToCurrent('set ylabel "' .. label .. '"') 880 | refreshCurrent() 881 | end 882 | function gnuplot.zlabel(label) 883 | if not _gptable.hasrefresh then 884 | print('gnuplot.zlabel disabled') 885 | return 886 | end 887 | writeToCurrent('set zlabel "' .. label .. '"') 888 | refreshCurrent() 889 | end 890 | function gnuplot.title(label) 891 | if not _gptable.hasrefresh then 892 | print('gnuplot.title disabled') 893 | return 894 | end 895 | writeToCurrent('set title "' .. label .. '"') 896 | refreshCurrent() 897 | end 898 | function gnuplot.grid(toggle) 899 | if not _gptable.hasrefresh then 900 | print('gnuplot.grid disabled') 901 | return 902 | end 903 | if toggle then 904 | writeToCurrent('set grid') 905 | refreshCurrent() 906 | else 907 | writeToCurrent('unset grid') 908 | refreshCurrent() 909 | end 910 | end 911 | function gnuplot.logscale(toggle) 912 | if not _gptable.hasrefresh then 913 | print('gnuplot.logscale disabled') 914 | return 915 | end 916 | if toggle then 917 | writeToCurrent('set logscale y') 918 | refreshCurrent() 919 | else 920 | writeToCurrent('unset logscale y') 921 | refreshCurrent() 922 | end 923 | end 924 | function gnuplot.movelegend(hloc,vloc) 925 | if not _gptable.hasrefresh then 926 | print('gnuplot.movelegend disabled') 927 | return 928 | end 929 | if hloc ~= 'left' and hloc ~= 'right' and hloc ~= 'center' then 930 | error('horizontal location is unknown : plot.movelegend expects 2 strings as location {left|right|center}{bottom|top|middle}') 931 | end 932 | if vloc ~= 'bottom' and vloc ~= 'top' and vloc ~= 'middle' then 933 | error('horizontal location is unknown : plot.movelegend expects 2 strings as location {left|right|center}{bottom|top|middle}') 934 | end 935 | writeToCurrent('set key ' .. hloc .. ' ' .. vloc) 936 | refreshCurrent() 937 | end 938 | 939 | function gnuplot.axis(axis) 940 | if not _gptable.hasrefresh then 941 | print('gnuplot.axis disabled') 942 | return 943 | end 944 | if axis == 'auto' then 945 | writeToCurrent('set size nosquare') 946 | writeToCurrent('set autoscale') 947 | refreshCurrent() 948 | elseif axis == 'image' or axis == 'equal' then 949 | writeToCurrent('set size ratio -1') 950 | refreshCurrent() 951 | elseif axis == 'fill' then 952 | writeToCurrent('set size ratio 1,1') 953 | refreshCurrent() 954 | elseif type(axis) == 'table' then 955 | if #axis ~= 4 then print('axis should have 4 componets {xmin,xmax,ymin,ymax}'); return end 956 | writeToCurrent('set xrange [' .. axis[1] .. ':' .. axis[2] .. ']') 957 | writeToCurrent('set yrange [' .. axis[3] .. ':' .. axis[4] .. ']') 958 | refreshCurrent() 959 | end 960 | end 961 | 962 | function gnuplot.raw(str) 963 | writeToCurrent(str) 964 | end 965 | 966 | -- plot(x) 967 | -- plot(x,'.'), plot(x,'.-') 968 | -- plot(x,y,'.'), plot(x,y,'.-') 969 | -- plot({x1,y1,'.'},{x2,y2,'.-'}) 970 | -- plot({{x1,y1,'.'},{x2,y2,'.-'}}) 971 | function gnuplot.plot(...) 972 | local arg = {...} 973 | if select('#',...) == 0 then 974 | error('no inputs, expecting at least a vector') 975 | end 976 | 977 | local formats = {} 978 | local xdata = {} 979 | local ydata = {} 980 | local legends = {} 981 | 982 | if type(arg[1]) == "table" then 983 | if type(arg[1][1]) == "table" then 984 | arg = arg[1] 985 | end 986 | for i,v in ipairs(arg) do 987 | local l,x,y,f = getvars(v) 988 | legends[#legends+1] = l 989 | formats[#formats+1] = f 990 | xdata[#xdata+1] = x 991 | ydata[#ydata+1] = y 992 | end 993 | else 994 | local l,x,y,f = getvars(arg) 995 | legends[#legends+1] = l 996 | formats[#formats+1] = f 997 | xdata[#xdata+1] = x 998 | ydata[#ydata+1] = y 999 | end 1000 | gnulplot(legends,xdata,ydata,formats) 1001 | end 1002 | 1003 | -- splot(z) 1004 | -- splot({x1,y1,z1},{x2,y2,z2}) 1005 | -- splot({'name1',x1,y1,z1},{'name2',x2,y2,z2}) 1006 | function gnuplot.splot(...) 1007 | local arg = {...} 1008 | if select('#',...) == 0 then 1009 | error('no inputs, expecting at least a matrix') 1010 | end 1011 | 1012 | local xdata = {} 1013 | local ydata = {} 1014 | local zdata = {} 1015 | local legends = {} 1016 | 1017 | if type(arg[1]) == "table" then 1018 | if type(arg[1][1]) == "table" then 1019 | arg = arg[1] 1020 | end 1021 | for i,v in ipairs(arg) do 1022 | local l,x,y,z = getsplotvars(v) 1023 | legends[#legends+1] = l 1024 | xdata[#xdata+1] = x 1025 | ydata[#ydata+1] = y 1026 | zdata[#zdata+1] = z 1027 | end 1028 | else 1029 | local l,x,y,z = getsplotvars(arg) 1030 | legends[#legends+1] = l 1031 | xdata[#xdata+1] = x 1032 | ydata[#ydata+1] = y 1033 | zdata[#zdata+1] = z 1034 | end 1035 | gnusplot(legends,xdata,ydata,zdata) 1036 | end 1037 | 1038 | -- scatter3(x, y, z) 1039 | -- scatter3({x1, y1, z1}, {x2, y2, z2}) 1040 | -- scatter3({'name1', x1, y1, z1}, {'name2', x2, y2, z2}) 1041 | function gnuplot.scatter3(...) 1042 | local arg = {...} 1043 | if select('#', ...) == 0 then 1044 | error('no inputs, expecting at least a matrix') 1045 | end 1046 | 1047 | local xdata = {} 1048 | local ydata = {} 1049 | local zdata = {} 1050 | local legends = {} 1051 | 1052 | if type(arg[1]) == "table" then 1053 | if type(arg[1][1]) == "table" then 1054 | arg = arg[1] 1055 | end 1056 | for i,v in ipairs(arg) do 1057 | local l, x, y, z = getscatter3vars(v) 1058 | legends[#legends + 1] = l 1059 | xdata[#xdata + 1] = x 1060 | ydata[#ydata + 1] = y 1061 | zdata[#zdata + 1] = z 1062 | end 1063 | else 1064 | local l, x, y, z = getscatter3vars(arg) 1065 | legends[#legends + 1] = l 1066 | xdata[#xdata + 1] = x 1067 | ydata[#ydata + 1] = y 1068 | zdata[#zdata + 1] = z 1069 | end 1070 | gnuscatter3(legends, xdata, ydata, zdata) 1071 | end 1072 | 1073 | -- imagesc(x) -- x 2D tensor [0 .. 1] 1074 | function gnuplot.imagesc(...) 1075 | local arg = {...} 1076 | if select('#',...) == 0 then 1077 | error('no inputs, expecting at least a matrix') 1078 | end 1079 | gnuimagesc(getimagescvars(arg)) 1080 | end 1081 | 1082 | -- bar(y) 1083 | -- bar(x,y) 1084 | function gnuplot.bar(...) 1085 | local arg = {...} 1086 | local nargs = {} 1087 | for i = 1,select('#',...) do 1088 | table.insert(nargs,arg[i]) 1089 | end 1090 | table.insert(nargs, '|') 1091 | gnuplot.plot(nargs) 1092 | end 1093 | 1094 | -- complete function: compute hist and display it 1095 | function gnuplot.hist(tensor,bins,min,max) 1096 | local h = gnuplot.histc(tensor,bins,min,max) 1097 | local x_axis = torch.Tensor(#h) 1098 | for i = 1,#h do 1099 | x_axis[i] = h[i].val 1100 | end 1101 | gnuplot.bar(x_axis, h.raw) 1102 | return h 1103 | end 1104 | 1105 | 1106 | findgnuplot() 1107 | -------------------------------------------------------------------------------- /hist.lua: -------------------------------------------------------------------------------- 1 | -- 2 | -- rudimentary histogram diplay on the command line. 3 | -- 4 | -- Author: Marco Scoffier 5 | -- Date : 6 | -- Mod : Oct 21, 2011 7 | -- + made 80 columns default 8 | -- + save index of max bin in h.max not pointer to bin 9 | -- 10 | local function histc__tostring(h, barHeight) 11 | barHeight = barHeight or 10 12 | local lastm = h[h.max].nb 13 | local incr = lastm/(barHeight+1) 14 | local m = lastm - incr 15 | local tl = torch.Tensor(#h):fill(0) 16 | local toph = '|' 17 | local topm = ':' 18 | local topl = '.' 19 | local bar = '|' 20 | local blank = ' ' 21 | local yaxis = '--------:' 22 | local str = 'nsamples:' 23 | str = str .. 24 | string.format(' min:(bin:%d/#%d/cntr:%2.2f) max:(bin:%d/#%d/cntr:%2.2f)\n', 25 | h.min,h[h.min].nb,h[h.min].val, 26 | h.max,h[h.max].nb,h[h.max].val) 27 | 28 | str = str .. yaxis 29 | for j = 1,#h do 30 | str = str .. '-' 31 | end 32 | str = str .. '\n' 33 | 34 | for i = 1,barHeight do 35 | -- y axis 36 | if i%1==0 then 37 | str = str .. string.format('%1.2e:',m) 38 | end 39 | for j = 1,#h do 40 | if tl[j] == 1 then 41 | str = str .. bar 42 | elseif h[j].nb < m then 43 | str = str .. blank 44 | else 45 | -- in the bracket 46 | tl[j] = 1 47 | -- find 1/3rds 48 | local p = (lastm - h[j].nb) / incr 49 | if p > 0.66 then 50 | str = str .. toph 51 | elseif p > 0.33 then 52 | str = str .. topm 53 | else 54 | str = str .. topl 55 | end 56 | end 57 | end 58 | str = str .. '\n' 59 | lastm = m 60 | m = m - incr 61 | end 62 | -- x axis 63 | str = str .. yaxis 64 | for j = 1,#h do 65 | if ((j - 2) % 6 == 0)then 66 | str = str .. '^' 67 | else 68 | str = str .. '-' 69 | end 70 | end 71 | str = str .. '\ncenters ' 72 | for j = 1,#h do 73 | if ((j - 2) % 6 == 0)then 74 | if h[j].val < 0 then 75 | str = str .. '-' 76 | else 77 | str = str .. '+' 78 | end 79 | str = str .. string.format('%1.2f ',math.abs(h[j].val)) 80 | end 81 | end 82 | return str 83 | end 84 | 85 | -- a simple function that computes the histogram of a tensor 86 | function gnuplot.histc(...) 87 | -- get args 88 | local args = {...} 89 | local tensor = args[1] or error('usage: gnuplot.histc (tensor [, nBins] [, min] [, max]') 90 | local bins = args[2] or 80 - 8 91 | local min = args[3] or tensor:min() 92 | local max = args[4] or tensor:max() 93 | local raw = args[5] or false 94 | 95 | -- compute histogram 96 | local hist = torch.histc(tensor:double(),bins,min,max) 97 | 98 | -- return raw histogram (no extra info) 99 | if raw then return hist end 100 | 101 | -- cleanup hist 102 | local cleanhist = {} 103 | cleanhist.raw = hist 104 | local _,mx = torch.max(cleanhist.raw,1) 105 | local _,mn = torch.min(cleanhist.raw,1) 106 | cleanhist.bins = bins 107 | cleanhist.binwidth = (max-min)/bins 108 | for i = 1,bins do 109 | cleanhist[i] = {} 110 | cleanhist[i].val = min + (i-0.5)*cleanhist.binwidth 111 | cleanhist[i].nb = hist[i] 112 | end 113 | cleanhist.max = mx[1] 114 | cleanhist.min = mn[1] 115 | 116 | -- print function 117 | setmetatable(cleanhist, {__tostring=histc__tostring}) 118 | return cleanhist 119 | end 120 | 121 | -------------------------------------------------------------------------------- /init.lua: -------------------------------------------------------------------------------- 1 | require 'torch' 2 | 3 | gnuplot = {} 4 | require('gnuplot.gnuplot') 5 | require('gnuplot.hist') 6 | 7 | return gnuplot 8 | -------------------------------------------------------------------------------- /mkdocs.yml: -------------------------------------------------------------------------------- 1 | site_name: gnuplot 2 | theme : simplex 3 | repo_url : https://github.com/torch/gnuplot 4 | use_directory_urls : false 5 | markdown_extensions: [extra] 6 | docs_dir : doc 7 | pages: 8 | - [index.md, Gnuplot] 9 | - [plotline.md, Plotting Lines] 10 | - [plotmatrix.md, Plotting Matrices] 11 | - [plotsurface.md, Plotting Surfaces] 12 | - [plothistogram.md, Plotting Histograms] 13 | - [file.md, Saving Plots] 14 | - [common.md, Common Operations] 15 | -------------------------------------------------------------------------------- /rocks/gnuplot-scm-1.rockspec: -------------------------------------------------------------------------------- 1 | package = "gnuplot" 2 | version = "scm-1" 3 | 4 | source = { 5 | url = "git://github.com/torch/gnuplot.git", 6 | } 7 | 8 | description = { 9 | summary = "Torch interface to Gnuplot", 10 | detailed = [[ 11 | ]], 12 | homepage = "https://github.com/torch/gnuplot", 13 | license = "BSD" 14 | } 15 | 16 | dependencies = { 17 | "lua >= 5.1", 18 | "torch >= 7.0", 19 | "paths >= 1.0", 20 | } 21 | 22 | build = { 23 | type = "builtin", 24 | modules = { 25 | ["gnuplot.init"] = "init.lua", 26 | ["gnuplot.gnuplot"] = "gnuplot.lua", 27 | ["gnuplot.hist"] = "hist.lua" 28 | }, 29 | 30 | install = { 31 | lua = { 32 | -- this is ugly (but this is luarocks) 33 | ["gnuplot.README"] = "README.md", 34 | ["gnuplot.doc.plot_filled"] = "doc/plot_filled.png", 35 | ["gnuplot.doc.plot_hist"] = "doc/plot_hist.png", 36 | ["gnuplot.doc.plot_imagesc"] = "doc/plot_imagesc.png", 37 | ["gnuplot.doc.plot_sincos"] = "doc/plot_sincos.png", 38 | ["gnuplot.doc.plot_splot"] = "doc/plot_splot.png", 39 | ["gnuplot.doc.plot_splot2"] = "doc/plot_splot2.png", 40 | ["gnuplot.doc.plot_x"] = "doc/plot_x.png", 41 | ["gnuplot.doc.plot_xyf"] = "doc/plot_xyf.png" 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /test.lua: -------------------------------------------------------------------------------- 1 | require 'gnuplot' 2 | require 'os' 3 | require 'paths' 4 | require 'torch' 5 | 6 | local tester = torch.Tester() 7 | local tests = {} 8 | 9 | -- Returns a random string of lowercase digits 10 | local function randomFilenameStr() 11 | local t = {} 12 | for i = 1, 10 do 13 | table.insert(t, string.char(math.random(97, 122))) 14 | end 15 | return table.concat(t) 16 | end 17 | 18 | -- Make sure we can write to a new filename, but not to a nonexistent directory. 19 | function tests.cannotWriteToNonExistentDir() 20 | -- Save locally, this should work 21 | local validFilename = randomFilenameStr() .. '.png' 22 | 23 | -- If this already exists (bad luck!), don't let the test overwrite it 24 | assert(not (paths.filep(validFilename) or 25 | paths.dirp(validFilename)), 26 | 'random filename aready exists (?)') 27 | 28 | -- Should work fine 29 | gnuplot.pngfigure(validFilename) 30 | gnuplot.plot({'Sin Curve',torch.sin(torch.linspace(-5,5))}) 31 | gnuplot.plotflush() 32 | 33 | -- Clean up after ourselves 34 | os.remove(validFilename) 35 | 36 | -- Now make an invalid output 37 | local nonExistentDir = randomFilenameStr() 38 | assert(not (paths.filep(nonExistentDir) or 39 | paths.dirp(nonExistentDir)), 40 | 'random dir aready exists (?)') 41 | 42 | -- This makes an absolute path below cwd, seems Lua has no way (?) to query 43 | -- the file separator charater by itself... 44 | local invalidFilename = paths.concat(nonExistentDir, validFilename) 45 | local function shouldCrash() 46 | gnuplot.pngfigure(invalidFilename) 47 | end 48 | tester:assertErrorPattern(shouldCrash, 'directory does not exist') 49 | end 50 | 51 | tester:add(tests) 52 | return tester:run() 53 | --------------------------------------------------------------------------------