C++ 调用Gnuplot实现图形绘制的过程

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)

 

  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
gnuplot-c++是一个用于在C++调用gnuplot的库。根据引用和引用中的代码片段,我们可以看到gnuplot-c++库的初始化代码。在初始化函数中,它首先设置gnuplot可执行文件的路径为"pgnuplot.exe",然后检查是否可以找到gnuplot可执行文件。如果找不到,它将抛出一个异常。接下来,它通过_popen函数打开与gnuplot的连接,并将连接保存在gnucmd变量中。最后,它将nplots和valid变量初始化为0和true。 关于gnuplot-c++库的更多信息,你可以从引用中的链接下载gnuplot-c++库的压缩包。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [C++ 结合 gnuplot 实现数据可视化](https://blog.csdn.net/yangyangyang20092010/article/details/17249731)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [C++ 调用Gnuplot实现图形绘制过程](https://blog.csdn.net/weijimin1/article/details/109185049)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值