tag:blogger.com,1999:blog-86842078738166231642024-03-17T20:03:59.607-07:00Gnuplot surprisingI always tell myself: "Use your head, then you may find Gnuplot can plot graphics surprising you!"数声风笛离亭晚,我想潇湘君想秦!http://www.blogger.com/profile/08963583502168321534noreply@blogger.comBlogger45125tag:blogger.com,1999:blog-8684207873816623164.post-30611544105199903152012-12-29T23:49:00.000-08:002012-12-29T23:54:10.295-08:00Pie charting with gnuplotIn two of my previous posts (<a href="http://gnuplot-surprising.blogspot.com/2011/10/pie-chart-in-gnuplot.html">post-1</a> <a href="http://gnuplot-surprising.blogspot.com/2012/11/plot-pie-chart-using-gnuplot.html">post-2</a>), I have talked about how to plot a pie chart using gnuplot. This time I will introduce a new method witch is much more simpler.<br />
<br />
This new method will use the plotstyle "circle". And this feather is not provide since gnuplot version 4.6. So you have to update your gnuplot to a version not lower than 4.6.<br />
<br />
<div class="codes">
<pre>set term post eps color enhanced size 5cm,5cm
set output "pie_chart.eps"
set size square
set xrange [-1:1]
set yrange [-1:1]
set style fill solid 1
unset border
unset tics
unset key
plot '-' with circle linecolor var
0 0 1 0 30 1
0 0 1 30 60 2
0 0 1 60 120 3
0 0 1 120 190 4
0 0 1 190 275 5
0 0 1 275 360 6
e
##the first two numbers 0 0 are the
# x and y coordinate of center
##the third number 1 is the radius
#of the circle
##the fourth and fifth number are the
#begin and end angle of the sector
##the last number is the color indicate
set output
</pre>
</div>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixqiUXVtbsYwEjiAmikKYLoM7cWuLw0LGPU7oE-S4z9J5NG-APM0bcmSfGF7eOnWnrk2f7OhsejozUlGesXfgvHZklilKYD7_eWX0C12McyG7TkGPXfeT0D8m5MMjDfrI23Lfw91w7-boK/s1600/pie_chart.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixqiUXVtbsYwEjiAmikKYLoM7cWuLw0LGPU7oE-S4z9J5NG-APM0bcmSfGF7eOnWnrk2f7OhsejozUlGesXfgvHZklilKYD7_eWX0C12McyG7TkGPXfeT0D8m5MMjDfrI23Lfw91w7-boK/s1600/pie_chart.png" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Fig.1 Pie charting using gnuplot</td></tr>
</tbody></table>
<br />数声风笛离亭晚,我想潇湘君想秦!http://www.blogger.com/profile/08963583502168321534noreply@blogger.com13tag:blogger.com,1999:blog-8684207873816623164.post-8468984039041840262012-11-30T03:47:00.000-08:002013-03-26T19:49:35.362-07:00Plot a pie chart using gnuplotIf your gnuplot version is not lower than 4.4, then plotting a pie chart can be done with command "set object circle...". The following is an example.<br />
<br />
<div class="codes">
<pre>reset
set term postscript eps color enhanced size 5cm,5cm
set output "pie_chart.eps"
set size square
set style fill solid 1.0 border -1
set object 1 circle at screen 0.5,0.5 size \
screen 0.45 arc [0 :50 ] fillcolor rgb "red" front
set object 2 circle at screen 0.5,0.5 size \
screen 0.45 arc [50 :150] fillcolor rgb "orange" front
set object 3 circle at screen 0.5,0.5 size \
screen 0.45 arc [150:220] fillcolor rgb "forest-green" front
set object 4 circle at screen 0.5,0.5 size \
screen 0.45 arc [220:360] fillcolor rgb "dark-magenta" front
#plot a white line, i.e., plot nothing
unset border
unset tics
unset key
plot x with lines lc rgb "#ffffff"
set output
</pre>
</div>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxcCWnB0ECBlffykcu_mhCN1yGrYX43ldwseJqNlR9aR7hPFUpm7Hqx3fds3hvDI3l2i8bL8TG_1EqCJ0cl0JltrMjUacHf_2yNqIBVuIFt_x1h4ymvR3aM6O44vGEpCctJVy8G8Ga-_s7/s1600/pie_chart.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxcCWnB0ECBlffykcu_mhCN1yGrYX43ldwseJqNlR9aR7hPFUpm7Hqx3fds3hvDI3l2i8bL8TG_1EqCJ0cl0JltrMjUacHf_2yNqIBVuIFt_x1h4ymvR3aM6O44vGEpCctJVy8G8Ga-_s7/s1600/pie_chart.jpg" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Fig.1 Plot a pie chart using gnuplot</td></tr>
</tbody></table>
<br />
If you think setting of the position, size and arc manually is very tedious, you may write a script with any programming language.<br />
<br />
<h3>
Follow <a href="http://gnuplot-surprising.blogspot.com/2012/12/pie-charting-with-gnuplot.html" target="_blank">this link</a> to see a new simpler method to plot a pie chart. </h3>
<br />数声风笛离亭晚,我想潇湘君想秦!http://www.blogger.com/profile/08963583502168321534noreply@blogger.com64tag:blogger.com,1999:blog-8684207873816623164.post-13475240561900997912012-10-31T05:48:00.000-07:002012-10-31T05:50:51.713-07:00Convertting a rgb color value to its hex-stringIn a <a href="http://gnuplot-surprising.blogspot.com/2012/08/converting-from-hex-string-color-to-its.html" target="_blank">previous post</a>, I have talked about how to convert a hex-string color to its rgbvalue. This time I will talk about the inversion, i.e., converting a rgb value color specification to its hex-string correspondence.<br />
<br />
<div class="codes">
<pre>#pick the red color value out
red(rgbvalue)=rgbvalue/65536
#pick the green color value out
green(rgbvalue)=(rgbvalue%65536)/256
#pick the blue color value out
blue(rgbvalue)=rgbvalue%256
#convert dec color value to its hex string
dec2hex(dec)=gprintf("%02x",dec)
#convert rgb color value to its hex string
rgb2hex(color)="#".\
dec2hex(red(color)).\
dec2hex(green(color)).\
dec2hex(blue(color))
color = 65535 #rgb-value
print "The hex string of rgb-color ". color ." is: "
print rgb2hex(color)
plot cos(x) w line linecolor rgb rgb2hex(color)
</pre>
</div>
<br />
Run this script, it will output the following string and picture.<br />
<br />
<div class="codes">
<pre>The hex string of rgb color 65535 is:
#00ffff
</pre>
</div>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEim2jlW4TiJqGxIQQ4Za-_QuTxjn5QpSYFxke7rlQaKDoZUH6SfcplsexD-F5JECGSYTquLHpJ99QhaLrPSpO9Tato-eKGJ4yHOJCH7g-45LMAnmlcLMocLzC8iNTphfNOmsECY_tyCp0VS/s1600/rgbvalue_to_hexstring.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEim2jlW4TiJqGxIQQ4Za-_QuTxjn5QpSYFxke7rlQaKDoZUH6SfcplsexD-F5JECGSYTquLHpJ99QhaLrPSpO9Tato-eKGJ4yHOJCH7g-45LMAnmlcLMocLzC8iNTphfNOmsECY_tyCp0VS/s1600/rgbvalue_to_hexstring.png" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Fig.1 Converting from a rgb color specification to its hex-string</td></tr>
</tbody></table>
<br />
<br />数声风笛离亭晚,我想潇湘君想秦!http://www.blogger.com/profile/08963583502168321534noreply@blogger.com3tag:blogger.com,1999:blog-8684207873816623164.post-14039259668109382152012-09-29T21:01:00.000-07:002012-09-29T21:01:27.486-07:00Self-defined linestyle under gnuplot postscript terminal In post <a href="http://gnuplot-surprising.blogspot.com/2012/07/self-defined-linestyle-under-gnuplot.html">http://gnuplot-surprising.blogspot.com/2012/07/self-defined-linestyle-under-gnuplot.html</a> we have talked about self-defined linestyle under gnuplot svg terminal. Here we show how to realize the same effect under postscript terminal.<br />
<br />
Assume you have plotted a graph using the following script.<br />
<br />
<div class="codes">
<pre>set term post eps color enhanced
set output "self-defined_linestyle.eps"
plot sin(x) w l linestyle 1
set output
</pre>
</div>
<br />
Open the outputed ps (or eps) file with any text editor, and find codes as the following:<br />
<br />
<div class="codes">
<pre>% Default Line colors
/LCw {1 1 1} def
/LCb {0 0 0} def
/LCa {0 0 0} def
/LC0 {1 0 0} def
/LC1 {0 1 0} def
/LC2 {0 0 1} def
/LC3 {1 0 1} def
/LC4 {0 1 1} def
/LC5 {1 1 0} def
/LC6 {0 0 0} def
/LC7 {1 0.3 0} def
/LC8 {0.5 0.5 0.5} def
% Default Line Types
/LTw {PL [] 1 setgray} def
/LTb {BL [] LCb DL} def
/LTa {AL [1 udl mul 2 udl mul] 0 setdash LCa setrgbcolor} def
/LT0 {PL [8 dl1 1 dl2 1 dl1 1 dl2] LC0 DL} def
/LT1 {PL [4 dl1 2 dl2] LC1 DL} def
/LT2 {PL [2 dl1 3 dl2] LC2 DL} def
/LT3 {PL [1 dl1 1.5 dl2] LC3 DL} def
/LT4 {PL [6 dl1 2 dl2 1 dl1 2 dl2] LC4 DL} def
/LT5 {PL [3 dl1 3 dl2 1 dl1 3 dl2] LC5 DL} def
/LT6 {PL [2 dl1 2 dl2 2 dl1 6 dl2] LC6 DL} def
/LT7 {PL [1 dl1 2 dl2 6 dl1 2 dl2 1 dl1 2 dl2] LC7 DL} def
/LT8 {PL [2 dl1 2 dl2 2 dl1 2 dl2 2 dl1 2 dl2 2 dl1 4 dl2] LC8 DL} def
</pre>
</div>
<br />
In the upper codes "/LC? {# # #} def" defines the line color, and "/LT? {Pl [#1 dl1 #2 dl2]LC?} def" defines the line type. "#1 dl1 #2 dl2" means the line is a dash line with #1 legth colored and #2 length blank.<br />
<br />
Modify these codes as what you like, and save the file. For example, I use "/LT0 {PL [8 dl1 4 dl2 1 dl1 4 dl2 1 dl1 8 dl2] LC0 DL} def" to repalce the original "/LT0 {PL [] LC0 DL} def".<br />
<br />
At last, open the modified postscript file with gsview (or any other postscript viewer), and you will find the linestyle have been changed. The following is the original and modifed graph respectively.<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiAijhXJzfplEJ5j1_HQl_M_i3IKx65JOcF82H63KSZLKHbw6RFniQKnfAoatCi2BfL2HwH9GHygtYAjA1tmiG2saSEM-q2LeluuY-fAYstQSX-e9qEAHLmskGt9VazUD1laxYoG1OTJoRF/s1600/self-defined_linstyle_original.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="282" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiAijhXJzfplEJ5j1_HQl_M_i3IKx65JOcF82H63KSZLKHbw6RFniQKnfAoatCi2BfL2HwH9GHygtYAjA1tmiG2saSEM-q2LeluuY-fAYstQSX-e9qEAHLmskGt9VazUD1laxYoG1OTJoRF/s1600/self-defined_linstyle_original.jpg" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Fig.1 Original outputted picture </td></tr>
</tbody></table>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiYdFFvF4kIIR4UuuowPQSxZMG9F_8HBEmZ-vrUL9PLgE4Oc1yHw1Ab4_mcrzBmVsmoBKtnMZt4zwqXmQpCfTZP4aCC9_sU7LAhXQTW6778XCnB8UrGrarBX5d6XGf2QToJzTDaRs1UNHv1/s1600/self-defined_linstyle_modidied.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="277" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiYdFFvF4kIIR4UuuowPQSxZMG9F_8HBEmZ-vrUL9PLgE4Oc1yHw1Ab4_mcrzBmVsmoBKtnMZt4zwqXmQpCfTZP4aCC9_sU7LAhXQTW6778XCnB8UrGrarBX5d6XGf2QToJzTDaRs1UNHv1/s1600/self-defined_linstyle_modidied.jpg" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Fig.2 Modified outputted picture</td></tr>
</tbody></table>
<br />
<br />数声风笛离亭晚,我想潇湘君想秦!http://www.blogger.com/profile/08963583502168321534noreply@blogger.com1tag:blogger.com,1999:blog-8684207873816623164.post-18407069747315362852012-08-24T05:11:00.002-07:002012-11-08T00:22:38.725-08:00Converting from a hex-string color to its rgbvalueThere are several ways to specify color when one plots using gnuplot.Among them hex-string (like "#0000ff") and rgbvalue (like "256") specification is very important. In this post we will talk about converting from a hex-string color to its rgbvalue (<a href="http://gnuplot-surprising.blogspot.com/2012/10/convertting-rgb-color-value-to-its-hex.html" target="_blank">the inversion will be talked in another post</a>).<br />
<br />
<div class="codes">
<pre>reset
#pick the red color hex string out
red(colorstring)= colorstring[2:3]
#pick the green color hex string out
green(colorstring)=colorstring[4:5]
#pick the blue color hex string out
blue(colorstring)=colorstring[6:7]
#convert a hex string to its dec format
hex2dec(hex)=gprintf("%0.f",int('0X'.hex))
#calculate the rgb value from the r,g,b weight
rgb(r,g,b) = 65536*int(r)+256*int(g)+int(b)
#convert the hex color string to its rgb value.
hex2rgbvalue(color)=rgb(\
hex2dec(red(color)),\
hex2dec(green(color)),\
hex2dec(blue(color))\
)
set term png
set output "hex_string2rgbvalue.png"
plot 'color.dat' u 1:2:(hex2rgbvalue(stringcolumn(3)))\
w lp pt 7 ps 3 lc rgb variable notitle
set output
</pre>
</div>
<br />
File 'color.dat' has content like the following:<br />
<div class="codes">
<pre>0 1 #ff0000
1 2 #00ff00
2 3 #0000ff
3 4 #ff00ff
4 5 #ffff00
5 6 #00ffff
0 6 #00ffff
1 5 #ffff00
2 4 #ff00ff
3 3 #0000ff
4 2 #00ff00
5 1 #ff0000
</pre>
</div>
<br />
The output file 'hex_string2rgbvalue.png' looks like this:<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijGWxd98N0UiXEjY6sniW8o4J5pV170xwPm3PbYM-mE9ko9yvDdQ42DaBMnKyTxVmdimYzZE-uB1uKUX8AbcTQrr_MwZJ8fJMGSP0n-APnPz0XYIPLfIpcEGQ-BZY96mMOpxtys5H0p8MX/s1600/hex_string2rgbvalue.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijGWxd98N0UiXEjY6sniW8o4J5pV170xwPm3PbYM-mE9ko9yvDdQ42DaBMnKyTxVmdimYzZE-uB1uKUX8AbcTQrr_MwZJ8fJMGSP0n-APnPz0XYIPLfIpcEGQ-BZY96mMOpxtys5H0p8MX/s1600/hex_string2rgbvalue.png" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Fig.1 Converting from a hex-string color to its rgb-value</td></tr>
</tbody></table>
数声风笛离亭晚,我想潇湘君想秦!http://www.blogger.com/profile/08963583502168321534noreply@blogger.com8tag:blogger.com,1999:blog-8684207873816623164.post-52299661642451200592012-07-07T23:49:00.000-07:002012-11-08T00:20:19.942-08:00Self-defined linestyle under gnuplot svg terminalGnuplot have defined a series of line styles. But maybe you still have not find the one which is just appropriate for you. For example,when you want to use a dash-dot-dot line, you find there is not such a linestyle.<br />
<br />
When you are using svg teminal (<a href="http://gnuplot-surprising.blogspot.com/2012/09/self-defined-linestyle-under-gnuplot.html" target="_blank">self-defined linestyle under postscript terminal will be talked in another post</a>) this problem can solved easily. Fisrt, plot your file with solid line. <br />
<br />
<div class="codes">
<pre>set term svg
set output "sin.svg"
plot sin(x) with line
set output
</pre>
</div>
<br />
After that, open the svg file with a text-editor, and find codes like the following:<br />
<br />
<div class="codes">
<pre><g id="gnuplot_plot_1" >
<title>gnuplot_plot_1</title>
<g style=" fill:none; color:red;
stroke:currentColor; stroke-width:1.00;
stroke-linecap:butt; stroke-linejoin:miter">
</pre>
</div>
<br />
Then change it to<br />
<br />
<div class="codes">
<pre><g id="gnuplot_plot_1" >
<title>gnuplot_plot_1</title>
<g style="stroke-dasharray: 7,2,2,2,2,2;
fill:none; color:red; stroke:currentColor;
stroke-width:1.00; stroke-linecap:butt;
stroke-linejoin:miter">
</pre>
</div>
<br />
This time open your svg file, and you find the line sytle has changed. The follwing picture is a screen capture of "sin.svg".<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQuzC3enUNiFnA78XpWN_YWGmvTCAib9m61bdMXqnCBhtoRxQ6Wljshk5JsU0lqkog8Ukr_pq4ZyN_-mPJ56nCZfY0R_wshlIt8KiHfPo7z7uwWnWrHLkPJUy4bETf9Mv_prBD3XXUHE2u/s1600/sin_screen_capture.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="260" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQuzC3enUNiFnA78XpWN_YWGmvTCAib9m61bdMXqnCBhtoRxQ6Wljshk5JsU0lqkog8Ukr_pq4ZyN_-mPJ56nCZfY0R_wshlIt8KiHfPo7z7uwWnWrHLkPJUy4bETf9Mv_prBD3XXUHE2u/s320/sin_screen_capture.jpg" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Fig.1 Self-defined linestyle under gnuplot svg terminal</td></tr>
</tbody></table>
<br />
To further understand this post, you may google "stroke-dasharray + svg".数声风笛离亭晚,我想潇湘君想秦!http://www.blogger.com/profile/08963583502168321534noreply@blogger.com1tag:blogger.com,1999:blog-8684207873816623164.post-48960639453685001292012-06-04T05:11:00.000-07:002012-06-27T02:11:34.724-07:00Plot an interactive svg graph using gnuplotIn this post an example of plotting an interactive svg graph using gnuplot is talked.<br />
<br />
First, run the following gnuplot plotting script.<br />
<br />
<div class="codes"><pre>set term svg mouse jsdir "./js"
set output "sincos.svg"
plot sin(x) w l lc rgb"#ff0000", cos(x) w l lc rgb"#00ff00"
set output
</pre></div><br />
And a file called "sincos.svg" is plotted. As we specified in the script that the javascript file is in directory "./js", "gnuplot-dir/share/js" have to be copied to the current directory (windows operating system, and if you use linux maybe the directory should be /usr/local/share/gnuplot/<i>gnuplot-version</i>/js). This time open the file, and you can find when you click the graph, the coordinate of the current point will be displayed. And when you click the legend, the cooresponding line will be invisible, and click it again, the line will visible.<br />
<br />
The jsdir can also be a url location. And this is is usually appropriate if you are embedding the svg into a web page. For example, command --- set term svg mouse jsdir "http://gnuplot.sourceforge.net/demo_svg_4.6/" can be used.<br />
<br />
<div class="codes"><pre>set term svg mouse jsdir "http://gnuplot.sourceforge.net/demo_svg_4.6/"
set output "sincos.svg"
plot sin(x) w l lc rgb"#ff0000", cos(x) w l lc rgb"#00ff00"
set output
</pre></div>数声风笛离亭晚,我想潇湘君想秦!http://www.blogger.com/profile/08963583502168321534noreply@blogger.com11tag:blogger.com,1999:blog-8684207873816623164.post-3759139008273369202012-05-19T02:47:00.000-07:002012-05-26T22:25:53.511-07:00How to pick out the maximum and minimum points when plotting with gnuplot?Picking out the maximum and minimum points from a data file may be useful when you want to make some explanation to your graph. It is known that GPVAL_Y_MAX (GPVAL_Y_MIN) is the maximum (minimum) value. But what is the corresponding X coordinate? <br />
<br />
With Gnuplot 4.6 the both the x and y coordinate of maximum and minimum points can be find out easily. The method is using new command "stats". This command is used for statistic. When it is run, some statistical results will be gotten. If your data file contains two column of data, (STATS_pos_max_y, STATS_max_y) will be the coordinate of the maximum point and (STATS_pos_min_y, STATS_min_y) will be the coordinate of the minimum point.<br />
<br />
For example, we have a data file named "data.dat" like this<br />
<br />
<div class="codes">
<pre>0.1 0.289010715
0.2 0.050630492
0.3 0.721247895
0.4 0.284271151
0.5 0.505051253
0.6 0.101819025
0.7 0.008466133
0.8 0.36249047
0.9 0.487576233
1.0 0.595090343
1.1 0.865255938
1.2 0.696628854
1.3 0.505899456
1.4 0.338131983
1.5 0.108034045
</pre>
</div>
<br />
Run a gnuplot script as follows<br />
<br />
<div class="codes">
<pre>reset
set term png
set output "max_min.png"
stats "data.dat" u 1:2 nooutput
set xrange [STATS_min_x:STATS_max_x]
set label 1 "Maximun" at STATS_pos_max_y, STATS_max_y offset 1,-0.5
set label 2 "Minimun" at STATS_pos_min_y, STATS_min_y offset 1,0.5
plot "data.dat" w p pt 3 lc rgb"#ff0000" notitle, \
STATS_min_y w l lc rgb"#00ffff" notitle, \
STATS_max_y w l lc rgb"#00ffff" notitle
set output
</pre>
</div>
<br />
And you will get a graph like the following one.<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTeHETgSpDT74V403qkuKwLxqCyoKEbvFgo806zCpdI7GVaawII-Uiifx1f_tXbMA-GCI_JpFSPYds7V8rChkzO79w8f0Wxhjh3RZ7nU72ot5-21bnF_EK04eM_Q2VtUNMmVQy7yYA8XkZ/s1600/max_min.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiTeHETgSpDT74V403qkuKwLxqCyoKEbvFgo806zCpdI7GVaawII-Uiifx1f_tXbMA-GCI_JpFSPYds7V8rChkzO79w8f0Wxhjh3RZ7nU72ot5-21bnF_EK04eM_Q2VtUNMmVQy7yYA8XkZ/s320/max_min.png" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Picking out the maximum and minimum points when plotting using gnuplot</td></tr>
</tbody></table>数声风笛离亭晚,我想潇湘君想秦!http://www.blogger.com/profile/08963583502168321534noreply@blogger.com12tag:blogger.com,1999:blog-8684207873816623164.post-60903538809032129532012-04-02T04:52:00.000-07:002012-04-03T00:09:54.939-07:00New version of gnuplot makes iterations easy --- animation as an exampleGnuplot4.6 have been released on 2012.03.08. And some new features have been added. Among them, I think block-structured if/else/while/do is the most useful one. With this new feature, one can easily deal with loop. In this post I will talk about this new feature taking animation as an example. In a <a href="http://gnuplot-surprising.blogspot.com/2011/09/creating-gif-animation-using-gnuplot.html" target="_blank">previous post</a>, how to create a gif animation using gnuplot have been talked. And it seems a little complex. An extra file is needed. Here with this new feature in gnuplot4.6, the work can be done simply as follows:<br />
<br />
<div class="codes">
<pre>reset
set term gif animate
set output "animate.gif"
n=24 #n frames
dt=2*pi/n
set xrange [0:4*pi]
set yrange [-1:1]
do for [i=0:n]{
plot sin(x+i*dt)/(1. + i/12.) w l lw 1.5 title sprintf("t=%i",i)
}
set output
</pre>
</div>
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTjQxtV7j6uVm08m5G3F60slKISbdQLpxAOBiua5FOAyGRLgSU5rWoWH3Hz99lbD_iYATF_lOkF8WpYaxZy2ZaOlr24BH-oco9gzsj4guKcuI_aiUn707bODu1qY1DA_8cQoUWdaaj4Gol/s1600/animate.gif" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhTjQxtV7j6uVm08m5G3F60slKISbdQLpxAOBiua5FOAyGRLgSU5rWoWH3Hz99lbD_iYATF_lOkF8WpYaxZy2ZaOlr24BH-oco9gzsj4guKcuI_aiUn707bODu1qY1DA_8cQoUWdaaj4Gol/s1600/animate.gif" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Animation using new fearute of gnuplot4.6</td></tr>
</tbody></table>
<pre>
</pre>数声风笛离亭晚,我想潇湘君想秦!http://www.blogger.com/profile/08963583502168321534noreply@blogger.com5tag:blogger.com,1999:blog-8684207873816623164.post-13476476406490932322012-03-16T05:04:00.000-07:002012-03-27T23:28:45.166-07:00The diffrence among various forms of quotation marksThere are three different forms of quotation marks in gnuplot. They are single quote, double quote and backquote. <br />
<br />
The main difference between sing and double quote marks is whether backslash processing of special characters can be used. In double quote the backslash pattern is effective, so if you type in the following command<br />
<div class="codes">
<pre>print "The first line.\nThe second line."
</pre>
</div>
Then it will comes out to be <br />
<div class="codes">
<pre>The first line.
The second line.
</pre>
</div>
While if you type in the command like this:<br />
<div class="codes">
<pre>print 'The first line.\nThe second line.'
</pre>
</div>
Then the out coming string will be <br />
<div class="codes">
<pre>The first line.\nThe second line.
</pre>
</div>
<br />
The back quotes have a absolutely different utility. It is used to enclose system commands for substitution into the command lines. (Note that this utility is not supported on all platforms.) For example in a Linux platform you can add a watermark to your graph like this:<br />
<br />
<div class="codes">
<pre>set label "By gnuplot-surprising at `date +%Y-%m-%d`" center\
at screen 0.5,0.5 tc rgb"#cccccc"
plot sin(x)
</pre>
</div>
<br />
The watermarked picture looks like this<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiXtONAI7LCn9O_WdD5i84oS9a9PLo8CrYsgQNSmhJB93oi5ilmzUNXl9d0FvfuVU9mZXzS_zQ6UzH_XrfttilFvSg4_tmHO2TVYXpZt-we96L4ANAVuWkw5qDnEVMsbm3aRU5TqbWKlSzi/s1600/sub.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiXtONAI7LCn9O_WdD5i84oS9a9PLo8CrYsgQNSmhJB93oi5ilmzUNXl9d0FvfuVU9mZXzS_zQ6UzH_XrfttilFvSg4_tmHO2TVYXpZt-we96L4ANAVuWkw5qDnEVMsbm3aRU5TqbWKlSzi/s320/sub.png" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Back quotes in gnuplot --- substitution</td></tr>
</tbody></table>数声风笛离亭晚,我想潇湘君想秦!http://www.blogger.com/profile/08963583502168321534noreply@blogger.com2tag:blogger.com,1999:blog-8684207873816623164.post-37527919734970206652012-02-17T22:55:00.000-08:002012-02-17T22:55:47.880-08:00How to plot a cross shaped axis when using gnuplotIt is always asked by my friends that how to plot a graph with cross shaped axis when using gnuplot. To the best of my knowledge, there is not such a built in axis style in gnuplot. So you have to draw the axis manually. Here a script is provided. You can just use it. If you would like to make some improvements, It is welcomed.<br />
<br />
<div class="codes">
<pre>#frame.gnuplot
#Author: 数声风笛离亭晚,我想潇湘君想秦
##########################################################
#Utility: plot a "+" style axis
#Usage:
#call "frame.gnuplot" "ox" "oy" "xmin" "xmax" "ymin"\
# "ymax" "dx" "dy"
#Parameters:
#(Frame_ox,Frame_oy): The position of the origin
#(Frame_xmin,Frame_ymin): The bottum-left point
#(Frame_xmax,Frame_ymax): The top-right point
#Frame_dx: The interval of x-ticks
#Frame_dy: The interval of y-ticks
#Before calling this script, Frame_xtic_len and
#Frame_ytic_len can be set to control the length
#of the corresponding tick length. The length is
#measured in graph coordinate. The default values
#are 0.02 and 0.01.
#Example:
#Frame_xtic_len = 0.01
#Frame_ytic_len = 0.01
#call "frame.gnuplot" "0." "1.0" "-10." "10." "0." \
# "2." "2." "0.2"
#plot sin(x)+1.
##########################################################
Frame_ox=$0
Frame_oy=$1
Frame_xmin=$2
Frame_xmax=$3
Frame_ymin=$4
Frame_ymax=$5
Frame_dx=$6
Frame_dy=$7
#set the default value of Frame_x(y)tic_len
if (!exist("Frame_xtic_len")) Frame_xtic_len=0.02
if (!exist("Frame_ytic_len")) Frame_ytic_len=0.01
#no plot of the default border and tics
set border 0
unset tics
#set the xrange and yrange to the user defined value
set xrange [Frame_xmin:Frame_xmax]
set yrange [Frame_ymin:Frame_ymax]
#draw the axis
set arrow from graph 0, first Frame_oy to graph 1.03,\
first Frame_oy size graph 0.02,15,60 fill ls -1
set arrow from first Frame_ox, graph 0 to first Frame_ox,\
graph 1.03 size graph 0.015,15,60 fill ls -1
#draw the ticks
set for [i=0:int((Frame_xmax-Frame_xmin)/Frame_dx)] \
arrow from Frame_xmin+i*Frame_dx,Frame_oy to \
Frame_xmin+i*Frame_dx,Frame_oy+ \
(Frame_ymax - Frame_ymin)*Frame_xtic_len nohead
set for [i=0:int((Frame_ymax-Frame_ymin)/Frame_dy)] \
arrow from Frame_ox,Frame_ymin+i*Frame_dy to \
Frame_ox+(Frame_xmax - Frame_xmin)*Frame_ytic_len, \
Frame_ymin+i*Frame_dy nohead
#draw the labels with the ticks
set for [i=0:int((Frame_xmax-Frame_xmin)/Frame_dx)] \
label sprintf("%3.1f",Frame_xmin+i*Frame_dx) at \
Frame_xmin+i*Frame_dx,Frame_oy center offset 0,-0.75
set for [i=0:int((Frame_ymax-Frame_ymin)/Frame_dy)] \
label sprintf("%3.1f",Frame_ymin+i*Frame_dy) at \
Frame_ox,Frame_ymin+i*Frame_dy right offset -0.75,0
</pre>
</div>
<br />
Now let us show how this script is used by an example.<br />
<br />
<div class="codes">
<pre>Frame_xtic_len = 0.01
Frame_ytic_len = 0.01
call "frame.gnuplot" "0." "1.0" "-10." "10." "0." \
"2." "2." "0.2"
plot sin(x)+1. notitle
</pre>
</div>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjiUJOuNtWSxLURj2Un8IO2O5bQg0xQ5QfrZ-zdvh6-gAas4BKd65yj1Rxv4vPeClpBHiAj9dPJ_ifMyOruWSx99IR7PajdNILUxv7g8O6GxN-hPfGeTtBJ_PMgoM5TVi70UzKNTMDNOFh8/s1600/cross_style_axis.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="182" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjiUJOuNtWSxLURj2Un8IO2O5bQg0xQ5QfrZ-zdvh6-gAas4BKd65yj1Rxv4vPeClpBHiAj9dPJ_ifMyOruWSx99IR7PajdNILUxv7g8O6GxN-hPfGeTtBJ_PMgoM5TVi70UzKNTMDNOFh8/s320/cross_style_axis.jpg" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Fig.1 Cross shaped axis plotted by gnuplot</td></tr>
</tbody></table>
<br />
<br />数声风笛离亭晚,我想潇湘君想秦!http://www.blogger.com/profile/08963583502168321534noreply@blogger.com4tag:blogger.com,1999:blog-8684207873816623164.post-60018540352663203462012-01-09T04:00:00.001-08:002012-01-09T04:00:40.446-08:00gnuplot的多国语言支持----通过latex(2)上一个帖子中我们介绍了通过latex实现gnuplot的多国语言支持。但是该方法有些繁琐——需要两步才能得到最后的图像。在实际应用的时候该方法可以通过(*)latex终端类型的“header”参数来简化。若将latex宏包调用以及其他一些命令放到该参数后面则生成的tex文件将在文件头部分包含这些信息。这样就不需要手动修改tex文件了,一步就可以得到最后的图像。<br />
<br />
试一试下面的脚步。运行之,然后用latex编译生成的tex文件,编译生成的文件已经中文支持。<br />
<br />
<div class="codes"><pre>reset
set term epslatex color standalone header \
"\\usepackage{CJK}\n \
\\AtBeginDocument{\\begin{CJK*}{GBK}{song}} \
\\AtEndDocument{\\end{CJK*}}"
set output "voltage.tex"
set xlabel "时间(秒)"
set ylabel "电压(伏)"
unset key
plot sin(x)+0.1*(2*rand(0)-1.) w l lw 2
set output
</pre></div><br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVEyDwGmCCYhLMaaV5x1C5gOBF4w2oiMeR_rCxUtAU_Bsi8bAtqmUGmmkoAg8hdiAfGPFyc7al19Pq1BhzJ1YUiqJPWm6PZhf-P7TIpifrd5W6ceoLaiPed-xHNFX6mOzB_ChuOjh3iWd1/s1600/voltage_chinese.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="224" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVEyDwGmCCYhLMaaV5x1C5gOBF4w2oiMeR_rCxUtAU_Bsi8bAtqmUGmmkoAg8hdiAfGPFyc7al19Pq1BhzJ1YUiqJPWm6PZhf-P7TIpifrd5W6ceoLaiPed-xHNFX6mOzB_ChuOjh3iWd1/s320/voltage_chinese.jpg" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">gnuplot绘制的支持中文的图像</td></tr>
</tbody></table>数声风笛离亭晚,我想潇湘君想秦!http://www.blogger.com/profile/08963583502168321534noreply@blogger.com0tag:blogger.com,1999:blog-8684207873816623164.post-29929876516959038642011-12-22T02:17:00.000-08:002011-12-22T02:17:52.059-08:00Multi-language support of gnuplot --- Using latex (2)We have shown how to get multi-language support by using latex in the former posts. It works, but a little complicated --- You need to take two steps to get the final graph. In practice, it can be simplified by using an option of the (*)latex terminal. This option is "header". You can put the package use and other commands after this option, and then the generated tex file will contain these information at the header part. So you need not to modify the tex file manually. You get the final graph at one step.<br />
<br />
So try the following plot script. Run it and compile the output tex file using latex, and at once you a Chinese supported graph.<br />
<br />
<div class="codes">
<pre>reset
set term epslatex color standalone header \
"\\usepackage{CJK}\n \
\\AtBeginDocument{\\begin{CJK*}{GBK}{song}} \
\\AtEndDocument{\\end{CJK*}}"
set output "voltage.tex"
set xlabel "时间(秒)"
set ylabel "电压(伏)"
unset key
plot sin(x)+0.1*(2*rand(0)-1.) w l lw 2
set output
</pre>
</div>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVEyDwGmCCYhLMaaV5x1C5gOBF4w2oiMeR_rCxUtAU_Bsi8bAtqmUGmmkoAg8hdiAfGPFyc7al19Pq1BhzJ1YUiqJPWm6PZhf-P7TIpifrd5W6ceoLaiPed-xHNFX6mOzB_ChuOjh3iWd1/s1600/voltage_chinese.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="224" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVEyDwGmCCYhLMaaV5x1C5gOBF4w2oiMeR_rCxUtAU_Bsi8bAtqmUGmmkoAg8hdiAfGPFyc7al19Pq1BhzJ1YUiqJPWm6PZhf-P7TIpifrd5W6ceoLaiPed-xHNFX6mOzB_ChuOjh3iWd1/s320/voltage_chinese.jpg" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Gnuplot plotted graph with Chinese support</td></tr>
</tbody></table>数声风笛离亭晚,我想潇湘君想秦!http://www.blogger.com/profile/08963583502168321534noreply@blogger.com1tag:blogger.com,1999:blog-8684207873816623164.post-86210195884108670092011-12-15T03:53:00.000-08:002012-01-09T04:01:55.268-08:00gnuplot的多国语言支持----通过latex(1)非英语用户经常遇到的一个问题就是在图像中插入非英语字符。对新手而言,有时这是一件很具挑战性的工作。该帖子将讨论如何使用latex来解决该问题。<br />
<br />
我们知道通过使用合适的宏包latex可以进行多语言排版。我们还知道gnuplot的终端类型中包括诸如latex,epslatex等。所以我们可以先在这些终端类型下绘图,然后通过修改tex文件来获得图像的多国语言支持。下面以中文的支持为例来说明该问题。<br />
<br />
首先我们使用下面的脚本来绘图。<br />
<br />
<div class="codes">
<pre>reset
set term epslatex color standalone
set output "voltage.tex"
set xlabel "my xlabel"
set ylabel "my ylabel"
unset key
plot sin(x)+0.1*(2*rand(0)-1.) w l lw 2
set output
</pre>
</div>
<br />
<br />
使用latex编译输出的tex文件,得到如下图片<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKmD99QrXqk6ewwIrvP1c3HWH4e_q8L0gNla-HDFy-H6Mlwu4Aei3JMEJW0Ch90hJoPi8UqMF1bKOuso5VOsiGGatfvc9U0U_MS6aglBSmAXzv4wTba39YSRrr4dcFZ3WlHPB6ENCKqHhK/s1600/voltage.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="224" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKmD99QrXqk6ewwIrvP1c3HWH4e_q8L0gNla-HDFy-H6Mlwu4Aei3JMEJW0Ch90hJoPi8UqMF1bKOuso5VOsiGGatfvc9U0U_MS6aglBSmAXzv4wTba39YSRrr4dcFZ3WlHPB6ENCKqHhK/s320/voltage.jpg" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">无中文支持的gnuplot绘图结果</td></tr>
</tbody></table>
<br />
我们希望将x轴标注为“时间(秒)”,y轴标注为“电压(伏)”。为实现该效果,使用任意文本编辑软件打开tex文件,找到如下行<br />
<br />
<div class="codes">
<pre>\put(4039,154){\makebox(0,0){\strut{}my xlabel}}%
</pre>
</div>
<br />
和<br />
<br />
<div class="codes">
<pre>\put(308,2739){\rotatebox{-270}{\makebox(0,0){\strut{}my ylabel}}}
</pre>
</div>
<br />
然后将“my xlabel”替换为“时间(秒)”,将“my ylabel”替换为“电压(伏)”。同时将下面的代码加到文件的头部分来得到latex对中文的支持。<br />
<br />
<div class="codes">
<pre>\usepackage{CJK}
\AtBeginDocument{\begin{CJK*}{GBK}{song}}
\AtEndDocument{\end{CJK*}}
</pre>
</div>
<br />
此时再次编译tex文件就可以得到中文支持的的图片了。<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVEyDwGmCCYhLMaaV5x1C5gOBF4w2oiMeR_rCxUtAU_Bsi8bAtqmUGmmkoAg8hdiAfGPFyc7al19Pq1BhzJ1YUiqJPWm6PZhf-P7TIpifrd5W6ceoLaiPed-xHNFX6mOzB_ChuOjh3iWd1/s1600/voltage_chinese.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="224" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVEyDwGmCCYhLMaaV5x1C5gOBF4w2oiMeR_rCxUtAU_Bsi8bAtqmUGmmkoAg8hdiAfGPFyc7al19Pq1BhzJ1YUiqJPWm6PZhf-P7TIpifrd5W6ceoLaiPed-xHNFX6mOzB_ChuOjh3iWd1/s320/voltage_chinese.jpg" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">中文支持的gnuplot绘图结果</td></tr>
</tbody></table>
<br />
修改前和修改后的tex可以通过下面链接下载。<br />
<br />
<a href="https://docs.google.com/open?id=0B5wSAiwiTq4gOTViYzViMGMtN2MzZi00NTc5LTgzNjMtZjc4ZDMwYjM2OWI4" target="_blank">voltage.tex</a> <a href="https://docs.google.com/open?id=0B5wSAiwiTq4gNGY5ZTU1MDUtNGI4OS00YWM0LThhNWItMGRlMTM3NTM0N2Fi" target="_blank">voltage_chinese.tex</a>数声风笛离亭晚,我想潇湘君想秦!http://www.blogger.com/profile/08963583502168321534noreply@blogger.com0tag:blogger.com,1999:blog-8684207873816623164.post-36195426076765561722011-12-03T23:42:00.001-08:002012-01-09T04:01:05.423-08:00Multi-language support of gnuplot --- Using latexFor some non-English speakers, inserting some their own characters into the graph is usual. And for some fresh, this some times becomes a challenging task. This posts will talk about how to solve this problem using latex.<br />
<br />
We know that some packages used latex can deal with multi-language typesetting. And we also know that gnuplot have terminals like latex, epslatex. So we can first plot a English graph with these terminals, then modify the output tex files, and at last get a multi-language supported graph. Take Chinese support for example.<br />
<br />
We first plot a graph using the following script.<br />
<br />
<div class="codes"><pre>reset
set term epslatex color standalone
set output "voltage.tex"
set xlabel "my xlabel"
set ylabel "my ylabel"
unset key
plot sin(x)+0.1*(2*rand(0)-1.) w l lw 2
set output
</pre></div><br />
Compile the output tex file using latex we get a graph like the following one.<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKmD99QrXqk6ewwIrvP1c3HWH4e_q8L0gNla-HDFy-H6Mlwu4Aei3JMEJW0Ch90hJoPi8UqMF1bKOuso5VOsiGGatfvc9U0U_MS6aglBSmAXzv4wTba39YSRrr4dcFZ3WlHPB6ENCKqHhK/s1600/voltage.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="224" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKmD99QrXqk6ewwIrvP1c3HWH4e_q8L0gNla-HDFy-H6Mlwu4Aei3JMEJW0Ch90hJoPi8UqMF1bKOuso5VOsiGGatfvc9U0U_MS6aglBSmAXzv4wTba39YSRrr4dcFZ3WlHPB6ENCKqHhK/s320/voltage.jpg" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Gnuplot plotted graph without Chinese support</td></tr>
</tbody></table>And we want to label the x-axes with "时间(秒)"(means time) and y-axes with "电压(伏)"(means voltage). To realize this effect, we open the tex file with any text editor and find lines<br />
<br />
<div class="codes"><pre>\put(4039,154){\makebox(0,0){\strut{}my xlabel}}%
</pre></div><br />
and<br />
<br />
<div class="codes"><pre>\put(308,2739){\rotatebox{-270}{\makebox(0,0){\strut{}my ylabel}}}
</pre></div><br />
Then we repalce "my xlabel" with "时间(秒)", "my ylabel" with "电压(伏)". And add the following codes into the header part of the file to get the chinese support of latex.<br />
<br />
<div class="codes"><pre>\usepackage{CJK}
\AtBeginDocument{\begin{CJK*}{GBK}{song}}
\AtEndDocument{\end{CJK*}}
</pre></div>This time compile the tex file uising latex again. And a chinese supported graph is gotten.<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVEyDwGmCCYhLMaaV5x1C5gOBF4w2oiMeR_rCxUtAU_Bsi8bAtqmUGmmkoAg8hdiAfGPFyc7al19Pq1BhzJ1YUiqJPWm6PZhf-P7TIpifrd5W6ceoLaiPed-xHNFX6mOzB_ChuOjh3iWd1/s1600/voltage_chinese.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="224" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVEyDwGmCCYhLMaaV5x1C5gOBF4w2oiMeR_rCxUtAU_Bsi8bAtqmUGmmkoAg8hdiAfGPFyc7al19Pq1BhzJ1YUiqJPWm6PZhf-P7TIpifrd5W6ceoLaiPed-xHNFX6mOzB_ChuOjh3iWd1/s320/voltage_chinese.jpg" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Gnuplot plotted graph with Chinese support</td></tr>
</tbody></table>The modified and pre-modified tex files can be downloaded here.<br />
<a href="https://docs.google.com/open?id=0B5wSAiwiTq4gOTViYzViMGMtN2MzZi00NTc5LTgzNjMtZjc4ZDMwYjM2OWI4" target="_blank">voltage.tex</a> <a href="https://docs.google.com/open?id=0B5wSAiwiTq4gNGY5ZTU1MDUtNGI4OS00YWM0LThhNWItMGRlMTM3NTM0N2Fi" target="_blank">voltage_chinese.tex</a>数声风笛离亭晚,我想潇湘君想秦!http://www.blogger.com/profile/08963583502168321534noreply@blogger.com5tag:blogger.com,1999:blog-8684207873816623164.post-24685474929198863662011-11-22T02:21:00.001-08:002011-11-22T02:25:25.467-08:00How to plot nothing using gnuplotSome times we just want to create a figure with only the axes ticks and so on, no any content (The content may be added later use some other tools). We know that if there is not a plot command, gnuplot will not create a output picture, So play some tricks should be played. In this post I will talk on two methods to plot nothing using gnuplot.<br />
<br />
The first one is plot a line with background color.<br />
<div class="codes">
<pre>reset
set term png font ",20" xffffff #set png terminal and the background color is white
set output "blank.png"
set key off
set ytics 0.5
plot sin(x) with line linecolor rgb"#ffffff"
set output
</pre>
</div>
<br />
The second one is plot something out of yrange.<br />
<div class="codes">
<pre>set term png font ",20"
set output "blank.png"
set yrange [-1:1]
set key off
plot 2 with lines
set output
</pre>
</div>
Maybe some other methods also exist, But this three shold be the most used and simplest ones. At last a sample picture posted. (The two methods will just creat pictures the simillar as this one.)<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjkeAwH_4A7ajv95FJRz7hkelU-kcOuteKMVcuPuxwKW2myuSGNmjoG0XXUrXpYihUrpFvlXIFaHizZpVJ6BVLG83QUQPnaTqIIbAqO1yPP7VGCR0RG-wwSt8Df_U_rqidZTlgCDrbuKYq/s1600/blank.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjkeAwH_4A7ajv95FJRz7hkelU-kcOuteKMVcuPuxwKW2myuSGNmjoG0XXUrXpYihUrpFvlXIFaHizZpVJ6BVLG83QUQPnaTqIIbAqO1yPP7VGCR0RG-wwSt8Df_U_rqidZTlgCDrbuKYq/s320/blank.png" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Plot nothing using gnuplot</td></tr>
</tbody></table>数声风笛离亭晚,我想潇湘君想秦!http://www.blogger.com/profile/08963583502168321534noreply@blogger.com9tag:blogger.com,1999:blog-8684207873816623164.post-16314688583888363192011-11-15T03:18:00.001-08:002011-11-15T03:51:43.828-08:00Aviodding the blank margin of a eps picture being auto croped by some softwaresSome one complained to me that when he inserted a gnuplot produced eps picture into some software (not all) the blank margins are auto cropped. And he want to avoid this to happen.<br />
Yes, I also found this problem myself. Some software "intelligently" crop the blank margin of a postscript picture. Always this is just OK. But in some special cases, we set a blank margin of special purpose, for example, to leave some spaces for readers to add some notes. In these cases, the "intelligence" becomes "stupid".<br />
I solve this problem by adding a invisible rectangle (with linewidth equals 0 or with color same as the background color and fillstyle onborder) as large as the screen, i.e.,<br />
<div class="codes"><pre>set object rectangle from screen 0,0 to screen 1,1 \
lw 0 fillstyle noborder behind
</pre></div>Then although there are blank margins but they are not blank in fact. So when the picture is inserted to other software, the margin will not be auto cropped.数声风笛离亭晚,我想潇湘君想秦!http://www.blogger.com/profile/08963583502168321534noreply@blogger.com0tag:blogger.com,1999:blog-8684207873816623164.post-72260256729212166772011-10-30T04:01:00.000-07:002011-10-30T04:02:27.549-07:00Add value labels to the top of bars in a bar chartIt is easy to plot a bar chart with gnuplot using plot style boxes or histogram. This time we talk about how to add values labels to the top of bars in a bar chart.<br />
<br />
The first coming idea is adding labels manually using command "set label ...". This a method but a poor efficient one. A better one is plot the labels with plot style "labels". And this is the method we will talk about here. A sample script is shown below.<br />
<div class="codes">
<pre>reset
set term png font "Times,18" #set terminal and output file
set output "bar_labels.png"
set xlabel "x value" #set x and y label
set ylabel "Frequency"
set xrange [-0.5:4.5] #set x and y range
set yrange [0:4]
set xtics 0,1,4 #set xtics
set style fill solid #set plot style
set boxwidth 0.5
unset key #legend not plotted
plot "bar_data.dat" using 1:2 with boxes,\
"bar_data.dat" using 1:2:2 with labels
#plot bar chart and the value labels on the bars
</pre>
</div>
In this script, we have used a data file (bar_data.dat) like this one<br />
<div class="codes">
<pre>0 2
1 3
2 3
3 2
4 2
</pre>
</div>
<br />
Run the script, we get picture file bar_labels.png like the following one.<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLuFJRhuODM6qH65aeCOUBjKZzv7FsLNK3eWkF_rn59ceYnPbmBD6kwCrD5RQI2gsrQe_mUzqbuxypaHTQvgSdveGblbtofaeL3aMv3WghTtxyDxJDMk0HDDPCYUjRPstXwPMGFUNqO88M/s1600/bar_labels.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLuFJRhuODM6qH65aeCOUBjKZzv7FsLNK3eWkF_rn59ceYnPbmBD6kwCrD5RQI2gsrQe_mUzqbuxypaHTQvgSdveGblbtofaeL3aMv3WghTtxyDxJDMk0HDDPCYUjRPstXwPMGFUNqO88M/s320/bar_labels.png" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Gnuplot bar chart with value labels</td></tr>
</tbody></table>
<br />
If you do not like the position of the labels in the upper picture, you can just change it. For example,<br />
<div class="codes">
<pre>lot "bar_data.dat" using 1:2 with boxes,\
"bar_data.dat" using 1:($2+0.25):2 with labels
</pre>
</div>
will put the labels 0.25 units higher.<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEic9-7lG3FpMM7kYVvhkFIF5U7P9dt1Kb6WXGkZeJbhTMkv6XJFz8mV-siH4Wm34kHBYNPCNRTT7hYG31Nh_pSAi278_0-c5SXpHYbouf1E35Flq3UtGLo7PjCdJS3uDy03EfhZQOIKtisR/s1600/bar_labels.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEic9-7lG3FpMM7kYVvhkFIF5U7P9dt1Kb6WXGkZeJbhTMkv6XJFz8mV-siH4Wm34kHBYNPCNRTT7hYG31Nh_pSAi278_0-c5SXpHYbouf1E35Flq3UtGLo7PjCdJS3uDy03EfhZQOIKtisR/s320/bar_labels.png" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Value labels with a 0.25 units higher position</td></tr>
</tbody></table>数声风笛离亭晚,我想潇湘君想秦!http://www.blogger.com/profile/08963583502168321534noreply@blogger.com7tag:blogger.com,1999:blog-8684207873816623164.post-36287373398430714922011-10-19T05:35:00.000-07:002011-10-19T05:37:13.565-07:00Broken axes graph in gnuplot (3)To plot a x-axes broken graph with datafile, the two methods described in the last two posts are also suitable. The first method can be applied straightforwardly. And now we come to an example to apply the second method to a datafile plotting.<br />
<br />
Consider such a case, an experiment team research on a material's thermodynamical properties. They heat it and record the temperature for 13 hours the first day, then its time for they to go home. And they go on their recording the second day. At last they get a data file like the following one.<br />
<div class="codes">
<pre>0.00 25.76
1.00 29.91
2.00 35.33
3.00 38.85
4.00 43.15
5.00 47.35
6.00 50.45
7.00 53.55
8.00 56.12
9.00 57.31
10.00 59.11
11.00 62.21
12.00 62.17
13.00 63.05
23.00 63.56
24.00 62.98
25.00 66.05
26.00 65.88
27.00 64.70
28.00 65.37
29.00 63.12
30.00 65.66
31.00 66.14
32.00 64.92
33.00 65.68
34.00 64.01
35.00 63.50
36.00 63.75
</pre>
</div>
We can find that their is a break when they go home. We will use a broken axes graph to deal with this break. <br />
<div class="codes">
<pre>set term post eps enhanced
set output "broken_axes3.png"
set xrange [0:36-10+0.5]
set yrange [25:70]
set border 2+8
set arrow 1 from 0,25 to 13,25 nohead
set arrow 2 from 13.5,25 to 26.5,25 nohead
set arrow 3 from 0,70 to 13,70 nohead
set arrow 4 from 13.5,70 to 26.5,70 nohead
set arrow 5 from 12.75,24 to 13.25,26 nohead
set arrow 6 from 13.25,24 to 13.75,26 nohead
set arrow 7 from 12.75,69 to 13.25,71 nohead
set arrow 8 from 13.25,69 to 13.75,71 nohead
set xtics ("0" 0, "4" 4, "8" 8, "12" 12,\
"24" 14.5, "28" 18.5, "32" 22.5, "36" 26.5)
set xlabel "Time:t(h)"
set ylabel "Temperature:T({/Symbol \260}C)"
plot "data.dat" u ($1<13. ?$1: $1>13. ?($1-9.5):1/0):2 w lp lw 2 ps 2 notitle
</pre>
</div>
In this script the key point is "u ($1<13 ?$1: $1>13 ?($1-9.5):1/0):2" which transform range [23:36] to range [13.5:26.5] and let range [0:13] uneffected. The followng piture is broken_axes3.png (This png file is converted from broken_axes.eps).<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2kwlEahsrYlBRvCYMiIVt9lLKu9qhGhQaLCuafuqBQV4wjiktkILgy-mk2W2UYmzp7DL4x8pNzQajui68EMHL-ps22OeHssBepqtT-NAy7CDg3CJyAUXLca7XPSrVVtUGrEUBboxAaKIY/s1600/broken_axes3.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="228" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg2kwlEahsrYlBRvCYMiIVt9lLKu9qhGhQaLCuafuqBQV4wjiktkILgy-mk2W2UYmzp7DL4x8pNzQajui68EMHL-ps22OeHssBepqtT-NAy7CDg3CJyAUXLca7XPSrVVtUGrEUBboxAaKIY/s320/broken_axes3.png" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Broken x-axes graph plotted using gnuplot</td></tr>
</tbody></table>数声风笛离亭晚,我想潇湘君想秦!http://www.blogger.com/profile/08963583502168321534noreply@blogger.com7tag:blogger.com,1999:blog-8684207873816623164.post-18718224767793141972011-10-16T06:01:00.000-07:002011-10-16T06:01:10.930-07:00Broken axes graph in gnuplot (2)Last time I said there is a "better but a little tricky" method to plot a broken axes graph. And now let us come to see this method.<br />
<br />
If we want to plot function f(x) in range of [a:b] and [c:d] (i.e., a broken axes graph), we can define a new function:<br />
<pre>g(x) = f(x) a<= x <= b
g(x) = f(x+c-b-dx) b+dx <= x <= b+dx-c+d
g(x) = 1/0 other x
</pre>
where dx is the gap length. We plot this new function and modify xtics to a right one. Then we get a broken x-axes graph which will similar to the one we plotted last time. Let us start work our idea out. <br />
<div class="codes">
<pre>reset
set term png
set output "broken_axes2.png"
#Plot f(x)=sin(x**2/4.) at range [20:22] and [40:42]
f(x)=sin(x**2/4.)
g(x)=20<x && x<22 ?f(x) :22<x && x<22.2 ?1/0 :22.2<x && x<24.2 ?f(x+17.8) :1/0
set xrange [20:24.2]
set yrange [-1:1]
set sample 2000
set border 2+8 #the bottom and top border will be plotted mannually
set arrow 1 from 20,-1 to 22,-1 nohead #bottom and top border
set arrow 2 from 22.2,-1 to 24.2,-1 nohead
set arrow 3 from 20,1 to 22,1 nohead
set arrow 4 from 22.2,1 to 24.2,1 nohead
set arrow 5 from 21.95,-1.05 to 22.05,-0.95 nohead #axes broken indication line
set arrow 6 from 22.15,-1.05 to 22.25,-0.95 nohead
set arrow 7 from 21.95,0.95 to 22.05,1.05 nohead
set arrow 8 from 22.15,0.95 to 22.25,1.05 nohead
#Modify xtics to a right value
set xtics ("20" 20,"20.5" 20.5,"21" 21,"21.5" 21.5,"22" 22,\
"40" 22.2,"40.5" 22.7,"41" 23.2,"41.5" 23.7,"42"24.2)
set xlabel "Time:t(s)"
set ylabel "Signal:U(V)"
plot g(x) w l lw 2 notitle
</pre>
</div>
The picture file broken_axes2.png is shown below.<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_Ltqu2JzNbtpf7uxuxfc13jAOw28994FNxElzn2bGwXvhDKFXknRxyMS-mJsK41hoC0H9J0mbcJmylTdPFTciTdTCjJKvsIjD4A6rpnxHBGXkae6p_-RV9OcNqaPmFWOldStgVE5AEPY9/s1600/broken_axes2.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_Ltqu2JzNbtpf7uxuxfc13jAOw28994FNxElzn2bGwXvhDKFXknRxyMS-mJsK41hoC0H9J0mbcJmylTdPFTciTdTCjJKvsIjD4A6rpnxHBGXkae6p_-RV9OcNqaPmFWOldStgVE5AEPY9/s320/broken_axes2.png" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Broken axes graph plotted by gnuplot</td></tr>
</tbody></table>
<br />
Next time I will put this method to the datafile case.数声风笛离亭晚,我想潇湘君想秦!http://www.blogger.com/profile/08963583502168321534noreply@blogger.com0tag:blogger.com,1999:blog-8684207873816623164.post-54245649933706015072011-10-06T05:19:00.000-07:002011-10-17T06:51:50.451-07:00Broken axes graph in gnuplot (1)If the range of data is large, it is not <span style="color: black; font-size: 10.5pt; line-height: normal;">convenient</span> to plot the whole range. Then broken axes graph is introduced. Let us see a broken axes graph at first and then how it is plotted.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmk1I7ZloISR_EGhCGs82pykGnnRsAaiC7coVOj_-9F-c3PA0P6j_l9yda2jt5DD83Jg11EcCWFnFwdc6C8G9_cjIursKRiMxZZeiOhVPZOBFE1W_SgAUY8-hh_1Rsr36ffdf8qB8KwnK3/s1600/broken_axes1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjmk1I7ZloISR_EGhCGs82pykGnnRsAaiC7coVOj_-9F-c3PA0P6j_l9yda2jt5DD83Jg11EcCWFnFwdc6C8G9_cjIursKRiMxZZeiOhVPZOBFE1W_SgAUY8-hh_1Rsr36ffdf8qB8KwnK3/s320/broken_axes1.png" width="320" /></a></div>
<br />
To plot a broken axes graph like the upper one, there are two methods. We first introduce the one which is simple to understand and a better but a little tricky one will be talked next time. This method uses the "multiplot" mode, plotting the left and right part of the broken axes graph separately. Let us come to see our script. This script just produce the former graph. The explaination of the script is covered as comments of the script and will not be given separately.<br />
<br />
<div class="codes">
<pre>reset
set term png font "Times,15"
set output "broken_axes1.png"
f(x)=exp(-x)*sin(500*x) #plotting function
set yrange [-1:1] #The later two plot have same yrange
set sample 400
set tics nomirror
set tmargin at screen 0.9 #the later two plots will share this tmargin
set bmargin at screen 0.1 #------------------------------------b------
set multiplot #begin multiplot mode
#axes broken line
set arrow 1 from screen 0.5,0.08 to screen 0.52,0.12 nohead
set arrow 2 from screen 0.52,0.08 to screen 0.54,0.12 nohead
set arrow 3 from screen 0.5,0.88 to screen 0.52,0.92 nohead
set arrow 4 from screen 0.52,0.88 to screen 0.54,0.92 nohead
#x,y axis label and title label
set label 1 "Time: t(s)" at screen 0.475,0.025
set label 2 "Signal:U(mV)" at screen 0.025,0.44 rotate by 90
set label 3 center "U=exp(-t)sin(500t)" at screen 0.5,0.95
#The left part
set border 1+2+4 #the right border is not plotted
set lmargin at screen 0.1 #the left-part's location
set rmargin at screen 0.51
set xtics 0,0.02,0.08
plot [0:0.1] f(x) w l lt 1 lw 2 notitle
#unset the labels and arrows, otherwise they will be plot
#for the second time
unset label 1
unset label 2
unset label 3
unset arrow 1
unset arrow 2
unset arrow 3
unset arrow 4
#the right part
set border 1+4+8 #the left border is not plotted
set lmargin at screen 0.53 #the right-part's location
set rmargin at screen 0.94
#ytics is not plotted, as the second plot will share it with the first one
unset ytics
set xtics 0.9,0.02,1.0
plot [0.9:1] f(x) w l lt 1 lw 2
unset multiplot
</pre>
</div>数声风笛离亭晚,我想潇湘君想秦!http://www.blogger.com/profile/08963583502168321534noreply@blogger.com1tag:blogger.com,1999:blog-8684207873816623164.post-64986921498525440112011-10-03T07:36:00.000-07:002012-02-17T22:56:39.395-08:00Round corner key box in gnuplotIn a <a href="http://gnuplot-surprising.blogspot.com/2011/10/round-corner-rectangle-in-gnuplot.html">previous post</a> I talked about create a round corner rectangle in gnuplot. But none application was involved in that post. Today I use the method to create a round corner key box.<br />
<div class="codes">
<pre>###############################################
#Variables:
##(a,b) is the low left vertex of the rectangle
##(c,d) is the up right vertex of the rectangle
##rx is the radius along x-axis
##ry is the radius along y-axis
##x is the independent variable of the curve
f_low(a,b,c,d,rx,ry,x)=a<x && x<a+rx ? \
-ry*sqrt(1-((x-a-rx)/rx)**2)+b+ry : \
a+rx<x && x<c-rx ? b :c-rx<x && x<c ?\
-ry*sqrt(1-((x-c+rx)/rx)**2)+b+ry : 1/0
#The low curve of a round corner rectangle
f_up(a,b,c,d,rx,ry,x)=a<x && x<a+rx ?\
ry*sqrt(1-((x-a-rx)/rx)**2)+d-ry : \
a+rx<x && x<c-rx ? d :c-rx<x && x<c ?\
ry*sqrt(1-((x-c+rx)/rx)**2)+d-ry : 1/0
#The up curve of a round corner rectangle
###############################################
reset
set term png font "Times,18" #terminal and output file
set output "round_corner_rectangle_key_box.png"
set tics out nomirror
unset key #key will be created manually
set sample 1000 #samples
#Setting the back ground color
set object 1 rect from graph 0,0 to graph 1,1 back
set object 1 rect fc rgb "#AAAAFF" fillstyle solid 1.0
#The text of the key (some people call it legend)
set label center "y=f(x)" at 5.75,0.7 front
#x and y label
set xlabel "x"
set ylabel "y=f(x)"
#Plot the curve,round corner rectangle and sample line of key
plot sin(5.*x)*exp(-x*x/20.) w l lw 2 lc rgb"green",\
'+' u 1:(f_low(3.5,0.5,9,0.9,0.5,0.05,$1)):\
(f_up(3.5,0.5,9,0.9,0.5,0.05,$1)) w filledcurve\
lc rgb"pink" notitle,\
x>7.5 && x<8.5 ?0.7:1/0 w l lw 2 lc rgb"green"
</pre>
</div>
Nothing new in this script. The only important and difficult thing is deciding the position of the key text, key sample line and key box. At last we get a picture like this one.<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiK9V9vHdqfBH2I2zaXN1xYYBVlbuasSHxGAM9akNf46Xm15-ucB6rsKu2s2VatFchslLm-ZIqTTtyTGJZ2QWwMuiIpub0uNZ5bU1A2f_invLazCRZOWqWj9x_tMYcwmpHgA-lz7KnEGCwr/s1600/round_corner_rectangle_key_box.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiK9V9vHdqfBH2I2zaXN1xYYBVlbuasSHxGAM9akNf46Xm15-ucB6rsKu2s2VatFchslLm-ZIqTTtyTGJZ2QWwMuiIpub0uNZ5bU1A2f_invLazCRZOWqWj9x_tMYcwmpHgA-lz7KnEGCwr/s320/round_corner_rectangle_key_box.png" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Round corner key box in gnuplot</td></tr>
</tbody></table>
<br />
<br />
<br />
<br />数声风笛离亭晚,我想潇湘君想秦!http://www.blogger.com/profile/08963583502168321534noreply@blogger.com0tag:blogger.com,1999:blog-8684207873816623164.post-23329540423213749752011-10-02T06:08:00.000-07:002012-12-29T23:52:40.432-08:00Pie chart in gnuplotAlthough it is advised that use pie chart as less as possible. Some times a pie chart can clearly show the percentage of some component and it is useful. So I will talk about how to make a pie chart in gnuplot.<br />
<br />
There is no command to create a pie chart in gnuplot. To plot a pie chart we need to play some tricks.<br />
<br />
A pie chart is made up of some sectors with different colors. First we come to see how to plot a sector.<br />
<br />
Sectors are parts of a sphere symmetrical plane, so it easy to plot a sector in parametric mode using splot command. For example, commands<br />
<div class="codes">
<pre>set term png
set output "sector.png"
set parametric
set urange [0:pi/3]
set vrange [0:1]
set view map
set size square
unset border
unset tics
unset colorbox
splot cos(u)*v,sin(u)*v,1 w pm3d notitle
</pre>
</div>
create a sector like follows.<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1RhZW-u45fK3VbnBeWZGn5avR1QaSA1KrtbCD0QS0NQ1ZFtycTZaxYXEBgpbMSMGYXiiWLunnQj9ccBAHspvMgFWadTJDbDApvKPkq1hB0OdjEoKe2_UzhdMlAeqKPp6ynj-PSLnuPU7U/s1600/sector.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1RhZW-u45fK3VbnBeWZGn5avR1QaSA1KrtbCD0QS0NQ1ZFtycTZaxYXEBgpbMSMGYXiiWLunnQj9ccBAHspvMgFWadTJDbDApvKPkq1hB0OdjEoKe2_UzhdMlAeqKPp6ynj-PSLnuPU7U/s320/sector.png" height="240" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Sector plotted using gnuplot</td></tr>
</tbody></table>
Since we have solved the problem of drawing a sector, now we come to make some sectors to form a pie chart. The following is a file describing the income percentage in diffrent country of a company, we would like to plot it to a pie chart.<br />
<div class="codes">
<pre>England 0.2
France 0.1
Canada 0.3
America 0.2
Japan 0.1
China 0.1
</pre>
</div>
The begining and ending angles of a sector is controled by the urange, but it is not convinient to set the urange dynamically in gnuplot. So we will use python to do some assistant work. Our python script is shown below.<br />
<div class="codes">
<pre>import os
import math
def sign(x):
'''
Sign function.
sign(x)=1 when x >=0
sign(x)=1 when else.
'''
if (x>=0):
return 1
else:
return -1
input=file("data.txt","r") #open data file
plot=file("pie.gnuplot","w") #open plot script file
#Some plot commands
plotcommand='''
reset
set term png #terminal and output file
set output "pie.png"
set size square #square size
set isosample 50,50 #samples
set parametric #parametric mode on
set xrange [-1:1] #x,y,v range
set yrange [-1:1]
set vrange [0:1]
unset border #no border, tics and colorbox
unset xtics
unset ytics
unset colorbox
set view map #the view point
set palette defined(0 "red",1 "green",2 "blue",\\
3 "yellow",4 "cyan",5 "brown",6 "greenyellow",\\
7 "gray",8"bisque",9"violet",10"black")
#The color palette
set cbrange [0:10]
set multiplot #multiplot mode
'''
plot.write(plotcommand)
#output the commands to plot script file
u_begin=0.
#The begin value of u(dummy variable in parametric plot)
i=0. #The item indicate
while True:
##Read data
data=input.readline()
if len(data)==0: #if end of data file, break
break
data=data.split()
##Caculate some parameters
u_end=u_begin+float(data[1]) #end value of u
ang=(u_begin+u_end)*math.pi #the angle lables will be rotated
x=math.cos(ang)
x=x+sign(x)*0.2 #x value of label position
y=math.sin(ang)
y=y+sign(y)*0.2 #y value of label position
##Output some plot commands
plot.write("set urange [%f*2*pi:%f*2*pi]\n" \
%(u_begin,u_end)) #command set the range of variable u
plot.write('set label %d center "%s" at %f,%f rotate \
by %f*180/pi\n' %(int(i+1),data[0],x,y,ang))
#command set the labels
plot.write("splot cos(u)*v,sin(u)*v,%f w pm3d \
notitle\n" %i)
#command plot a sector
u_begin=u_end #the next begin value of u
i=i+1
plot.write("unset multiplot") #plot command
input.close() #close files
plot.close()
os.system("gnuplot plot.gplt") #execute the plot script
</pre>
</div>
Note that although we use a python script, the plot is done by gnuplot. Python is used only to create the gnuplot plot script, i.e., to do some text work.<br />
<br />
Run this python script we get a gnuplot script named pie.gnuplot and the following pie chart.<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6WaqQa3h5JNGzxUtKU5GaK9UwMA2eLMbogHsgQ_GOOdN8w8IxNT73uJR8lu-EmViUWl36aQCWay7-tH9So_btsY7YICiP12dPCokt5Ahx8zStZbo_KFb2e4b_I-R8p_doW8xRfUNQnsK3/s1600/pie.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6WaqQa3h5JNGzxUtKU5GaK9UwMA2eLMbogHsgQ_GOOdN8w8IxNT73uJR8lu-EmViUWl36aQCWay7-tH9So_btsY7YICiP12dPCokt5Ahx8zStZbo_KFb2e4b_I-R8p_doW8xRfUNQnsK3/s320/pie.png" height="240" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Fig.1 Pie chart created by gnuplot with assistance of python </td></tr>
</tbody></table>
<br />
<h3>
Posts <a href="http://gnuplot-surprising.blogspot.com/2012/11/plot-pie-chart-using-gnuplot.html">Plot a pie chart using gnuplot</a> and <a href="http://gnuplot-surprising.blogspot.com/2012/12/pie-charting-with-gnuplot.html" target="_blank">Pie charting using gnuplot</a> give new solutions of pie chart plotting and anyone interested in can take for a reference.</h3>
数声风笛离亭晚,我想潇湘君想秦!http://www.blogger.com/profile/08963583502168321534noreply@blogger.com9tag:blogger.com,1999:blog-8684207873816623164.post-50078694539407769482011-10-01T06:13:00.000-07:002012-02-17T22:57:09.968-08:00Round corner rectangle in gnuplotA simple rectangle always looks too hard while a round corner rectangle looks more comfortable. In this article I will talk about how to create an round corner rectangle in gnuplot.<br />
<br />
There is rectangle object in gnuplot, but unfortunately there seems no option to set it a round corner one. So an absolutely new method will be used. I will use plot style "filledcurve" to create an round corner rectangle. Now come to the script.<br />
<div class="codes">
<pre>###############################################
#Variables:
##(a,b) is the low left vertex of the rectangle
##(c,d) is the up right vertex of the rectangle
##rx is the radius along x-axis
##ry is the radius along y-axis
##x is the independent variable of the curve
f_low(a,b,c,d,rx,ry,x)=a<x && x<a+rx ? \
-ry*sqrt(1-((x-a-rx)/rx)**2)+b+ry : \
a+rx<x && x<c-rx ? b :c-rx<x && x<c ?\
-ry*sqrt(1-((x-c+rx)/rx)**2)+b+ry : 1/0
#The low curve of a round corner rectangle
f_up(a,b,c,d,rx,ry,x)=a<x && x<a+rx ?\
ry*sqrt(1-((x-a-rx)/rx)**2)+d-ry : \
a+rx<x && x<c-rx ? d :c-rx<x && x<c ?\
ry*sqrt(1-((x-c+rx)/rx)**2)+d-ry : 1/0
#The up curve of a round corner rectangle
###############################################
unset border #no border
unset tics #no tics
set sample 1000
set xrange [-10:10]
set yrange [-10:10]
set term png
set output "round_corner_rectangle.png"
plot '+' u 1:(f_low(-10,-10,10,10,1,2,$1)):\
(f_up(-10,-10,10,10,1,2,$1)) w filledcurve\
lc rgb"pink" notitle
#plot a round corner rectangle
</pre>
</div>
Load the plotting script, and we get picture file found_corner_rectangle.png like this.<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5YV058_lpo06OsjeYRKVH7SYjc593Nw5B723wnAPnAUptFnhK2krEkFIZjyoS0Rdxe0KXahR70d0fbXBUONnMhAfoOUlPcYJtzK8pCXbwCgRYA8i1O3jgsH50ePQuejxz8q5aiInMwYcN/s1600/round_corner_rectangle.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="240" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi5YV058_lpo06OsjeYRKVH7SYjc593Nw5B723wnAPnAUptFnhK2krEkFIZjyoS0Rdxe0KXahR70d0fbXBUONnMhAfoOUlPcYJtzK8pCXbwCgRYA8i1O3jgsH50ePQuejxz8q5aiInMwYcN/s320/round_corner_rectangle.png" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Round corner rectangle created using filledcurve plot style in gunplot</td></tr>
</tbody></table>数声风笛离亭晚,我想潇湘君想秦!http://www.blogger.com/profile/08963583502168321534noreply@blogger.com2tag:blogger.com,1999:blog-8684207873816623164.post-74676233582842758952011-09-30T07:58:00.000-07:002012-02-17T22:57:26.832-08:00Rotate a picture using gnuplotLast time I promised rotating a picture using gnuplot will be talked. Today I come to realize my promise.Let us look at the script at first.<br />
<div class="codes">
<pre>#Utility:Convert a rgb colorized image to a gray one
#Author:数声风笛离亭晚,我想潇湘君想秦!
#Email:qinjieli@gmail.com
#Usage:
##call "rotate.gnuplot" angle inputfile outputfile
#parameters:
##angle: the angle to be rotated
##inputfile: input filename (without ".png")
##outputfile: outputfilename (without ".png")
reset
angle=$0 #angle to be rotated
angle=angle*pi/180 #change degrees to radians
inputfile="$1.png" #input file
outputfile="$2.png" #ouput file
set size ratio -1
# Set the scales so that the unit has the same length
#on both the x and y axes
set lmargin 0 #nO margin
set rmargin 0
set tmargin 0
set bmargin 0
unset tics #no tics and border line
unset border
#plot and get the size of the rotated picture
plot inputfile binary filetype=png rotate=angle w rgbima notitle
xmax=GPVAL_DATA_X_MAX
xmin=GPVAL_DATA_X_MIN
ymin=GPVAL_DATA_Y_MIN
ymax=GPVAL_DATA_Y_MAX
set xrange [xmin:xmax] #set x and y range
set yrange [ymin:ymax]
set term png truecolor size (xmax-xmin),(ymax-ymin) #set the terminal
set output outputfile
plot inputfile binary filetype=png rotate=angle w rgbima notitle
</pre>
</div>
The script first plot the plot the rotated picture on screen. This plot has two functions, determining the size of the output picture and letting the user have view at the rotated picture. Save the script as "rotate.gnuplot", and then it can be called like this:<br />
<div class="codes">
<pre>gnuplot> call "rotate.gnuplot" 30 input output
</pre>
</div>
At last is one of our finished work.<br />
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxp3UDOs5F8KY_74tAgk5KFqWhOokKn-8Y3nBPm2v-hSJXU7TxVozYyXtVRuiSlpoiAdEI4Cl4h_S66FXJhgVLREl7TsxidwfQKUzRa9VLy_9pC1jyXekkQCNRbJxHwRxnVgmAwu7yh36a/s1600/input1.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="213" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgxp3UDOs5F8KY_74tAgk5KFqWhOokKn-8Y3nBPm2v-hSJXU7TxVozYyXtVRuiSlpoiAdEI4Cl4h_S66FXJhgVLREl7TsxidwfQKUzRa9VLy_9pC1jyXekkQCNRbJxHwRxnVgmAwu7yh36a/s320/input1.png" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Input picture file (initial file download from <a href="http://www.flickr.com/photos/2493/1078598654/sizes/z/in/photostream/">here</a>)</td></tr>
</tbody></table>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1aPowEaAX6NMUIlvuHIcgw4Gra69dGGodi23bGKuxVS5Vo0N2Am9bTrzrA7wBnDkmwYvwMtgRPh3ndsWFwGS84qGon8JySVm823n4bP0E1_t2Hp8bY8zCaRnYrMgb6L1dq2_KASMGFJe0/s1600/output.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" height="287" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh1aPowEaAX6NMUIlvuHIcgw4Gra69dGGodi23bGKuxVS5Vo0N2Am9bTrzrA7wBnDkmwYvwMtgRPh3ndsWFwGS84qGon8JySVm823n4bP0E1_t2Hp8bY8zCaRnYrMgb6L1dq2_KASMGFJe0/s320/output.png" width="320" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;">Picture rotated by 30 degree using our script</td></tr>
</tbody></table>
<br />
<br />数声风笛离亭晚,我想潇湘君想秦!http://www.blogger.com/profile/08963583502168321534noreply@blogger.com1