sin函数图像,x取值范围-3.14~3.14,默认有图例

gnuplot> plot [-3.14:3.14] sin(x)

  • 如果你不需要上面的图例,你可以在运行
    gnuplot> unset key

  • 如果要还原
    gnuplot> set key default

一般情况下使用gnuplot都是科学绘图,因此很多都是放在文章里面。一般优秀的科技文献都是用latex来编写的,所以gnuplot提供了直接输出 tex文件的功能,只需要把output设置为latex就可以了。

gnuplot> set terminal latex
set output “sin.tex”
plot [-3.14:3.14] sin(x)

你可以把这个文件直接插入你的文章中,例如
\begin{figure}
\begin{center}
\input{sin.tex}
\end{center}
\end{figure}
以后凡是要生成插入latex的图片,就可以用上面的办法实现。

1
2
3
4
5
6
7
8
gnuplot> set terminal latex
set output "sinagain.tex"
set size 5/5.,4/3.
set format xy "$%g$"
set title "This is a plot of $y=\\sin(x)$"
set xlabel "This is the $x$ axis"
set ylabel "This is\\the\\$y$ axis"
plot [0:6.28] [0:1] sin(x)

set size 设置图片的大小
set format xy 设置x、y轴文字的格式
set title 设置图片标题
set xlabel 设置x轴的文字
一般情况下,科技论文的图片很多都含有好几个图线,那么这里就介绍下如何在一张图里显示多张图片。
先看代码:
gnuplot> set terminal latex
set output “combine.tex”
set format xy “$%g$”
set title “Combination”
set xlabel “$x$ axis”
set ylabel “$y$ axis”
plot [-3.14:3.14] 0.2*x with points, sin(x) with lines
这里没有很特别的地方,只是plot多了一个项目,当要绘制多个图线的时候需要用逗号来分隔,注意这里不是中文的逗号,而是西文的逗号。另外with后面的参数是说用什么线性来绘制。出来的效果看下面的图:
接下来再对图片进行加工。一般图片x轴每个计数单位都是希望能够控制的,而不是系统自己来决定的,那么可以通过以下的代码来实现。
看代码:
gnuplot> set terminal latex
set output “tic.tex”
set format y “$%g$”
set format x “$%.2f$”
set title “This is $\sin(x)$”
set xlabel “This is the $x$ axis”
set ylabel “$\sin(x)$”
set xtics -pi, pi/4
plot [-pi:pi] sin(x)

加了一个set xtics这个是设置x的间距,也就是从-pi开始、每隔pi/4出现一次,pi是gnuplot内建的一个变量,所以只要输入pi,系统知道其含义。

使用 gnuplot 在网页中显示数据
set terminal png truecolor #为了能够生成图像,必须告诉 gnuplot 图形文件应该采用什么格式以及应该如何显示。

set output “sarimage.png”
set autoscale

在生成图形时,需要指定数据的 x 和 y 轴范围。这个示例让 gnuplot 自己计算范围值。

set xdata time
set timefmt “%H:%M:%S”

因为这个示例使用日期值作为数据的参照点,需要告诉 gnuplot 日期数据的格式。
%d - day of month 1 -31
%m - month of year 1 -12
%y - year 0-99
%b - three character of month name , ie: jan ,feb
%B - name of month

set style data lines

在显示这个图形时,绘制出的数据应该是一条平滑的数据线。常用的其他绘制格式包括:dots、boxes、errorbars、 candlesticks。

plot “sarx1.txt” using 1:2 title “%user”, ‘’ using 1:3 title “%sys”

接下来,使用 plot 命令实际绘制或生成图形。首先,指定数据输入文件名,然后告诉 gnuplot 要绘制哪些列。在这个示例中,使用第 1 列作为 x 轴,绘制第 2 列数据,标题为 “%user”;然后绘制第 3 列,标题为 “%sys”。标题(即标签)显示在图形的右上角。在绘制时,第 2 列和第 3 列使用第 1 列作为 x 值。plot 命令中的每个 ‘using’ 语句由逗号分隔。

.plt .gnu .conf

在 plot 命令中可以使用缩写。例如,在最初的 plot 命令后面,其他 plot 命令选项都可以缩写,用选项的第一个字母表示。但是,对于本文,我只用缩写表示输入文件,即使用两个单引号表示输入文件 (sarx1.txt)。下面详细解释一下。第一个示例是本文中使用的表示法,第二个示例是缩写表示法,第三个示例是不使用任何缩写的完整命令语句。这三 个示例产生相同的输出。

plot “sarx1.txt” using 1:2 title “%user”, ‘’ using 1:3 title “%sys”

plot “sarx1.txt” using 1:2 title “%user”, ‘’ u 1:3 t “%sys”

plot “sarx1.txt” using 1:2 title “%user”,\
″sarx1.txt’’ using 1:3 title “%sys”

在向其他用户显示图形时,有时候应该包含明确的标签和标题,以便用户了解数据的意义。为了包含 x 和 y 标签,应该使用 xlabel 和 ylabel 命令并把标签文本放在引号中:

set ylabel “ y line info here”
set xlabel “ x line info here”

使用 title 命令添加图形的标题:

set title “main title info here”

在生成图形时,gnuplot 使用自己的默认颜色。在默认情况下,在白色背景上生成图形;这对于可能要打印的图形是有意义的。但是,可以使用颜色编码指定任何颜色,颜色编码以字母 x 开头,用十六进制表示。十六进制编码的格式为:

xrrggbb

在 Google 上搜索 ‘hex color codes’ 可以找到十六进制的颜色编码表。

覆盖 gnuplot 默认颜色的次序为:

background
border
X
Y
plotting lines

浅灰色的十六进制表示为:C9C9C9。

可以使用以下命令生成浅灰色背景的图形:

set terminal png xC9C9C9

在查看图形时,使用网格作为参照点也非常有用。下面的命令使用 grid 选项让 gnuplot 在图形上加上网格:

set grid

可以根据这些信息指定自己的范围。下面的示例使用的 x 范围从 14:00 到 18:15,y 范围从 10 到 50。

set xrange [“14:00:00” : “18:15:00”]
set yrange [“10:00” : “50:00” ]

set terminal png truecolor
set output “sarimage.png”
set autoscale
set style data lines
set xrange [0: 10]
set yrange [0.0:2000]
plot “dblp_all_6k_514.txt” using 1:6 title “%user”, ‘’ using 1:3 title “%sys”

常用的其他绘制格式包括:dots、boxes、errorbars、candlesticks、lines、histograms。