<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>日期 &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/%E6%97%A5%E6%9C%9F/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Tue, 28 Apr 2020 02:07:06 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>[原创] gnuplot调教记</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-gnuplot%e8%b0%83%e6%95%99%e8%ae%b0/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-gnuplot%e8%b0%83%e6%95%99%e8%ae%b0/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Fri, 24 Oct 2014 09:48:59 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[综合]]></category>
		<category><![CDATA[gnuplot]]></category>
		<category><![CDATA[X轴重复]]></category>
		<category><![CDATA[中文]]></category>
		<category><![CDATA[乱码]]></category>
		<category><![CDATA[字体]]></category>
		<category><![CDATA[日期]]></category>
		<category><![CDATA[日期重复]]></category>
		<category><![CDATA[画图]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=8348</guid>

					<description><![CDATA[<p>
<a href="http://www.gnuplot.info/" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">gnuplot</span></a>是命令行绘图的极佳选择之一。用它画出一副简单的图极其容易，但是对画出的图进行微调却很令人抓狂，这主要&#8220;得益&#8221;于gnuplot超级强大的功能，以及无穷无尽纷繁复杂的说明文档，通常让人没时间去细细研究。<br />
如果你经常有gnuplot的使用需求，那么可以仔细研读一下它的文档，否则，还是像我一样，现学现用吧。<br />
我折腾了一天多时间，把我想要的一副图给画出来了，里面涉及到了很多图像的微调过程，有些解决方案很难搜到，但我运气稍好，终究还是弄出来了，在此，作为一个使用案例来分析一下。因此，本文并不是gnuplot的使用教程，而是用gnuplot绘图时，对某些奇怪问题的解决办法的分享。</p>
<p>本文基于gnuplot版本：4.4，系统：RHEL 5.3<br />
<span id="more-8348"></span><br />
<span style="background-color:#00ff00;">『1』</span>基本需求<br />
先说一下我要画图的基本需求。听上去很简单：就是二维平面上的X，Y坐标点线图。我有一个数据文件<span style="color:#0000ff;">data.txt</span>，第一列是日期，第二列是该日期下的数值，例如：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
20141020	1525
20141019	1429
20141018	1321
20141017	1780
20141016	1655
</pre>
<p>下面来看看怎么把它画成图。</p>
<p><span style="background-color: rgb(0, 255, 0);">『2』</span>编写最简单的shell脚本<br />
在shell脚本中调用gnuplot，绘制出一个最简单的图：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="bash language-bash hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(91, 218, 237); word-wrap: inherit !important; word-break: inherit !important;">#!/bin/bash
</span>
gnuplot&#160;&#60;&#60;&#160;EOF
&#160;&#160;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">set</span>&#160;terminal&#160;png
&#160;&#160;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">set</span>&#160;xdata&#160;time
&#160;&#160;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">set</span>&#160;timefmt&#160;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;%Y%m%d&#34;</span>
&#160;&#160;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">set</span>&#160;output&#160;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;output.png&#34;</span></code></pre>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-gnuplot%e8%b0%83%e6%95%99%e8%ae%b0/" class="read-more">Read More </a></section>]]></description>
										<content:encoded><![CDATA[<p>
<a href="http://www.gnuplot.info/" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">gnuplot</span></a>是命令行绘图的极佳选择之一。用它画出一副简单的图极其容易，但是对画出的图进行微调却很令人抓狂，这主要&ldquo;得益&rdquo;于gnuplot超级强大的功能，以及无穷无尽纷繁复杂的说明文档，通常让人没时间去细细研究。<br />
如果你经常有gnuplot的使用需求，那么可以仔细研读一下它的文档，否则，还是像我一样，现学现用吧。<br />
我折腾了一天多时间，把我想要的一副图给画出来了，里面涉及到了很多图像的微调过程，有些解决方案很难搜到，但我运气稍好，终究还是弄出来了，在此，作为一个使用案例来分析一下。因此，本文并不是gnuplot的使用教程，而是用gnuplot绘图时，对某些奇怪问题的解决办法的分享。</p>
<p>本文基于gnuplot版本：4.4，系统：RHEL 5.3<br />
<span id="more-8348"></span><br />
<span style="background-color:#00ff00;">『1』</span>基本需求<br />
先说一下我要画图的基本需求。听上去很简单：就是二维平面上的X，Y坐标点线图。我有一个数据文件<span style="color:#0000ff;">data.txt</span>，第一列是日期，第二列是该日期下的数值，例如：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
20141020	1525
20141019	1429
20141018	1321
20141017	1780
20141016	1655
</pre>
<p>下面来看看怎么把它画成图。</p>
<p><span style="background-color: rgb(0, 255, 0);">『2』</span>编写最简单的shell脚本<br />
在shell脚本中调用gnuplot，绘制出一个最简单的图：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="bash language-bash hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(91, 218, 237); word-wrap: inherit !important; word-break: inherit !important;">#!/bin/bash
</span>
gnuplot&nbsp;&lt;&lt;&nbsp;EOF
&nbsp;&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">set</span>&nbsp;terminal&nbsp;png
&nbsp;&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">set</span>&nbsp;xdata&nbsp;time
&nbsp;&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">set</span>&nbsp;timefmt&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;%Y%m%d&quot;</span>
&nbsp;&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">set</span>&nbsp;output&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;output.png&quot;</span>
&nbsp;&nbsp;plot&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;data.txt&quot;</span>&nbsp;using&nbsp;1:2
&nbsp;&nbsp;&nbsp;&nbsp;quit
EOF
</code></pre>
</section>
<p>画出的图是这样的：</p>
<div style="text-align: center;">
	<img decoding="async" alt="" src="http://www.codelast.com/wp-content/uploads/2014/10/gnuplot_output_img_1.png" style="width: 640px; height: 480px;" /></div>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
显然这个图非常难看，并且图上的某些内容让人匪夷所思，我们先不管它，先看看它是怎么运作的。<br />
在shell脚本中两个 <span style="color:#0000ff;">EOF</span> 中间的是gnuplot的绘图参数设置命令，每一条命令的含义分别是：<br />
<span style="color:#ff0000;">set terminal png</span>：将输出图片设置为png格式。<br />
<span style="color:#ff0000;">set xdata time</span>：设置X轴为时间。<br />
<span style="color:#ff0000;">set timefmt &quot;%Y%m%d&quot;</span>：设置输入数据的时间格式。<br />
<span style="color:#ff0000;">set output &quot;output.png&quot;</span>：设置输出图片的文件名，这里你可以使用绝对路径，此处为了简单，只使用相对路径，将输出到当前目录下。<br />
<span style="color:#ff0000;">plot &quot;data.txt&quot; using 1:2</span>：将 <span style="color:#0000ff;">data.txt</span> 作为输入数据文件，使用其第一列作为X轴，第2列作为Y轴。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
可见要画出一幅图是相当简单的，但是正如前面所说，要调整这幅图到你满意的程度，并不是一件简单的事情&mdash;&mdash;如果不调整，上面的乱糟糟图根本就没法看嘛！</p>
<p><span style="background-color: rgb(0, 255, 0);">『3』</span>漫长的参数调整过程<br />
<span style="background-color:#ffa07a;">（1）</span>去掉右上角的文字说明<br />
右上角有一个 <span style="color:#b22222;">&quot;data.txt&quot; using 1:2</span> 的说明，要去掉它，需要添加一句：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
set nokey
</pre>
<p>画出的图变成了：</p>
<div style="text-align: center;">
	<img decoding="async" alt="" src="http://www.codelast.com/wp-content/uploads/2014/10/gnuplot_output_img_2.png" style="text-align: center; width: 640px; height: 480px;" /></div>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
现在干净一点了。</p>
<p><span style="background-color: rgb(255, 160, 122);">（2）</span>更改X轴的日期显示格式<br />
将X轴的日期改为&ldquo;月-日&rdquo;（例如&ldquo;10-16&rdquo;），加一句代码即可：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
set format x &quot;%m-%d&quot;
</pre>
<p>画出的图变成了：</p>
<div style="text-align: center;">
	<img decoding="async" alt="" src="http://www.codelast.com/wp-content/uploads/2014/10/gnuplot_output_img_3.png" style="text-align: center; width: 640px; height: 480px;" /></div>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
X轴日期显示格式已改，没有悬念。有人说，但是它怎么有重复的日期啊？这个问题后面再说。</p>
<p><span style="background-color: rgb(255, 160, 122);">（3）</span>清除Y轴显示的数值<br />
添加代码：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
set format y &quot;&quot;
</pre>
<p>画出的图变成了：</p>
<div style="text-align: center;">
	<img decoding="async" alt="" src="http://www.codelast.com/wp-content/uploads/2014/10/gnuplot_output_img_4.png" style="text-align: center; width: 640px; height: 480px;" /></div>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
现在Y轴没数字了。</p>
<p><span style="background-color: rgb(255, 160, 122);">（4）</span>添加网格<br />
图中的任意两个点，它们的Y坐标值是否相同？如果这两个点离得稍微远一点，就看不清楚了，因此，为图像添加上网格，可以很好地辅助我们识别它们是否在同一水平线上：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
set grid
</pre>
<p>画出的图变成了：</p>
<div style="text-align: center;">
	<img decoding="async" alt="" src="http://www.codelast.com/wp-content/uploads/2014/10/gnuplot_output_img_5.png" style="text-align: center; width: 640px; height: 480px;" /></div>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color: rgb(255, 160, 122);">（5）</span>去除X轴上重复的日期<br />
添加如下代码：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
set xtics 60*60*24
</pre>
<p>画出的图变成了：</p>
<div style="text-align: center;">
	<img decoding="async" alt="" src="http://www.codelast.com/wp-content/uploads/2014/10/gnuplot_output_img_6.png" style="text-align: center; width: 640px; height: 480px;" /></div>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
这句代码为什么能&ldquo;神奇&rdquo;地修复X轴坐标重复的问题？如果你有耐心，可以看看<a href="http://stackoverflow.com/questions/18647807/gnuplot-is-doubling-x-axis-labels-for-some-reason" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">这篇</span></a>文章的解释。它的大概原理是：当X轴显示的值是时间的时候，xtics 的增量单位是<span style="color:#0000ff;">秒</span>，如果你要它的增量单位是<span style="color:#0000ff;">天</span>，就需要将 xtics 设置为一天的秒数，即<span style="color:#b22222;">60*60*24</span>。</p>
<p><span style="background-color: rgb(255, 160, 122);">（6）</span>设置输入的数据文件的分隔符<br />
前面我们输入的数据文件data.txt以<span style="color:#0000ff;">\t</span>为两列的数据分隔符，但如果我有自定义的分隔符，是否要先把它转成\t，然后才能给gnuplot使用呢？不需要，只要用命令指定分隔符就可以了：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
set datafile separator &quot;\t&quot;
</pre>
<p>
<span style="background-color: rgb(255, 160, 122);">（7）</span>点与点之间连线<br />
上面的图，画出的点实在太小了，如果相邻两点之间能用直线相连，看起来就清楚得多了：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
plot &quot;data.txt&quot; using 1:2 with linespoints
</pre>
<p>画出的图变成了：</p>
<div style="text-align: center;">
	<img decoding="async" alt="" src="http://www.codelast.com/wp-content/uploads/2014/10/gnuplot_output_img_7.png" style="text-align: center; width: 640px; height: 480px;" /></div>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
这是怎么做到的呢？plot 命令中的 <span style="color:#0000ff;">with&nbsp;linespoints</span> 指定了画出的图是点线图。</p>
<p><span style="background-color: rgb(255, 160, 122);">（8）</span>修改一下线的颜色，线的粗细，点的大小<br />
代码：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
plot &quot;data.txt&quot; using 1:2 with linespoints linecolor 3 linewidth 1 pointtype 5 pointsize 1 
</pre>
<p>画出的图变成了：</p>
<div style="text-align: center;">
	<img decoding="async" alt="" src="http://www.codelast.com/wp-content/uploads/2014/10/gnuplot_output_img_8.png" style="text-align: center; width: 640px; height: 480px;" /></div>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
这下就好看多了，在上面的代码中，linecolor指定了线的颜色，linewidth指定了线的宽度，pointtype指定了点的类型，pointsize指定了点的大小，可以参考gnuplot的官方文档获知诸如pointtype之类的代号分别代表什么样的点/线。</p>
<p><span style="background-color: rgb(255, 160, 122);">（9）</span>在点的旁边显示其纵坐标值<br />
前面我们已经把Y轴刻度去掉了，因此有必要在点的旁边显示其纵坐标值：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
plot &quot;data.txt&quot; using 1:2 with linespoints linecolor 3 linewidth 1 pointtype 5 pointsize 1, &quot;&quot; using 1:2:2 with labels
</pre>
<p>画出的图变成了：</p>
<div style="text-align: center;">
	<img decoding="async" alt="" src="http://www.codelast.com/wp-content/uploads/2014/10/gnuplot_output_img_9.png" style="text-align: center; width: 640px; height: 480px;" /></div>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
这一步的代码，比之前的代码多了 <span style="color:#0000ff;">&quot;&quot; using 1:2:2 with labels</span> 这部分。其中，<span style="color:#0000ff;">with labels</span> 表示在点旁边画上标签（label），但是这个label的值是什么呢？这里使用 using 1:2:2 表示第一列为X坐标值，第二列为Y坐标值，同时第二列也作为label的值。如果数据文件data.txt中有第三列，你可以用 using 1:2:3 来使用第三列作为label的值。有人说，上面还有一个空的双引号，是作什么用的呢？Sorry，这个真不记得了...</p>
<p><span style="background-color: rgb(255, 160, 122);">（10）</span>不要让label和数据点交错在一起<br />
上面的数据点被label的数值盖住了，看起来很不爽，我们把数值偏移一定的位置，让它们错开：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
plot &quot;data.txt&quot; using 1:2 with linespoints linecolor 3 linewidth 1 pointtype 5 pointsize 1, &quot;&quot; using 1:2:2 with labels center offset 0,0.5
</pre>
<div style="text-align: center;">
	<img decoding="async" alt="" src="http://www.codelast.com/wp-content/uploads/2014/10/gnuplot_output_img_10.png" style="text-align: center; width: 640px; height: 480px;" /></div>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
这下漂亮多了！起这个作用的代码是&nbsp;<span style="color:#0000ff;">center offset 0,0.5</span>。其中，<span style="color:#0000ff;">center</span> 加不加我没发现有什么区别；<span style="color:#0000ff;">offset 0,0.5</span> 表示把label在X轴方向平称0，在Y轴方向平移0.5。注意，平移的单位既不是像素，也不是和Y轴一样的单位，而是&ldquo;相对距离&rdquo;，不信我们可以把 <span style="color:#0000ff;">offset 0,0.5</span> 改成 <span style="color:#ff0000;">offset -1,1</span>，会看到label向左、向上都平移了一个单位，也就是下面这个样子：</p>
<div style="text-align: center;">
	<img decoding="async" alt="" src="http://www.codelast.com/wp-content/uploads/2014/10/gnuplot_output_img_11.png" style="text-align: center; width: 640px; height: 480px;" /></div>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
可见label平移的距离远不止一个像素点，并且向上平移的距离也不是Y轴的&ldquo;1&rdquo;。<br />
这个把label和数据点错开的方法，代码极其简单，但是为了找到这个solution，我搜遍了网页，试了无数种方法，其他的都有各种各样的显示问题，就是这样写才最好，找得我要吐血。</p>
<p><span style="background-color: rgb(255, 160, 122);">（11）</span>为X轴添加上说明文字<br />
添加代码：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
set xlabel &quot;Date&quot; textcolor lt 1
</pre>
<p>画出的图变成了：</p>
<div style="text-align: center;">
	<img decoding="async" alt="" src="http://www.codelast.com/wp-content/uploads/2014/10/gnuplot_output_img_12.png" style="text-align: center; width: 640px; height: 480px;" /></div>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
其中，<span style="color:#0000ff;">textcolor lt 1</span> 指定了文字的颜色。</p>
<p><span style="background-color: rgb(255, 160, 122);">（12）</span>把X轴的说明文字改成中文<br />
简单地把上面的&ldquo;<span style="color:#b22222;">Date</span>&rdquo;改为中文的&ldquo;日期&rdquo;，看看画出的图会变成什么：</p>
<div style="text-align: center;">
	<img decoding="async" alt="" src="http://www.codelast.com/wp-content/uploads/2014/10/gnuplot_output_img_13.png" style="text-align: center; width: 640px; height: 480px;" /></div>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
不出所料，中文乱码了。<br />
解决这个问题没有那么容易，虽然解决方案有千千万，不过我认为最实用的方法就是：在系统中安装支持中文的字体，并且在使用gnuplot绘图时指定使用这个字体：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
set term png font &quot;/usr/share/fonts/truetype/wqy/wqy-microhei.ttc,12&quot;
</pre>
<p>这句话的前半部分含义与&nbsp;<span style="color:#0000ff;">set terminal png</span> 相同，后半部分指定了使用的字体的路径以及文字的大小。wqy-microhei.ttc 即为<a href="http://wenq.org/wqy2/index.cgi" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">文泉驿</span></a>微米黑字体文件，可以从很多地方下载到，当然你也可以尝试使用其他的字体。字体文件不一定要放在&nbsp;/usr/share/fonts/truetype/wqy/ 路径下，但是最好遵循Linux系统中其他字体的规则。<br />
这样，画出的图就变成了：<br />
<img decoding="async" alt="" src="http://www.codelast.com/wp-content/uploads/2014/10/gnuplot_output_img_14.png" style="text-align: center; width: 640px; height: 480px;" /><br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color: rgb(255, 160, 122);">（13）</span>为图片添加标题（title）<br />
在上面已经搞定中文输出的基础上，我们再添加中文的title就很容易了：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
set title &quot;每天售出的商品数量&quot; textcolor lt 1
</pre>
<p>画出的图变成了：</p>
<div style="text-align: center;">
	<img decoding="async" alt="" src="http://www.codelast.com/wp-content/uploads/2014/10/gnuplot_output_img_15.png" style="text-align: center; width: 640px; height: 480px;" /></div>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color: rgb(255, 160, 122);">（14）</span>图片两边太挤了，空出一定的空隙<br />
添加代码：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
set offset graph 0.10, 0.10
</pre>
<p>画出的图变成了：</p>
<div style="text-align: center;">
	<img decoding="async" alt="" src="http://www.codelast.com/wp-content/uploads/2014/10/gnuplot_output_img_16.png" style="text-align: center; width: 640px; height: 480px;" /></div>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
上面的代码使得我们最左、最右的两个点不会碰到坐标轴，看上去舒服多了。至于它的具体含义，我不记得了，总之可以通过调整offset的数值来观察效果。</p>
<p><span style="background-color: rgb(0, 255, 0);">『4』</span>总结<br />
经过上面的一系列调整，我们终于得到了一幅看上去比较顺眼的图片。最后把完整的代码贴上来：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="bash language-bash hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-meta" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(91, 218, 237); word-wrap: inherit !important; word-break: inherit !important;">#!/bin/bash
</span>
gnuplot&nbsp;&lt;&lt;&nbsp;EOF
&nbsp;&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">set</span>&nbsp;term&nbsp;png&nbsp;font&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;/usr/share/fonts/truetype/wqy/wqy-microhei.ttc,12&quot;</span>
&nbsp;&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">set</span>&nbsp;nokey
&nbsp;&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">set</span>&nbsp;xdata&nbsp;time
&nbsp;&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">set</span>&nbsp;timefmt&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;%Y%m%d&quot;</span>
&nbsp;&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">set</span>&nbsp;format&nbsp;x&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;%m-%d&quot;</span>
&nbsp;&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">set</span>&nbsp;format&nbsp;y&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;&quot;</span>
&nbsp;&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">set</span>&nbsp;offset&nbsp;graph&nbsp;0.10,&nbsp;0.10
&nbsp;&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">set</span>&nbsp;xtics&nbsp;60*60*24
&nbsp;&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">set</span>&nbsp;grid
&nbsp;&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">set</span>&nbsp;title&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;每天售出的商品数量&quot;</span>&nbsp;textcolor&nbsp;lt&nbsp;1
&nbsp;&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">set</span>&nbsp;xlabel&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;日期&quot;</span>&nbsp;textcolor&nbsp;lt&nbsp;1
&nbsp;&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">set</span>&nbsp;output&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;output.png&quot;</span>
&nbsp;&nbsp;<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">set</span>&nbsp;datafile&nbsp;separator&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;\t&quot;</span>
&nbsp;&nbsp;plot&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;data.txt&quot;</span>&nbsp;using&nbsp;1:2&nbsp;with&nbsp;linespoints&nbsp;linecolor&nbsp;3&nbsp;linewidth&nbsp;1&nbsp;pointtype&nbsp;5&nbsp;pointsize&nbsp;1,&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;&quot;</span>&nbsp;using&nbsp;1:2:2&nbsp;with&nbsp;labels&nbsp;center&nbsp;offset&nbsp;0,0.5
&nbsp;&nbsp;&nbsp;&nbsp;quit
EOF
</code></pre>
</section>
<p>
另附几个常见错误的解决办法：<br />
<span style="color:#0000ff;">（1）</span>Could not find/open font when opening font &quot;arial&quot;, using internal non-scalable font<br />
没有设置字体的时候可能会出现这个错误，这时，你可以指定一个系统中已有的字体，或者安装一个新字体并指定使用它，就可以避免这个问题，例如，安装文泉驿正黑字体：</p>
<pre class="brush:shell;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
yum install wqy-zenhei-fonts.noarch
</pre>
<p>然后在绘图代码中：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
set term png font &quot;/usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc&quot; 10
</pre>
<p>表示使用 /usr/share/fonts/wqy-zenhei/wqy-zenhei.ttc 这个刚安装上的字体文件作为图像中的字体，并且字号为10。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="color:#0000ff;">（2）</span>shell调用gnuplot时，shell的内置变量（$1，$2，&hellip;）与gnuplot的内置变量冲突的问题<br />
在shell脚本中，$1，$2，&hellip; 表示传给shell脚本的参数，这与在gnuplot的交互模式下，gnuplot定义的含义是不同的。<br />
在gnuplot的交互模式下，$1，$2，&hellip; 之类的变量是指输入数据文件的第1列，第2列，&hellip;<br />
所以现在问题来了，当用shell脚本调用gnuplot时（就像本文上面给出的代码一样），如何能让gnuplot代码的$1，$2仍然取到的是数据文件的第1列，第2列？<br />
<a href="http://stackoverflow.com/questions/4179591/how-to-manipulate-column-values-when-using-gnuplot-from-a-shell-script" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">这篇</span></a>文章给出了解答，这里把几种方法列举如下：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
&#39;$1&#39;
&quot;\$1&quot;
\$1
</pre>
<p>
<span style="color:#0000ff;">（3）</span>如何在画出的曲线图中，每N个坐标点才标一个Y坐标值，而不是每个坐标点都标？<br />
某些数据的点非常密集，如果我们把每个点旁边都标上它的Y坐标值，会导致图上的文字密密麻麻地挤在一起，从而使得我们根本看不清，因此，在这种情况下，我们只需要每N个点才标一个Y坐标值就可以了。<br />
这个解决办法需要有点技巧。<br />
假设某个数据文件 data.txt 的第1列是点的顺序编号（1，2，3，&hellip;&hellip;），第2列是该点的纵坐标值，假设最后绘制图像的那一句是：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
plot &quot;data.txt&quot; using 1:2 with linespoints linecolor 3 linewidth 1 pointtype 5 pointsize 1, &quot;&quot; using 1:2:2 with labels center offset 0,0.5
</pre>
<p>现在要对这一句进行特殊处理，使得它做到&ldquo;每5个点绘制一个Y坐标值&rdquo;，那么我们只需要把 <span style="color:#ff0000;">using 1:2:2</span> 这里改成：</p>
<pre class="brush:plain;first-line:1;pad-line-numbers:true;highlight:null;collapse:false;">
using 1:2:(int(\$1) % 5 == 0 ? sprintf(&quot;%.3f&quot;, \$2) : &quot;&quot;)
</pre>
<p>解释：<br />
本来<span style="color:#ff0000;">1:2:</span><span style="color:#0000ff;">2</span>的最后一个 <span style="color:#0000ff;">2</span> 表示将数据文件data.txt的第2列作为label的值（在这里也就是Y坐标值），但是我们把它改成了一个条件判断语句，gnuplot是支持的，很强大。<br />
这个条件判断语句的含义是：当data.txt数据文件的第1列的值是5的整数倍的时候，我们打印出第2列的实际数值（以小数点后3位小数的格式），否则我们就打印出一个空字符串（这实际上导致了什么也不会打印出来）。<br />
大家注意了，这里的为了取数据文件的第1列、第2列，用了 <span style="color:#ff0000;">\$1</span> 以及 <span style="color:#ff0000;">\$2</span> 的形式，我在前面第（2）条里说过了，这是由于shell脚本内置的参数变量$1，$2与gnuplot有冲突，所以要转义。<br />
另外，为什么会在 <span style="color:#ff0000;">\$1</span>&nbsp;外面包一个 <span style="color:#0000ff;">int()</span> 呢？如果没有的话，gnuplot会打印出类似于&ldquo;can only mod ints&rdquo;的一个错误提示。<br />
这种解决方案比较微妙，但是它确实达到了我想要的效果。<br />
那有人会说，如果data.txt的第一列不是点的顺序编号呢？在这种情况下，可以用程序自动地为data.txt加上一列（第1列），使其成为顺序编号，然后用第2、第3列来绘图就可以了。</p>
<p style="font-size: 16px; margin: 5px 0px; clear: both; font-family: sans-serif;">
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
	<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
	转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
	感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>

			<!--[syntaxhighlighter]-->
			<!--代码高亮，请勿编辑-->
			<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shCore.js"></script><script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushPlain.js"></script>
<script type="text/javascript" src="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/scripts/shBrushShell.js"></script>

			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shCoreCk.css" />
			<link type="text/css" rel="stylesheet" href="https://www.codelast.com/wp-content/plugins/ck-and-syntaxhighlighter/syntaxhighlighter/styles/shThemeCk.css" />
			<script type="text/javascript">
			SyntaxHighlighter.defaults['class-name']	= '';
			SyntaxHighlighter.defaults['smart-tabs']	= true;
			SyntaxHighlighter.defaults['tab-size']		= 2;
			SyntaxHighlighter.defaults['gutter']		= true;
			SyntaxHighlighter.defaults['quick-code']	= true;
			SyntaxHighlighter.defaults['collapse'] 		= false;
			SyntaxHighlighter.defaults['auto-links']	= true;
			SyntaxHighlighter.defaults['toolbar']		= true;
			SyntaxHighlighter.all();
			</script>
			<!--[/syntaxhighlighter]-->]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-gnuplot%e8%b0%83%e6%95%99%e8%ae%b0/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
