C++ 调用Gnuplot实现图形绘制的过程
安装Gnuplot
sudo apt-get install gnuplot
sudo apt-get install gnuplot-x11
测试
输入gnuplot
出现命令行,输入
plot sin(x)
画一条正弦曲线
如图
出现上述图说明gnuplot库安装成功
下载gnuplot-cpp
下载地址
https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/gnuplot-cpp/gnuplot-cpp.zip
解压gnuplot-cpp,在文件夹中有一个案例文件。
在文件夹中目录下打开终端,输入 make即可对该案例进行编译
如果在其它项目需要使用gnuplot将gnuplot_i.hpp,复制到对应项目,在程序中加入
#include “gnuplot_i.hpp”
注:还可以通过使用gnuplot-iostream来实现c++调用
gnuplot-iostream库需要boost库才能使用
案例链接
http://stahlke.org/dan/gnuplot-iostream/
案例
#include "gnuplot_i.hpp"
Gnuplot g1("lines");
std::vector<double> x_t, y_t;
for (int i = 0; i < TrainRadarData.size(); i++)
{
x_t.push_back((double)TrainRadarData[i].position_x_);
y_t.push_back((double)TrainRadarData[i].position_y_);
}
g1.set_grid();
//g1.set_pointsize(10);
g1.set_style("points").plot_xy(x_t, y_t, "radar train data"); //设置形状
g1.set_xlabel("radar x /m"); //设置x轴说明
g1.set_ylabel("radar y /m"); //设置y轴说明
如果要改变点的形状和颜色
g1.set_style("points lc rgb 'red' pt 3 ").plot_xy(x_t, y_t, "radar train data"); //设置点为红色的*
(1)线型(linetype )。在此类型中主要设置线条的颜色,具体对应如下:
n | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
linetype | black | red | green | blue | pink | 浅蓝 | yellow |
(2)点型(pointtype)。此类型用于设置点得形状,可分为14中,具体对应如下:
n | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
pointtype | 无 | + | × | * | □ | ■ | ○ | ● | △ | ▲ | ▽ | ▼ | ◇ | ◆ |
(3)线条宽度(linewidth)、点大小(pointsize)。两者都可以设置为整数或小数。
(4)图样(style):gnuplot 描绘数据数据图形是以读入档案中的坐标值后,以图样绘上。gnuplot可提供9种图样,分别是:
ü lines : 将相邻的点以线条连接。如 plot sin(x) with lines。
ü points : 将每一点以一符号绘上。如 plot sin(x) with points
ü linespoints : 同时具有lines 及 points 的功能。
ü impulses : 将每一点画一垂直线至X 轴。如 plot sin(x) with impulses。
ü dots : 将每一点绘一细点。如plot sin(x) with dots。
ü steps : 以垂直线及水平线各一条来连接两点,形成梯形。如连接 (x1,y1),(x2,y2)两点,以(x1,y1)到(x2,y1)和(x2,y1)到(x2,y2) 两线段连接。如 plot sin(x) with steps。
ü errorbars : 对每一点坐标值(x,y),画一由(x,ylow) 至(x,yhigh) 的线段。并在线段两端做上 tic mark。如plot sin(x) with errorbars。
ü boxes : The boxes style draws a box centredabout the given x coordinate from the yaxis to the given ycoordinate.如plot sin(x) with boxes。
ü boxerrorbars : 组合errorbars 与 boxes 两者功能。如 plot sin(x) with boxerrorbars。
(5) 图例(key)
若想改变显示的图例名称,可通过下述语句实现:
plot 'node1'title“node_1” #将图例显示的名称从“node1”改为“node_1”
也可以通过下述语句直接在画线条时显示:
plot 'node1' title“node_1”withlines
下为一个具体例子的应用程序:
gnuplot> set title“cbr_delay” font “Times-Roman,12” #该图的标头为cbr_delay,字体大小为12号
gnuplot> set xlabel“simulationtime” #该图x轴的标示为simulation time
gnuplot> set ylabel“delaytime” #该图y轴的标示为simulation time
gnuplot> xrange[0:10] #设置x轴的坐标范围为0到10
gnuplot> xtics0,1,10 #设置x轴的坐标在0到10 之间以1为间隔分布
gnuplot> plot 'node1'with linespoints lt 0 lw 2 pt 1 ps 1.5 #绘制node1,线为黑色,线宽为2,
点的形状为“+”,点的大小为1.5
#下一语句实现的功能为在同一个gnuplot图上绘制node2,线为黑色,线宽为2,点的形状为“×”,点的大小为1.5
gnuplot> replot 'node2' with linespoints lt 0 lw2 pt 2 ps1.5
上面设置实现的功能为:绘出两个节点的线形图,该图的标头为cbr_delay,字体大小为12号,x轴和y轴的表示分别为simulation time和delay time。X轴的坐标范围为0到10,之间以1为间隔标示。两个节点都是以linespoints图样绘制,线条颜色均为黑色,节点1的点形状为“+”,节点2的点形状为“×”。
参考链接
1、http://blog.chinaunix.net/uid-28412198-id-3763133.html
2、https://blog.csdn.net/ktigerhero3/article/details/80928859
#从官方下载最新稳定版本
http://www.gnuplot.info/download.html
#解压
tar -xvf gnuplot-5.2.0.tar.gz
#经典三步
./configure --prefix=/opt/gnuplot
make;
make install
#添加环境变量
PATH=/opt/gnuplot/bin:$PATH
export PATH
#执行gnuplot命令
gnuplot
#在gnuplot提示符下执行"plot sin(x)"绘制正弦曲线
gnuplot> plot sin(x)