gnuplotref.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. GNUPLOT reference
  2. Gnuplot An Interactive Plotting Program -
  3. http://www.bersch.net/gnuplot-doc/gnuplot.html
  4. Publication quality plots with Gnuplot -
  5. http://triclinic.org/2015/05/publication-quality-plots-with-gnuplot/
  6. Creating Publication-Ready EPS/PDF files from Gnuplot -
  7. https://michaelgoerz.net/notes/creating-publication-ready-eps-pdf-files-from-gnuplot.html
  8. gnuplot in Action Pg.No: 104 Modifiers for the appearance of lines, points
  9. gnuplot in Action Pg.No: 276 Startup and initialization files
  10. gnuplot in Action Pg.No: 216 Font resolution
  11. gnuplot in Action Pg.No: 218 Tabel 10.1 Control characters for enhanced text mode
  12. # startup and initialization files
  13. gnuplot> show loadpath
  14. # loading script.gp in gnuplot
  15. $ cat script.gp | gnuplot
  16. # loading script.gp with data file.dat in gnuplot
  17. $ cat script.gp file.dat | gnuplot
  18. # load script.gp with data file.dat in gnuplot & direct output plot to graph.png
  19. $ cat script.gp file.dat | gnuplot > graph.png
  20. # complete workflow to export to file
  21. gnuplot> plot exp(-x**2) # plot command
  22. gnuplot> set terminal pngcairo # selects the file format
  23. gnuplot> set output "graph.png" # specifies the output filename
  24. gnuplot> replot # repeats the most recent plot command
  25. gnuplot> set terminal wxt # restores the terminal settings
  26. gnuplot> set output # sends output to the screen again by using
  27. # an empty filename
  28. # saving and loading commands
  29. gnuplot> save "graph.gp"
  30. gnuplot> load "graph.gp"
  31. # examples of user-defined variables and constants
  32. gnuplot> e = 2.71828182845905 # the base of the natural logarithm
  33. gnuplot> sqrt2 = sqrt(2.0) # the square root of 2
  34. gnuplot> euler = 0.57721566490153 # the euler-mascheroni constant
  35. # examples of user-defined functions
  36. f(x) = -x * log(x)
  37. gauss(x,m,s) = exp(-0.5 * ((x-m)/s)**2)/sqrt(2*pi*s**2)
  38. binom(n,k) = n!/(k! * (n-k)!) # uses the factorial operator
  39. min(a,b) = (a < b) ? a : b # uses the ternary ? operator
  40. step(x) = (x < 0) ? 0 : 1 # a piece-wise defined function
  41. g(x) = exp(-0.5*(x/s)**2)/s
  42. plot s=1 g(x), s=2 g(x), s=3 g(x)
  43. g(x,s) = exp(-0.5*(x/s)**2)/s
  44. plot g(x,1), g(x,2), g(x,3)
  45. f(x) = abs(x) < 1 ? 1 : NaN
  46. # loop over numbers
  47. plot "data" u 1:2 w l, "" u 1:3 w l, "" u 1:4 w l
  48. plot for [j=2:4] "data" u 1:j w l # obtain the same plot as in the previous
  49. # modifiers for the appearence of lines, plots, and other graph elements
  50. Keyword Abbreviation (Ref: gnuplot in Action, Pg.No: 104)
  51. -------------------------------------
  52. linetype lt
  53. linestyle ls
  54. linewidth lw
  55. linecolor lc
  56. dashtype dt
  57. pointtype pt
  58. pointsize ps
  59. pointinterval pi
  60. textcolor tc
  61. fillstyle fs
  62. fillcolor fc
  63. border bo
  64. arrowstyle as
  65. plot pl
  66. splot spl
  67. with w
  68. with lines w l
  69. with points w p
  70. with linespoints w lp
  71. with vectors w vec
  72. using u
  73. line and point lp
  74. index i
  75. every ev
  76. smooth s
  77. smooth acsplines s acs
  78. smooth frequency s f
  79. smooth kdensity s kdens
  80. title t
  81. set output set o
  82. set terminal set t
  83. set logscale set logsc
  84. # set margin
  85. set margins <left>, <right>, <bottom>, <top>
  86. # plot two functions
  87. gnuplot> pl sin(x) t 'sine', cos(x) t 'cosine'
  88. gnuplot> spl sin(x)*cos(x) t 'sinusoid'
  89. # for information on plotting 3-D data
  90. gnuplot> help splot
  91. gnuplot> show version long
  92. gnuplot> set terminal
  93. gnuplot> show terminal
  94. terminal type is x11
  95. gnuplot> show samples
  96. sampling rate is 1000, 1000
  97. gnuplot> set samples 5000
  98. gnuplot> show samples
  99. sampling rate is 5000, 5000
  100. gnuplot> plot sin(x) ls 2 # line style changes color (prefer: 1,3,6,7,8,9,11)
  101. gnuplot> plot sin(x) lw 4 # line width changes width
  102. gnuplot> plot sin(x) ps 3 # point style changes point
  103. Display terminal styles with test
  104. gnuplot> test
  105. At any point during an interactive gnuplot session you can store the command
  106. used to create the most recent plot to a file with save:
  107. gnuplot> save "filename.gp"
  108. You can run a command file, either for the interactive prompt with load
  109. gnuplot> load 'filename.gp"
  110. Or by running the file directly form the terminal prompt
  111. $ chmod a+x filename.gp
  112. $ ./filename.gp
  113. The default ordinate range is not always good
  114. gnuplot> plot sin(x), x, x-(x**3)/6
  115. Change the default ordinate range:
  116. gnuplot> plot [] [-2:2] sin(x), x, x-(x**3)/6
  117. [] [] - default x and y range value
  118. [] [-2:2] - default x but range value of y is -2:2
  119. gnuplot> set xrange [0:1]
  120. gnuplot> set yrange [-2:2]
  121. Plotting data from a file:
  122. * By default gnuplot expects data to be numeric and whitespace separated.
  123. * If datafile contains multiple columns, use the using directive to specify
  124. which columns to plot.
  125. * Can plot multiple datasets on the same plot.
  126. gnuplot> plot "filename" using 1:2, "filename" u 1:3
  127. * By default data points from a file are plotted, we can style the data using
  128. the with directive.
  129. gnuplot> plot "filename" u 1:2 with lines, " " u 1:3 w linespoints/linesp
  130. gnuplot> plot "filename" u 1:2 w l title "XYZ", " " u 1:3 w linesp t "PQR"
  131. gnuplot> set xlabel "xlabel_name"
  132. gnuplot> set ylabel "ylabel_name"
  133. gnuplot> replot # replot the last command with the new settings
  134. Smooth unique:
  135. The smooth unique commands allows data sorting on the fly
  136. gnuplot> plot "jumbled" w lp
  137. gnuplot> plot "jumbled" smooth unique w lp t "unjumbled"
  138. gnuplot offers other options to interpolate and smooth data
  139. * bezier calculates and plots a Bezier approximation of order n, where n is the
  140. number of data points
  141. * sbezier applies the unique transformation first then calculates and plots a
  142. Bezier approximation
  143. * csplines applies the unique transformation first then calculates natural cubic
  144. splines to the results
  145. * acsplines applies the unique transformation first then forms a weighted cubic
  146. splines approximation. The weight is taken from a third argument to the using
  147. directive.
  148. User defined functions:
  149. gnuplot allows the user to define functions and variables for plotting.
  150. gnuplot> g(x) = a*cos(x)/a
  151. gnuplot> plot a=1, g(x), a=2, g(x)
  152. Data transformations:
  153. Arbitrary functions (include user defined functions) can be applied to each data
  154. point of a file.
  155. gnuplot> g(x) = a*cos(x)/a
  156. gnuplot> a = 23
  157. gnuplot> plot "filename" u 1:(g($1+$2))
  158. The column 0 is a psuedocolumn containing the line number of the datafile
  159. gnuplot> plot "filename" using 0:1
  160. Plotting with style:
  161. * gnuplot itself knows very little about rendering the style of the points,
  162. lines, fill, etc. This is left to the terminal.
  163. * Line and point styles can be changed by explicitly identifying the line/point
  164. type associated with the terminal.
  165. * Common style directives
  166. * linestyle (ls)
  167. * pointstyle (ps)
  168. * lineweight (lw)
  169. * Display terminal styles with test
  170. gnuplot> test
  171. References: http://idl.utsa.edu/me5013/lessons/ # for pdf slides
  172. * Book: Gnuplot in Action, P.K. Janert
  173. * Gnuplot -not so Frequently Asked Questions
  174. * Official documentation
  175. * Reference Card
  176. # png output
  177. gnuplot> set terminal pngcairo
  178. # pdf output
  179. gnuplot> set terminal pdfcairo
  180. # svg output
  181. gnuplot> set terminal svg
  182. gnuplot: Terminal types and LATEX Integration
  183. gnuplot Terminals:
  184. * The terminal is what actually renders the graphics in gnuplot
  185. * Many different types are available, mainly grouped as follows
  186. * Standard Graphics Formats - GIF, JPG, PNG, SVG, etc.
  187. * Graphics for print - PostScript, PDF
  188. * Interactive terminals - x11, wxt, Aqua, dumb
  189. * To see a full listing, launch gnuplot and type
  190. gnuplot> set terminal
  191. gnuplot> set term dumb
  192. gnuplot> plot sin(x)
  193. gnuplot> set term png
  194. gnuplot> set output "filename.png"
  195. gnuplot> replot
  196. gnuplot> exit()
  197. # plot a sinusoid and cosinus function
  198. gnuplot> a = 0.9
  199. gnuplot> f(x) = a * sin(x)
  200. gnuplot> g(x) = a * cos(x)
  201. gnuplot> plot [-2*pi:2*pi] f(x) title "wave" w l ls 1 g(x) notitle w l ls 2
  202. $ gnuplot -p plotscript.gp
  203. The command line option -p instructs gnuplot to keep the figure on the screen
  204. after finishing plotting.
  205. # postscript terminal
  206. gnuplot> set terminal postscript enhanced color
  207. gnuplot> set output "result.ps"
  208. # plotting each file in a directory
  209. To plot every file in the current directory, either of the following two
  210. commands will work:
  211. gnuplot> files = system("ls")
  212. gnuplot> plot for [f in files] f u 1:2 # uses the system() function
  213. gnuplot> plot for [f in "`ls`"] f u 1:2 # uses backticks
  214. # batch processing
  215. $ gnuplot plot1.gp plot2.gp plot3.gp # using command-line arguments
  216. $ cat plot1.gp plot2.gp plot3.gp | gnuplot # reading from standard input
  217. gnuplot> load "plot1.gp" # from within a gnuplot session
  218. gnuplot> load "plot2.gp"
  219. gnuplot> load "plot3.gp"
  220. # do loop
  221. set multiplot layout 2,2
  222. do for [name in "A B C D"] {
  223. filename = name . ".dat"
  224. set title sprintf("Condition %s", name)
  225. plot filename title name
  226. }
  227. unset multiplot
  228. # the built-in color sequences
  229. [Gnuplot in Action 2nd Edition, Chapter 9. Color, style, and appearance]
  230. set colorsequence [ default | classic | podo ]
  231. The three color schemes are identified using keywords:
  232. * default - A terminal-independent sequence of eight colors
  233. * classic - The original behavior, which differs from terminal to terminal
  234. but usually produces the red/green/blue sequence
  235. * podo - A terminal-independent sequence of eight colors, intended to
  236. be easy for color-blind people to distinguish
  237. Tip: Because the three built-in color sequences aren't very good, you should
  238. consider defining and using a custom sequence.
  239. Color specifications tend to be clumsy; they're both lengthy and obscure. It may
  240. therefore be a good idea to introduce shorthands.
  241. Instead of using a hex string, you can assign its value to a semantically named
  242. variable:
  243. gold = "#ffd700" <---- Hex string
  244. gold = pack(0xff, 0xd7, 0) <---- Equivalent: integer value
  245. plot "data" u 1:2 w l lc rgb gold
  246. This isn't ideal, because you still have to type the keyword rgb every time. You
  247. can absorb it into the variable if you use gnuplot's macro facility.
  248. blue = "rgb '#0000ff'"
  249. plot "data" u 1:2 w l lc @blue
  250. You can even include the lc keyword in the macro:
  251. green = "lc rgb 'green'"
  252. plot "data" u 1:2 w l @green
  253. Rather than use color names, you may also want to consider semantic names for
  254. colors:
  255. alarm = "red"
  256. normal = "#00cd00"
  257. # 2D plot of matrix data
  258. Plot a matrix in gnuplot having such data structure, using the first row and
  259. column as a x and y ticks (the first number of the first row is the number of
  260. columns) and represent the rest of the values by a color mapping so it can be
  261. seen on a 2D plane.
  262. * with image
  263. (regular grid, no interpolation, one quadrangle for each data point)
  264. set autoscale xfix
  265. set autoscale yfix
  266. set autoscale cbfix
  267. plot 'data' matrix nonuniform with image notitle
  268. * with pm3d
  269. (supports also irregular grids and interpolation, plots one quadrangle for four
  270. neighboring data points)
  271. set autoscale xfix
  272. set autoscale yfix
  273. set autoscale cbfix
  274. set pm3d map
  275. splot 'data' matrix nonuniform notitle
  276. # pass gnuplot a matrix in C
  277. #include <stdio.h>
  278. int main(void) {
  279. FILE *gp = popen("gnuplot -persist", "w");
  280. fprintf(gp, "plot '-' matrix with image pixels notitle\n");
  281. for (int i = 0; i < 10; i++) {
  282. for (int j = 0; j < 10; j++)
  283. fprintf(gp, "%d ", matrix[i][j]);
  284. fprintf(gp, "\n");
  285. }
  286. pclose(gp);
  287. return 0;
  288. }