gnuplot的安装及使用gnuplot和如何将c语言和java语言简单数据通过gnuplot作出对应波形

简介:
gnuplot及使用手册:网盘链接:链接:https://pan.baidu.com/s/1XT-aMoPPWZxhhaaAa4WlxA
提取码:6sf3
复制这段内容后打开百度网盘手机App,操作更方便哦

Gnuplot是一个命令行的交互式绘图工具(command-driven interactive function plotting program)。用户通过输入命令,可以逐步设置或修改绘图环境,并以图形描述数据或函数,使我们可以借由图形做更进一步的分析。gnuplot是由Colin Kelly和Thomas Williams于1986年开始开发的科学绘图工具,支持二维和三维图形。它的功能是把数据资料和数学函数转换为容易观察的平面或立体的图形,它有两种工作方式,交互式方式和批处理方式,它可以让使用者很容易地读入外部的数据结果,在屏幕上显示图形,并且可以选择和修改图形的画法,明显地表现出数据的特性。

gnuplot安装及环境变量的配置:
下载好后,直接点击安装就行,一定要记住安装目录,等下配置环境变量时使用。
安装好后进行环境变量配置:先将刚才安装的路径下的gnuplot/bin路径复制一下,如下图:
在这里插入图片描述
然后右键此电脑—点击属性—高级系统设置—环境变量配置—系统变量中的path—点击新建—将刚才复制的路径贴到新建变量—然后依次点击确认–确认—确认,切记三个确认都得点击。如图配置:
在这里插入图片描述
然后检查是否配置成功:然后win+R,输入cmd进入window命令窗口,输入gnuplot,按enter,出现如下界面则配置成功:
在这里插入图片描述
现在我们来将c语言简单数据通过gnuplot作出对应波形
第一步:在notepad++编辑器中生成c语言数据:

#include<stdio.h>
#include<math.h>
#define pi 3.14
int main()
{
    double a,b,c,d;
    int i;     
    for (i=-360; i<360; i++)
        {
            a=pi/180;
            b=sin(i*a);
            c=cos(i*a);
            d=tan(i*a);
          
          printf("%.16f %.16f %.16f %.16f\n", (float)i ,b,c,d);
        }
}

编译执行代码:
然后利用tcc进行编译,执行输出结果:

Microsoft Windows [版本 10.0.17134.1006]
(c) 2018 Microsoft Corporation。保留所有权利。

D:\tcc\tcc>tcc dada.c

D:\tcc\tcc>dada.exe
-360.0000000000000000 0.0031853017931371 0.9999949269133752 0.0031853179525310
-359.0000000000000000 0.0206282883640135 0.9997872142206916 0.0206326787046309
-358.0000000000000000 0.0380649977276571 0.9992752653538430 0.0380926047580876
-357.0000000000000000 0.0554901238772792 0.9984592360993433 0.0555757529912399
-356.0000000000000000 0.0728983643308739 0.9973393747756494 0.0730928369766533
-355.0000000000000000 0.0902844217447654 0.9959160221575980 0.0906546533403178
-354.0000000000000000 0.1076430055256001 0.9941896113727078 0.1082721085537940
-353.0000000000000000 0.1249688334402765 0.9921606677693772 0.1259562463015566
-352.0000000000000000 0.1422566332233306 0.9898298087570220 0.1437182755709987
-351.0000000000000000 0.1595011441812969 0.9871977436181958 0.1615695996191265
-350.0000000000000000 0.1766971187935393 0.9842652732927550 0.1795218459779830
-349.0000000000000000 0.1938393243090740 0.9810332901341329 0.1975868976704869
-348.0000000000000000 0.2109225443389056 0.9775027776377939 0.2157769258197047
-347.0000000000000000 0.2279415804433767 0.9736748101419568 0.2341044238477772
-346.0000000000000000 0.2448912537140550 0.9695505525006720 0.2525822434760412
-345.0000000000000000 0.2617664063496855 0.9651312597293548 0.2712236327554978
-344.0000000000000000 0.2785619032257113 0.9604182766228835 0.2900422763769322
-343.0000000000000000 0.2952726334568941 0.9554130373463776 0.3090523385330833
-342.0000000000000000 0.3118935119525668 0.9501170649987790 0.3282685086315839
-341.0000000000000000 0.3284194809640285 0.9445319711493720 0.3477060501873588
-340.0000000000000000 0.3448455116236195 0.9386594553473822 0.3673808532573700
-339.0000000000000000 0.3611666054750157 0.9325013046048002 0.3873094908195118

第二步:用gnuplot画出数据波形
第一种方法通过重定向,将dada.exe生成的数据存入一个数据文件中。然后利用gnuplot读取该数据文件作图。输入以下代码,就可以生成dada.dat文件。

D:\tcc\tcc>dada.exe>dada.dat

然后在da.dat所在路径下,从命令窗口执行gnuplot.exe,如下:

D:\tcc\tcc>gnuplot

        G N U P L O T
        Version 5.2 patchlevel 6    last modified 2019-01-01

        Copyright (C) 1986-1993, 1998, 2004, 2007-2018
        Thomas Williams, Colin Kelley and many others

        gnuplot home:     http://www.gnuplot.info
        faq, bugs, etc:   type "help FAQ"
        immediate help:   type "help"  (plot window: hit 'h')

Terminal type is now 'wxt'
Encoding set to 'default'.
gnuplot>

在gnuplot命令行提示符gnuplot>下输入作图指令(不要输gnuplot>):

gnuplot> plot [-360:360] [-2:2] "dada.dat" u 1:2 w l title "sin","<dada.exe" u 1:3 w l title "cos","<dada.exe" u 1:4 w l title "tan"


在这里插入图片描述
其中,[0:(1/50.0)]定义了横坐标的范围,[-2:2]是纵坐标的显示范围,u 1:2
表示使用数据文件的1,2列数据,w L表示使用折线作图。

方法二:不必生成数据文件,直接在gnuplot中以管道形式执行ta.exe并产生数据作图。只需将作图语句中的"ta.dat"写为"<ta.exe"即可。

gnuplot> plot [-360:360] [-2:2] "<dada.exe" u 1:2 w l title "sin","<dada.exe" u 1:3 w l title "cos","<dada.exe" u 1:4 w l title "tan"

在这里插入图片描述
现在我们来将java语言简单数据通过gnuplot作出对应波形
第一步:在editplus3编辑器中生成java语言数据:

 class Hjw22{  
    public static void main(String args[]){
     long startTime=System.currentTimeMillis();
		double a;
		double pi=3.14;
		for(int i=-360;i<361;i++){ 
			double f=i;
			a=(pi/180);
			double b= Math.cos(a*i); 
			double c= Math.sin(a*i); 
            double d= Math.tan(a*i);
			System.out.format("%.4f %.8f %.8f %.8f%n", f, c,b, d);
		}  
    long endTime=System.currentTimeMillis();
		   System.out.println("程序运行时间: "+(endTime - startTime)+"ms");
    }
}

编译执行代码,执行输出结果:

D:\java123\sanjiaohans>javac jj.java

D:\java123\sanjiaohans>Hjw22
'Hjw22' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

D:\java123\sanjiaohans>java hjw
错误: 找不到或无法加载主类 hjw

D:\java123\sanjiaohans>java Hjw22
-360.0 0.0000 1.0000 0.0000
-359.0 0.0175 0.9998 0.0175
-358.0 0.0349 0.9994 0.0349
-357.0 0.0523 0.9986 0.0524
-356.0 0.0698 0.9976 0.0699
-355.0 0.0872 0.9962 0.0875
-354.0 0.1045 0.9945 0.1051
-353.0 0.1219 0.9925 0.1228
-352.0 0.1392 0.9903 0.1405
-351.0 0.1564 0.9877 0.1584
-350.0 0.1736 0.9848 0.1763
-349.0 0.1908 0.9816 0.1944
-348.0 0.2079 0.9781 0.2126
-347.0 0.2250 0.9744 0.2309
-346.0 0.2419 0.9703 0.2493
-345.0 0.2588 0.9659 0.2679
-344.0 0.2756 0.9613 0.2867

第二步:用gnuplot画出数据波形
然后在jj.java所在路径下,从命令窗口执行gnuplot,如下:

Microsoft Windows [版本 10.0.17134.1006]
(c) 2018 Microsoft Corporation。保留所有权利。

D:\java123\sanjiaohans>javac jj.java

D:\java123\sanjiaohans>gnuplot

        G N U P L O T
        Version 5.2 patchlevel 6    last modified 2019-01-01

        Copyright (C) 1986-1993, 1998, 2004, 2007-2018
        Thomas Williams, Colin Kelley and many others

        gnuplot home:     http://www.gnuplot.info
        faq, bugs, etc:   type "help FAQ"
        immediate help:   type "help"  (plot window: hit 'h')

Terminal type is now 'wxt'
Encoding set to 'default'.
gnuplot>

在gnuplot命令行提示符gnuplot>下输入作图指令(不要输gnuplot>):

gnuplot> plot [-360:360] [-2:2] "<java Hjw22" u 1:2 w l title "sin","<java Hjw22" u 1:3 w l title "cos","<java Hjw22" u 1:4 w l title "tan"

在这里插入图片描述
总结:
安装gnuplot需要注意变量环境配置
还有一些gnuplot的代码在使用手册里面,可以打开自己学习。
参考博客:参考博客链接
参考博客链接

有疑问可以留言

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
你可以使用C语言编写一个函数来获取时间和温度数据,然后使用gnuplot库来绘制图表。 首先,你可以使用time.h头文件中的函数来获取当前时间。以下是一个示例函数,用于获取时间和温度数据: ```c #include <stdio.h> #include <time.h> float get_temperature() { // 这里假设你有一个获取温度的函数,并返回温度值 float temperature = 0.0; // 在这里获取温度值 // ... return temperature; } void get_time_and_temperature(float* temperature, char* time_str) { time_t current_time; struct tm* time_info; // 获取当前时间 time(&current_time); time_info = localtime(&current_time); // 将时间格式化为字符串 strftime(time_str, 50, "%Y-%m-%d %H:%M:%S", time_info); // 获取温度值 *temperature = get_temperature(); } int main() { float temperature; char time_str[50]; // 获取时间和温度数据 get_time_and_temperature(&temperature, time_str); // 打印数据 printf("Time: %s\n", time_str); printf("Temperature: %.2f\n", temperature); return 0; } ``` 上述代码中的`get_temperature()`函数需要根据你的实际情况实现,用于获取温度数据。 接下来,你可以使用gnuplot库来绘制图表。你需要安装gnuplot,并在代码中包含对应的头文件`gnuplot_i.h`。以下是一个使用gnuplot绘制温度数据的示例: ```c #include <stdio.h> #include <time.h> #include "gnuplot_i.h" void plot_temperature(float* temperature, int size) { gnuplot_ctrl* handle; int i; // 初始化gnuplot handle = gnuplot_init(); // 设置x轴为时间 gnuplot_set_xlabel(handle, "Time"); // 设置y轴为温度 gnuplot_set_ylabel(handle, "Temperature"); // 绘制温度曲线 gnuplot_plot_x(handle, temperature, size, "Temperature"); // 暂停 printf("Press enter to exit..."); getchar(); // 关闭gnuplot gnuplot_close(handle); } int main() { float temperature[] = {25.3, 24.8, 25.1, 25.6, 26.2}; // 示例温度数据 int size = sizeof(temperature) / sizeof(float); // 绘制温度曲线 plot_temperature(temperature, size); return 0; } ``` 在上述示例中,`temperature`数组包含了一些示例温度数据,你可以根据实际情况进行修改。运行程序后,它将使用gnuplot绘制温度曲线,并在绘图窗口中显示出来。 注意,你需要在编译时链接gnuplot库。如果你使用gcc编译器,可以使用以下命令进行编译: ``` gcc your_code.c -o your_executable -lgnuplot ``` 请确保已正确安装gnuplot,并将代码中的`your_code.c`和`your_executable`替换为你的实际文件名。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值