tag:blogger.com,1999:blog-24088470627814755092024-03-05T01:40:45.082-08:00Surrey Physics PlottersGnuplot tips and tricks from University of Surrey PhysicistsBlog of the Isotopeshttp://www.blogger.com/profile/09709830920524368020noreply@blogger.comBlogger10125tag:blogger.com,1999:blog-2408847062781475509.post-69446282694499311272024-01-04T07:30:00.000-08:002024-01-04T07:30:36.183-08:00Gnuplot 6.0.0<p> I noticed that Gnuplot 6.0.0 is out. Details of new features can be seen in the <a href="http://www.gnuplot.info/ReleaseNotes_6_0_0.html">release notes</a>. I'm excited about the possibility of sector plots, in particular, it can make "polar equivalent to sparse-matrix heatmaps." This is a kind of plot I do actually need to make for my research, and have outsourced the work to knowledgeable postdocs, or made somewhat ugly ones with matplotlib. I'm keen to see what I can do with Gnuplot (while acknowledging that it is me, rather than matplotlib per se that stop me from making the plots I want to with mpl)<br /></p>Blog of the Isotopeshttp://www.blogger.com/profile/09709830920524368020noreply@blogger.com0tag:blogger.com,1999:blog-2408847062781475509.post-21805376535092770962020-06-22T07:18:00.001-07:002020-06-22T07:18:41.512-07:00Separate y-axis on the right<div>If you want to have two independent y-axis – one on the left, and one on the right – you can unset the mirroring of the "main" left-hand y-axis marks with <span style="font-family: courier;">set ytics nomirror</span>, and use <span style="font-family: courier;">axis x1y2</span> in the plot command to use the right-hand y-axis. See the following example for a basic unembellished example<br /></div><div><br /></div><div><span style="font-family: courier;"><font size="2">gnuplot> set y2tics<br />gnuplot> set ytics nomirror<br />gnuplot> set ylabel "Energy [MeV]"<br />gnuplot> set y2label "{/Symbol b}_2"<br />gnuplot> set termopt enh</font></span></div><div><span style="font-family: courier;"><font size="2">gnuplot> set xlabel "index"<br />gnuplot> plot 'ensurf.all' u 0:13 w l t "Binding Energy", '' u 0:4 axis x1y2 w l t "deformation ({/Symbol b}_2)"</font></span><br /></div><div><br /></div><div>and the result is shown in the screengrab below</div><div><br /></div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKmUalKmSVIBHr4HkyzrRhaOMccNruQDz-lzpOZvvw3eat5b4Sr-CyVuAZ66nQQTN5jEeiUjFPQ_oLGOW9uoFBPNheOhO0ZbC6mWKkqy90cRX0lrEnTf5yivON_9G8WezyTQN0ZDtLDrDX/s635/Screen+Shot+2020-06-22+at+15.17.30.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="472" data-original-width="635" height="373" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjKmUalKmSVIBHr4HkyzrRhaOMccNruQDz-lzpOZvvw3eat5b4Sr-CyVuAZ66nQQTN5jEeiUjFPQ_oLGOW9uoFBPNheOhO0ZbC6mWKkqy90cRX0lrEnTf5yivON_9G8WezyTQN0ZDtLDrDX/w500-h373/Screen+Shot+2020-06-22+at+15.17.30.png" width="500" /></a></div><div><br /></div>Blog of the Isotopeshttp://www.blogger.com/profile/09709830920524368020noreply@blogger.com0tag:blogger.com,1999:blog-2408847062781475509.post-88108989844215114142018-07-03T07:54:00.005-07:002019-02-13T00:11:46.720-08:00Dash it all: Custom line styles<div dir="ltr" style="text-align: left;" trbidi="on">
Lines in gnuplot can come in different styles; solid lines, dashed lines, or dotted lines, for example. Probably the easiest way to set these is to use the <span style="font-family: "courier new" , "courier" , monospace;">dashtype pattern</span> specifier, in gnuplot 5 and above. The gnuplot help guide says:<br />
<div>
<br /></div>
<div>
<div style="background-color: #dfdbc3; color: #4d2f2d; font-family: Courier; font-size: 12px; font-stretch: normal; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> dashtype "pattern" # string containing a combination of the characters</span></div>
<div style="background-color: #dfdbc3; color: #4d2f2d; font-family: Courier; font-size: 12px; font-stretch: normal; line-height: normal;">
<span style="font-variant-ligatures: no-common-ligatures;"> # dot (.) hyphen (-) underscore(_) and space.</span></div>
</div>
<div>
<span style="font-variant-ligatures: no-common-ligatures;"><br /></span></div>
<div>
and some examples are shown below:</div>
<div>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace;">set term pdfcairo font "Chalkduster,15"</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">set out 'dashes.pdf'</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">set key samplen 12 opaque box</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">PI = acos(-1.)</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">set xrange [0:2*PI]</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">plot sin(x) dashtype '-' lw 3 t 'Sin(x)', \</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> sin(2*x) dashtype '.' lw 3 t 'Sin(2x)', \</span><br />
<span style="font-family: "courier new" , "courier" , monospace;"> sin(3*x) dashtype '. - _' lw 3 t 'Sin(3x)', \</span><br />
<span style="font-family: "courier new" , "courier" , monospace;">0 not</span></div>
<div style="text-align: left;">
<br /></div>
<div>
Here we set up a pdf output, with a somewhat silly choice of font -- and one that I checked existed on my system. I set up the key to include a very long length line in the legend, because I am making up an example with a complicated dash type.</div>
<div>
<br /></div>
<div>
The <span style="font-family: "courier new" , "courier" , monospace;">plot</span> line plots the functions <span style="font-family: "courier new" , "courier" , monospace;">sin(nx), n=1,2,3 </span>and assigns each a different dash type: <span style="font-family: "courier new" , "courier" , monospace;">'-'</span>, <span style="font-family: "courier new" , "courier" , monospace;">'.'</span> and <span style="font-family: "courier new" , "courier" , monospace;">'. - _'</span>for dashes, dots, and a more complicated pattern. The results are shown below:</div>
<div>
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHd35EBd7qVoxE7CAqIzuLZNPp1b_-QQqoQKk1oLDhAja4QBEa0Llk7CnRZYXm-71qPeyzBiDyXg2_YBug3BPV-ZqQcLBg-W4TD3Y-yn4gCIakKFoYEsM5I32IjJZvjAxWFYQ6ovWJRUlt/s1600/dashes.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" data-original-height="480" data-original-width="640" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHd35EBd7qVoxE7CAqIzuLZNPp1b_-QQqoQKk1oLDhAja4QBEa0Llk7CnRZYXm-71qPeyzBiDyXg2_YBug3BPV-ZqQcLBg-W4TD3Y-yn4gCIakKFoYEsM5I32IjJZvjAxWFYQ6ovWJRUlt/s1600/dashes.png" /></a></div>
<div>
<br /></div>
</div>
Blog of the Isotopeshttp://www.blogger.com/profile/09709830920524368020noreply@blogger.com1tag:blogger.com,1999:blog-2408847062781475509.post-70864776861027211082016-02-19T02:16:00.000-08:002018-07-03T07:57:11.319-07:00A pm3d surface plot<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
Here is a quick example of visualising a function of two variables using the pm3d "palette-mapped" 3d plot. It comes from a module I teach on computational techniques and is the end result of a solution of the Laplace equation for the electrical potential, in a square region in which the potential along the upper boundary of the square is held at +100V and is 0V along the other boundaries. The data, in the file <span style="font-family: "courier new" , "courier" , monospace;">pot.dat</span>, is <a href="http://personal.ph.surrey.ac.uk/%7Ephs3ps/blog/pot.dat">here</a>.</div>
<blockquote>
<pre><span style="color: darkred;"># set up the terminal to be the interactive wxt display,
# but with equal width and height</span>
set term wxt size 400,400
<span style="color: darkred;"># we will be plotting a function of two variables, but we
# don't want the default surface plot</span>
unset surface
<span style="color: darkred;"># view from above</span>
set view 0,90
<span style="color: darkred;"># set the coloured "pm3d" plot (rather than the surface,
# which we unset above)</span>
set pm3d
<span style="color: darkred;"># viewing from above, we don't want to have the z-axis
# tick marks shown all bunched up</span>
unset ztics
<span style="color: darkred;"># now plot, with a title</span>
splot 'pot.dat' title "Potential"
</pre>
</blockquote>
<div>
The result, loaded into gnuplot with a <span style="font-family: "courier new" , "courier" , monospace;">load './pot.gpl'</span> command is shown below:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_55EQaz8saMe2tgId7t0Z364HgHTvWNmEUa5iyNlN-TnchRLc3l6RhBncmAjQstH7hBuQ0HpGyImHCFM7JCDh9S6pwqO7m-Gv7_XXglrKTHWvRoa-MOCHRg9XhWASd0cPS40SlWIUiMT9/s1600/Screen+Shot+2016-02-19+at+10.14.51.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj_55EQaz8saMe2tgId7t0Z364HgHTvWNmEUa5iyNlN-TnchRLc3l6RhBncmAjQstH7hBuQ0HpGyImHCFM7JCDh9S6pwqO7m-Gv7_XXglrKTHWvRoa-MOCHRg9XhWASd0cPS40SlWIUiMT9/s320/Screen+Shot+2016-02-19+at+10.14.51.png" width="282" /></a></div>
<br />
It's not perfect. With a bit of tweaking with placement, the title could be centrally above the square of the plot and the whitespace more even in the frame, but it visualises the potential quite well.<br />
<br /></div>
</div>
Blog of the Isotopeshttp://www.blogger.com/profile/09709830920524368020noreply@blogger.com1tag:blogger.com,1999:blog-2408847062781475509.post-26861559511148020682016-01-12T02:12:00.000-08:002016-11-10T00:48:54.152-08:00Quicker and dirtier one-line printing<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: justify;">
In a <a href="http://spplotters.blogspot.co.uk/2015/07/a-quick-one-liner-for-plotting-multiple.html">previous post</a>, I wrote about how to use a <span style="font-family: "courier new" , "courier" , monospace;">for</span> loop and the <span style="font-family: "courier new" , "courier" , monospace;">system</span> command to plot several data sets from different files in one go. Taking that idea a bit further, one can make use of the <span style="font-family: "courier new" , "courier" , monospace;">dumb</span> terminal type and a Unix pipe (assuming one is using a variety of Unix, such as OS X or Linux) to have a completely self-contained one-liner from a Unix shell. This can be particularly useful for a quick way to check results running on a distant server that you want to connect to remotely via <span style="font-family: "courier new" , "courier" , monospace;">ssh</span> and don't have the bandwidth to comfortably bring up graphical windows via X.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
An example, building on the previous one, along with the result, is shown below:</div>
<div style="text-align: justify;">
<br /></div>
<div style="background-color: #dfdbc4; color: #4c2f2d; font-size: 12px; line-height: normal;">
<span style="font-family: "Courier New",Courier,monospace;">% echo "set term dumb ; plot for [file in system('ls -1 */quadrupoles.res')] file u 1:4 w l t file" | gnuplot</span></div>
<div style="background-color: #dfdbc4; color: #4c2f2d; font-size: 12px; line-height: normal; min-height: 14px;">
<span style="font-family: "Courier New",Courier,monospace;"><br /></span></div>
<div style="background-color: #dfdbc4; color: #4c2f2d; font-size: 12px; line-height: normal; min-height: 14px;">
<span style="font-family: "Courier New",Courier,monospace;"><br /></span></div>
<div style="background-color: #dfdbc4; color: #4c2f2d; font-size: 12px; line-height: normal; min-height: 14px;">
<span style="font-family: "Courier New",Courier,monospace;"><br /></span></div>
<div style="background-color: #dfdbc4; color: #4c2f2d; font-size: 12px; line-height: normal;">
<span style="font-family: "Courier New",Courier,monospace;"> 1200 ++---------+-----------+----------+----------+-----------+---------++</span></div>
<div style="background-color: #dfdbc4; color: #4c2f2d; font-size: 12px; line-height: normal;">
<span style="font-family: "Courier New",Courier,monospace;"> * + + + 66mev/quadrupoles.res ****** +</span></div>
<div style="background-color: #dfdbc4; color: #4c2f2d; font-size: 12px; line-height: normal;">
<span style="font-family: "Courier New",Courier,monospace;"> * 69mev/quadrupoles.res ###### |</span></div>
<div style="background-color: #dfdbc4; color: #4c2f2d; font-size: 12px; line-height: normal;">
<span style="font-family: "Courier New",Courier,monospace;"> 1000 +* 70mev/quadrupoles.res $$$$$$++</span></div>
<div style="background-color: #dfdbc4; color: #4c2f2d; font-size: 12px; line-height: normal;">
<span style="font-family: "Courier New",Courier,monospace;"> |* 71mev/quadrupoles.res %%%%%% |</span></div>
<div style="background-color: #dfdbc4; color: #4c2f2d; font-size: 12px; line-height: normal;">
<span style="font-family: "Courier New",Courier,monospace;"> |#* |</span></div>
<div style="background-color: #dfdbc4; color: #4c2f2d; font-size: 12px; line-height: normal;">
<span style="font-family: "Courier New",Courier,monospace;"> 800 ++* ++</span></div>
<div style="background-color: #dfdbc4; color: #4c2f2d; font-size: 12px; line-height: normal;">
<span style="font-family: "Courier New",Courier,monospace;"> | * |</span></div>
<div style="background-color: #dfdbc4; color: #4c2f2d; font-size: 12px; line-height: normal;">
<span style="font-family: "Courier New",Courier,monospace;"> | * |</span></div>
<div style="background-color: #dfdbc4; color: #4c2f2d; font-size: 12px; line-height: normal;">
<span style="font-family: "Courier New",Courier,monospace;"> 600 ++ #* ++</span></div>
<div style="background-color: #dfdbc4; color: #4c2f2d; font-size: 12px; line-height: normal;">
<span style="font-family: "Courier New",Courier,monospace;"> | * % |</span></div>
<div style="background-color: #dfdbc4; color: #4c2f2d; font-size: 12px; line-height: normal;">
<span style="font-family: "Courier New",Courier,monospace;"> | %* %%%%$$$$$$$$$$$$$$$$$$$$ |</span></div>
<div style="background-color: #dfdbc4; color: #4c2f2d; font-size: 12px; line-height: normal;">
<span style="font-family: "Courier New",Courier,monospace;"> | * %$############ $$$$$$$$$ |</span></div>
<div style="background-color: #dfdbc4; color: #4c2f2d; font-size: 12px; line-height: normal;">
<span style="font-family: "Courier New",Courier,monospace;"> 400 ++ #* %##******* #### $$$$++</span></div>
<div style="background-color: #dfdbc4; color: #4c2f2d; font-size: 12px; line-height: normal;">
<span style="font-family: "Courier New",Courier,monospace;"> | ** %****** **** ##** $$</span></div>
<div style="background-color: #dfdbc4; color: #4c2f2d; font-size: 12px; line-height: normal;">
<span style="font-family: "Courier New",Courier,monospace;"> | ** **** *** ** |</span></div>
<div style="background-color: #dfdbc4; color: #4c2f2d; font-size: 12px; line-height: normal;">
<span style="font-family: "Courier New",Courier,monospace;"> 200 ++ ** *** *** ++</span></div>
<div style="background-color: #dfdbc4; color: #4c2f2d; font-size: 12px; line-height: normal;">
<span style="font-family: "Courier New",Courier,monospace;"> | ** %** |</span></div>
<div style="background-color: #dfdbc4; color: #4c2f2d; font-size: 12px; line-height: normal;">
<span style="font-family: "Courier New",Courier,monospace;"> + *** + + + + +</span></div>
<div style="background-color: #dfdbc4; color: #4c2f2d; font-size: 12px; line-height: normal;">
<span style="font-family: "Courier New",Courier,monospace;"> 0 ++---------+-----------+----------+----------+-----------+---------++</span></div>
<div style="background-color: #dfdbc4; color: #4c2f2d; font-size: 12px; line-height: normal;">
<span style="font-family: "Courier New",Courier,monospace;"> 0 100 200 300 400 500 600</span></div>
<div style="text-align: justify;">
</div>
<div style="background-color: #dfdbc4; color: #4c2f2d; font-family: Courier; font-size: 12px; line-height: normal; min-height: 14px;">
<br /></div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
Now, along with the <span style="font-family: "courier new" , "courier" , monospace;">plot for [file in ...</span> construct, we have an extra gnuplot command <span style="font-family: "courier new" , "courier" , monospace;">set term dumb</span> to make the output be character-based in the terminal window. The Unix <span style="font-family: "courier new" , "courier" , monospace;">echo</span> command prints the whole set of gnuplot instructions, and those are then piped to the gnuplot program using the Unix pipe symbol (<span style="font-family: "courier new" , "courier" , monospace;">|</span>).</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
The dumb terminal by default uses a 'display' which is 79x24 characters, which is suitable for most default situations. If you are running from a Terminal window in a windowing environment (the most common situation at the time of writing) then you can always drag your terminal to be much bigger and then request a more detailed plot. The result is shown as a graphic below (unlike the above literal text result) so that it can be squashed down to fit in the blog post. When used in anger, the font is just the same as the example above. The difference in the command leading up to this figure is in writing <span style="font-family: "courier new" , "courier" , monospace;">set term dumb 300 75</span> to give 300 characters in the x direction and 75 in the y direction.</div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlgjx4LRZX9X2_gBt0bWV2os0brH0nmc1o0gA9-4IEH3O4tn8Cmx2WNbPDiIK646LZMkYstAx-PB0s3MmtPq-lBA_JdFvXf0MWgKUXnsl7DYmhbyqgXusopFuTlNO8yj37jVByk2ecTKgw/s1600/Screen+Shot+2016-01-12+at+10.10.29.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img alt="gnuplot textual plot" border="0" height="322" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlgjx4LRZX9X2_gBt0bWV2os0brH0nmc1o0gA9-4IEH3O4tn8Cmx2WNbPDiIK646LZMkYstAx-PB0s3MmtPq-lBA_JdFvXf0MWgKUXnsl7DYmhbyqgXusopFuTlNO8yj37jVByk2ecTKgw/s640/Screen+Shot+2016-01-12+at+10.10.29.png" title="" width="640" /></a></div>
<div style="text-align: justify;">
<br /></div>
<div style="text-align: justify;">
<br /></div>
</div>
Blog of the Isotopeshttp://www.blogger.com/profile/09709830920524368020noreply@blogger.com0tag:blogger.com,1999:blog-2408847062781475509.post-14429332112164344562015-07-21T01:39:00.000-07:002015-09-11T04:08:07.031-07:00A quick one-liner for plotting multiple files<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhEsuvPjXscqqIkp8KXD19gzdOkq33lbJzMLjJ9rV53wJ2aZV3zfOLGLXAvqbmYCkxCpd4oaI51uOIQEjjy8Aa7cccpdCe5z0vnQ8ep72lM18k516qXmRAW27_qnuxXex7qANCj9GCWHS0A/s1600/qmom.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhEsuvPjXscqqIkp8KXD19gzdOkq33lbJzMLjJ9rV53wJ2aZV3zfOLGLXAvqbmYCkxCpd4oaI51uOIQEjjy8Aa7cccpdCe5z0vnQ8ep72lM18k516qXmRAW27_qnuxXex7qANCj9GCWHS0A/s320/qmom.png" width="320" /></a></div>
<div style="text-align: justify;">
<span style="font-family: Verdana, sans-serif;">I have a series of directories, each with a file of the same name, containing numerical data to do with calculations of nuclear fusion. Each data set differs by the starting condition, and I'd like to be able to quickly show all the different calculations together, and see a visual update of them all, while my code is generating the files.</span></div>
<div style="text-align: justify;">
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-family: Verdana, sans-serif;">A quick one-liner, which does this is</span></div>
<br />
<div style="background-color: #dfdbc4; color: #4c2f2d; font-family: Courier; font-size: 12px;">
<br /></div>
<div style="background-color: #dfdbc4; color: #4c2f2d; font-family: Courier; font-size: 12px;">
plot for [file in system("ls -1 */qmom")] file u 0:1 w l t file</div>
<div style="background-color: #dfdbc4; color: #4c2f2d; font-family: Courier; font-size: 12px;">
<br /></div>
<br />
<div style="text-align: justify;">
<span style="font-family: Verdana, sans-serif;">In this case, the file in each directory is called </span><span style="font-family: Courier New, Courier, monospace;">qmom</span><span style="font-family: Verdana, sans-serif;">. The for loop syntax </span><span style="font-family: Courier New, Courier, monospace;">for [a in list] </span><span style="font-family: Verdana, sans-serif;">allows an arbitrary list of text strings to be looped over and assigned to a variable. This allows for a really quick way to produce a plot comparing data from several files.</span></div>
<div style="text-align: justify;">
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<div style="text-align: justify;">
<span style="font-family: Verdana, sans-serif;">Of course, extra commands to prettify the plot to one's taste would probably be needed for a production plot. The attached plot has had a bit of such stuff done (as well as adding labels etc.)</span></div>
<div style="text-align: justify;">
<br /></div>
<br /></div>
Blog of the Isotopeshttp://www.blogger.com/profile/09709830920524368020noreply@blogger.com0tag:blogger.com,1999:blog-2408847062781475509.post-84857758787162549072013-07-23T03:52:00.001-07:002013-07-23T03:54:55.987-07:00Beautiful splotting<div dir="ltr" style="text-align: left;" trbidi="on">
Recently, I've been doing rather a lot of R-matrix analysis. In short this means I'm trying to understand the probability of nuclear reactions occurring as a function of both energy and scattering angle. The gnuplot splot command is great for viewing the calculations and data as a function of two variables, and makes fitting the data much easier.<br />
<br />
<div class="separator" style="clear: both; text-align: left;">
However, with many data points, depth perception can get a little tricky. For example, a typical plot might look something like this:</div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiONPwbLnv6w4GWrRnWOV9myB_6aMJVVSbuzyOXxTLlTyZTCyqCUVOd0O_RQPWMtZbj6dc6AWgPrx2RHODXyiwPHIZFmv1rPmLnMzK6IChuE32n5ssJm_JtlcLfnEDuFSB7PdURQoc9XDHD/s1600/splotting1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="300" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiONPwbLnv6w4GWrRnWOV9myB_6aMJVVSbuzyOXxTLlTyZTCyqCUVOd0O_RQPWMtZbj6dc6AWgPrx2RHODXyiwPHIZFmv1rPmLnMzK6IChuE32n5ssJm_JtlcLfnEDuFSB7PdURQoc9XDHD/s400/splotting1.png" width="400" /></a></div>
<br />
In this case, the agreement between the curve and the points isn't fantastic, but it simply isn't clear which point should be compared to which line. You can't easily distinguish both the x and y coordinates of each point. Even interactively rotating the plot doesn't help much when the point density is this high.<br />
<br />
The tip here is to colour the points and calculations to help depth perception. Adding the palette option to the splot command will colour by a fourth variable that is appended to the column specification. Here's the sample code:<br />
<br />
<blockquote class="ecs_bq_code">
<b>set</b> terminal png enhanced size 800,600 font "sans, 16" <br />
<br />
<b>set</b> xlabel "Energy (MeV)"<br />
<b>set</b> ylabel "Angle (degrees)" offset 0,-1,0<br />
<b>set</b> zlabel "Cross section (mb)" rotate by 90<br />
<br />
<b>set</b> palette model RGB defined ( 0.0 'black', 0.3 'purple', 0.5 "blue", 0.7 "green", 0.9 "orange", 1.0 "red" )<br />
<br />
<b>set</b> output "splotting.png" <br />
<b>splot</b> "data.dat" u 1:3:6:3 w p ps 1 palette notitle,\<br />
"data.dat" u 1:3:4:3 w l lw 2 palette notitle</blockquote>
<br />
In this case, all the data is in data.dat. Columns 1 and 3 contain the energy and angle respectively. Column 4 and 6 contain the data points and the calculations. The fourth specifier (3) tells gnuplot to colour things by the angle - in this case, the y-axis variable. The resulting plot looks like this:<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZ0vvHDF7hINrSXsDK5xQ5AL47_AeajBGXXApZQwg1Ul3PmR8c6jzDlW9sGOVqLLY6Lw8CBY0U814Q9qDuqLqSUf7FutSqmPjRWoLh97QJWkXWqGmv4MgKF7IqkmuZMD9mCL1Uz9kBJvR1/s1600/splotting2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="300" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZ0vvHDF7hINrSXsDK5xQ5AL47_AeajBGXXApZQwg1Ul3PmR8c6jzDlW9sGOVqLLY6Lw8CBY0U814Q9qDuqLqSUf7FutSqmPjRWoLh97QJWkXWqGmv4MgKF7IqkmuZMD9mCL1Uz9kBJvR1/s400/splotting2.png" width="400" /> </a></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
<div class="separator" style="clear: both; text-align: left;">
It is now clear which lines should be compared to which points. The palette option certainly makes for prettier splots and can be really useful for improving their readability. The range of colours can be tuned in each case using the <span style="font-family: "Courier New",Courier,monospace;"><b>set</b> palette model</span> command to match the spread of your data to make things as clear as possible.</div>
</div>
Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-2408847062781475509.post-6002222403847293412013-04-30T10:44:00.003-07:002013-09-01T02:46:20.685-07:00Bar Charts<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh5BlhocPFe8MPuiuzpDEN8Aaw2pfRDsYSsxMe-Yt3KgZLJR9-Z3hdizxQ67EMpSpWxeUKkIKtFevl238vWsP3xlOHu1cBv0DCqZ1dy-3q2VbBIAq6wOHRKnMd0VZP2v_qAjyGHCQ5da3OG/s1600/cites.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh5BlhocPFe8MPuiuzpDEN8Aaw2pfRDsYSsxMe-Yt3KgZLJR9-Z3hdizxQ67EMpSpWxeUKkIKtFevl238vWsP3xlOHu1cBv0DCqZ1dy-3q2VbBIAq6wOHRKnMd0VZP2v_qAjyGHCQ5da3OG/s320/cites.png" width="320" /></a></div>
I wanted to make a bar chart for a recent <a href="http://blogoftheisotopes.blogspot.co.uk/2013/04/the-ref-is-ranker.html">post on my real blog</a> (on the difference between different citation databases for my recent papers). <br />
<br />
I needed to look up enough of the details of how to do it in gnuplot, that I thought I'd document it here for my own use, and in the hope that others may get some help from it. I pass my thanks to the <i><a href="http://www.gnuplotting.org/">gnuplotting blog</a></i> for some useful tips to help me get started, and the nice idea that having less prominent grids and tick labels makes things easier to read.<br />
<br />
The figure is above, and the necessary code follows, with commentary below:<br />
<blockquote class="ecs_bq_code">
<b>set</b> term 'pngcairo' font 'Helvetica,12'<br />
<b>set</b> out 'cites.png'<br />
<br />
<b>set</b> style line 1 lc rgb '#440000' lt 1<br />
<b>set</b> style line 2 lc rgb '#882200' lt 1<br />
<b>set</b> style line 3 lc rgb '#bb6622' lt 1<br />
<b>set</b> style line 4 lc rgb '#ffcc66' lt 1<br />
<br />
<b>set</b> style line 11 lc rgb '#808080' lt 1<br />
<b>set</b> border 3 back ls 11<br />
<b>set</b> tics nomirror<br />
<b>set</b> xtics 1,1.0,25.0 offset -1.5,0.0<br />
<b>set</b> style line 12 lc rgb '#808080' lt 0 lw 1<br />
<b>set</b> grid back ls 12<br />
<br />
<b>set</b> style fill solid 1.0 border rgb 'grey30'<br />
<br />
<b>set</b> ylabel 'citations'<br />
<b>set</b> xlabel 'Paper Index'<br />
<b>set</b> xrange [0:25]<br />
<br />
bw=0.15<br />
<b>plot</b> 'cite-data' u ($0+0.5-1.5*bw):1:(bw) w boxes ls 1 t 'Google Scholar', \<br />
'cite-data' u ($0+0.5-0.5*bw):2:(bw) w boxes ls 2 t 'Scopus', \<br />
'cite-data' u ($0+0.5+0.5*bw):3:(bw) w boxes ls 3 t 'ISI', \<br />
'cite-data' u ($0+0.5+1.5*bw):4:(bw) w boxes ls 4 t 'Journal site'</blockquote>
I used the pngcairo terminal, in the belief that it is better than the png terminal, but I should really investigate that.<br />
<br />
The block of "set style line" commands is used just to set the colour of the filling of the bars in the bar chart. If you follow the link to the blog post from my other blog that I mentioned above, you'll see that I didn't have those colour-changing lines, and used the default colours instead. I'm not sure which I prefer, but this at least shows how you can change them.<br />
<br />
The next block sets up the border, tick marks and labels and the grid. Line styles 11 and 12 are used to set the border and grid to be grey rather than black, to accentuate the actual data. I used the set tics command to define user tick marks. I set up the data file to have no abscissa column, so it will be plotting it just by the ordinal number, and a little playing with the offset enabled me to get the numbers in the right place.<br />
<br />
The <tt>set style fill</tt> line is needed to fill in the bars. If you don't set this, you'll just have box outlines, so if that's what you want, don't have this line. You can specify the border to be a different colour from the fill, as indicated.<br />
<br />
The last section sets up the bars. I have four bars to show per ordinate. I decided to make each one take up a fraction of 0.15 the width of the space available for each data point. The <tt>bw=0.15</tt> sets up a variable to store this. In the plot command, the <tt>$0</tt> specifies the ordinate for each point, since I don't provide x-value points in the cite-data file. The calculations such as <tt>$0+0.5-1.5*bw</tt> specify the centre of the point at which the bar appears, and the (bw) the width. <br />
<br />
If you want to play with this, <a href="http://personal.ph.surrey.ac.uk/~phs3ps/cite-data">here is the file cite-data</a> referred to in the gnuplot script.<br />
<br /></div>
Blog of the Isotopeshttp://www.blogger.com/profile/09709830920524368020noreply@blogger.com1tag:blogger.com,1999:blog-2408847062781475509.post-55577023455047459552013-01-01T06:43:00.001-08:002013-01-01T06:43:59.710-08:00Fourier series, conditions and recursive functions<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_nrevoJABVGIETCtAlN2ByiDBUCdo6gsLpo3iMfScXVWDA370QiOGE4rXPtw-t3takUY0LOYcsJEOsRwO1LjwfgWpq5mtrPpJgqDWFTRAsU7iDmpf4WI7N4fJpcazgtqyJ8_ar9XKK8Y6/s1600/fourier.png" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" height="273" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh_nrevoJABVGIETCtAlN2ByiDBUCdo6gsLpo3iMfScXVWDA370QiOGE4rXPtw-t3takUY0LOYcsJEOsRwO1LjwfgWpq5mtrPpJgqDWFTRAsU7iDmpf4WI7N4fJpcazgtqyJ8_ar9XKK8Y6/s320/fourier.png" width="320" /></a></div>
<div dir="ltr" style="text-align: left;" trbidi="on">
This script uses recursive functions to plot a square pulse function and its <a href="http://en.wikipedia.org/wiki/Fourier_series">Fourier series</a> expansion, truncated to some maximum N<sub>max</sub>. The terms of the series itself must be found analytically, but once found this script allows one to visualize the convergence of the series. The resulting plot is shown on the right.<br />
<br />
The result of the Fourier series is found by summing over the terms of the series, each of which have a simple analytic dependence on the summation variable. To do this, the <tt>gnuplot</tt> script makes use of the <a href="http://lavica.fesb.hr/cgi-bin/info2html?%28gnuplot%29Ternary">Ternary operator <tt>?</tt></a>, which can be used for conditional assignment. The syntax for this can be described as: <br />
<blockquote class="ecs_bq_math">
y(x) = (cond) ? [true] : [false]</blockquote>
The function <tt>y(x)</tt> takes the value [true] if condition (cond) is true, and [false] if condition (cond) is false. The Ternary operator itself is <tt>?</tt> and a colon <tt>:</tt> is used to separate the possible results [true] and [false]. Both [true] and [false] may be further conditional statements, meaning the statements can be nested, e.g.,<br />
<blockquote class="ecs_bq_math">
y(x) = (cond1) ? (cond2) ? [true2] : [false2] : [false1] </blockquote>
If (cond1) is true, then the second condition (cond2) is tested. If both are true, <tt>y(x)</tt> takes the value [true2]. If (cond1) is true but (cond2) is false, <tt>y(x)</tt> has the value [false2]. Finally if (cond1) is false <tt>y(x)</tt> takes the value [false1]. <br />
<br />
The step function for the present example is defined as:<br />
<blockquote class="ecs_bq_math">
f(x) = -2 (-π≤x<0)<br />
= 1 (0≤x<+π)
</blockquote>
and is periodic with period 2π. The terms of the Fourier series can be evaluated analytically. The <tt>gnuplot</tt> code to produce the plot is:<br />
<blockquote class="ecs_bq_code">
<b>set</b> terminal png enhanced size 800,600 font "sans, 18"<br />
<b>set</b> output "fourier.png"<br />
<br />
<b>set</b> tics nomirror<br />
<b>set</b> border 2<br />
<b>set</b> xzeroaxis lt -1 lw 2<br />
<b>set</b> xtics axis<br />
<br />
pi = 3.14159265359<br />
<br />
# f(x) is the step function that is being expanded<br />
f(x) = (x<-pi) ? f(x+2*pi) : (x>pi) ? f(x-2*pi) : (x<0) ? -2 : 1<br />
<br />
# g(n,x) is the nth term in the Fourier series<br />
g(n,x) = (n==0) ? -0.5 : (1-(-1)**n)*3*sin(n*x)/(n*pi)<br />
<br />
# s(n,x) is the Fourier series up to the nth term<br />
s(n,x) = (n>=0) ? g(n,x) + s(n-1,x) : 0<br />
<br />
<b>set</b> yrange[-3.5:2.5]<br />
<b>set</b> xlabel "x"<br />
<b>set</b> ylabel "f(x)"<br />
<br />
<b>set</b> samples 400<br />
<br />
# set the maximum term in the series<br />
nmax1=3<br />
nmax2=10<br />
<br />
<b>plot</b>
f(x) title "Step pulse",\<br />
s(nmax1,x) title "Fourier Series, N_{max}=".nmax1,\<br />
s(nmax2,x) title "Fourier Series, N_{max}=".nmax2
</blockquote>
The first two lines set the png terminal options and the output file name. <tt>f(x)</tt> is the step function itself. The first two conditions <tt>(x<-pi)</tt> and <tt>(x>pi)</tt> move the value of <tt>x</tt> into the range -π to +π, and the last condition <tt>(x<0)</tt> gives the function the value -2 or 1. <tt>g(n,x)</tt> is the nth term in the Fourier series, determined analytically, and <tt>s(n,x)</tt> gives the full Fourier series up to a maximum value of <tt>n</tt>, <tt>nmax. </tt>The condition in <tt>s(n,x)</tt> makes sure the recursion stops when <tt>n=0</tt>. The script will plot the exact step function and the Fourier series truncated to maximum order <tt>nmax1</tt> and <tt>nmax2</tt>. By varying <tt>nmax</tt>, the convergence of the series can be seen.<br />
<br />
There are a couple other neat tricks in there too. The second group of <span style="font-family: "Courier New",Courier,monospace;"><b>set</b></span> commands remove the normal borders and place the x-axis at <tt>y=0</tt>, and in the last line the string concatenation operator "." is used to append the gnuplot variable <tt>nmax</tt> onto the title for the Fourier series.</div>
</div>
Unknownnoreply@blogger.com2tag:blogger.com,1999:blog-2408847062781475509.post-42123011151362165642012-12-05T10:29:00.000-08:002018-11-14T02:24:58.119-08:00Hello, fitting and piecewise functions<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="tr_bq">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhv8y0vQ8JbN007EPLyi2TooASsEcyUVH3GUTe9D1KaUgvT63XZBuh_csJM7lyEgeiHBqjJKAiPaFjB6YYmzTtakJD54zgTWSZxjuH_Ix40TiN-MPiLarKp226xYCDnTBAVe6vgd3856HTt/s1600/lead-exp.png" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhv8y0vQ8JbN007EPLyi2TooASsEcyUVH3GUTe9D1KaUgvT63XZBuh_csJM7lyEgeiHBqjJKAiPaFjB6YYmzTtakJD54zgTWSZxjuH_Ix40TiN-MPiLarKp226xYCDnTBAVe6vgd3856HTt/s1600/lead-exp.png" /></a>Welcome to the Surrey Physics Plotters blog. It exists in the spirit of other blogs dedicated to the use of the <a href="http://www.gnuplot.info/"><tt>gnuplot</tt></a> plotting program. While I don't consider myself as much of an expert at gnuplot as the writers of <a href="http://www.gnuplotting.org/">gnuplotting</a> or <a href="http://gnuplot-tricks.blogspot.co.uk/">gnuplot tricks</a>, I think it would still be helpful to have another repository for useful examples that I, at least, will otherwise forget.</div>
<br />
So - the first example comes in the form of a plot which includes a set
of data defined at equally spaced points in the abscissa, whose
ordinates show a sudden kink in the gradient (the data are the radii of a
series of isotopes of lead nuclei - if you're interested, the paper is <a href="http://arxiv.org/abs/1210.2656">here</a>).<br />
<br />
I wanted to include on the plot a fit to the data, assuming one straight
line up to atomic number 208, and a different straight line from 208
up. I'll show the code here, and explain how it works below.<br />
<blockquote class="ecs_bq_code">
<b> set</b> term postscript enhanced col size 10cm,8cm font 'Helvetica,12'<br />
<b>set</b> out 'lead-exp.eps'<br />
<br />
f1(x) = m1*(x-208)<br />
f2(x) = m2*(x-208)<br />
<br />
<b>fit</b> [202:208] f1(x) 'expdat.gnu' using 1:($2-5.5010):3 via m1<br />
<b>fit</b> [208:214] f2(x) 'expdat.gnu' using 1:($2-5.5010):3 via m2<br />
<br />
f(x) = abs(x-205)<3 ? f1(x) : abs(x-211)<3 ? f2(x) : 1/0<br />
<br />
<b>set</b> xrange [201:215]<br />
<b>set</b> yrange [-0.05:0.08]<br />
<b>set</b> ylabel '{/Symbol d}<r_{ch}^2>^{1/2} [fm]'<br />
<b>set</b> xlabel 'A'<br />
<b>plot</b> 'expdat.gnu' u 1:($2-5.5010):3 w yerr ls 1 notit, f(x) lt 2 notit<br />
<b>quit</b><br />
<br />
<i> # Data from I. Angeli, At. Data Nucl. Data Tables, 87, 185 (2004)<br /> 202 5.4690 .0055 .0007<br /> 204 5.4794 .0008 .0005<br /> 206 5.4897 .0007 .0003<br /> 208 5.5010 .0009<br /> 210 5.5230 .0035 .0005<br /> 212 5.5450 .0075 .0010<br /> 214 5.5650 .0105 .0014</i></blockquote>
The first two lines set up the output file as a postscript file (since
this is what I wanted for the publication). Specifying the size here
adds in the BoundingBox for encapsulated postscript, though it probably
makes sense to use the eps option instead.<br />
<br />
The two separate fitting functions are defined as <tt>f1(x)</tt> and <tt>f2(x)</tt>. They have undetermined parameters <tt>m1</tt> and <tt>m2</tt> and are designed to be zero at <tt>x=208</tt>.<br />
<br />
I then fit the two separate lines in specified ranges, using the data
that comes at the end of the plot file - I have assumed that the above
file is saved as <tt>expdat.gnu</tt>.<br />
<br />
The line defining <tt>f(x)</tt> makes a piecewise function, which is <tt>f1(x)</tt> if <tt>x</tt> is in the range <tt>202<x<208</tt>, is <tt>f2(x)</tt> if it is in the range <tt>208<x<214</tt> and undefined otherwise - the "<tt>1/0</tt>" serves to make the function undefined.<br />
<br />
Everything else is a bit more straightforward, I think. The range in x and y are set, as are the labels, though the <tt>enhanced</tt> option in the <tt>set term</tt> line means that the <tt>_</tt> and <tt>^</tt>
symbols are treated as subscript and superscript indicators as in LaTeX
in the label text. The results (turned into a png file) is shown at
the top.</div>
Blog of the Isotopeshttp://www.blogger.com/profile/09709830920524368020noreply@blogger.com0