使用c++源代码在gnuplot中设置参数
Set parameters in gnuplot using C++ source code
我试图用gnuplot从c++源代码绘制一些矢量。我已经读过一个关于如何将c++与gnuplot连接起来的问题。
这是源代码:
int main()
{
char * commandsForGnuplot[] = {"set title "Trajectory_ARM"","plot 'data.temp' with linespoints"};
double xvals[NUM_POINTS] = {1.0, 2.0, 3.0, 4.0, 5.0};
double yvals[NUM_POINTS] = {5.0 ,3.0, 1.0, 3.0, 5.0};
FILE * temp = fopen("data.temp", "w");
/*Opens an interface that one can use to send commands as if they were typing into the
* gnuplot command line. "The -persistent" keeps the plot open even after your
* C program terminates.
*/
FILE * gnuplotPipe = popen ("gnuplot -persistent", "w");
int i;
for (i=0; i < NUM_POINTS; i++)
{
fprintf(temp, "%lf %lf n", xvals[i] , yvals[i]); //Write the data to a temporary file
}
for (i=0; i < NUM_COMMANDS; i++)
{
fprintf(gnuplotPipe, "%s n", commandsForGnuplot[i]); //Send commands to gnuplot one by one.
}
return 0;
}
问题是:
我有一个新的5点向量(a)我想在同一个图中有a和关于yval的xval。我应该如何修改我的代码?在本例中,我想用两种不同的颜色绘制A和xval
这是用c格式化的完整源代码。我刚刚添加了第三个向量A
,以及commandsForGnuplot
('' u 1:3 w lp
)的第三个指令,该指令将此向量A
绘制为相对于xvals
。注意,gnuplot默认情况下用不同的颜色绘制每条线。
#include <stdio.h>
#include <stdlib.h>
#define NUM_POINTS 5 // each vector has 5 points
int main()
{
double xvals[NUM_POINTS] = {1.0, 2.0, 3.0, 4.0, 5.0};
double yvals[NUM_POINTS] = {5.0 ,3.0, 1.0, 3.0, 5.0};
double Avals[NUM_POINTS] = {4.0 ,5.0, 3.0, 0.0, 1.0};
int i;
//Write xvals, yvals, and Avals to a temporary file
FILE * temp = fopen("data.temp", "w");
for(i=0; i < NUM_POINTS; i++)
fprintf(temp, "%lf %lf %lfn", xvals[i] , yvals[i], Avals[i]);
fclose(temp); // close file if it won't be used anymore
// open interface to gnuplot. Option -p lets plot windows survive after program exits
FILE * gnuplotPipe = popen ("gnuplot -p", "w");
// send commands to gnuplot one by one.
fprintf(gnuplotPipe, "%s n", "set title 'Trajectory\_ARM'"); // command 0: define the graph title
fprintf(gnuplotPipe, "%s n", "plot 'data.temp' u 1:2 w lp"); // command 1: plot yvals vs xvals, and Avals vs xvals
fflush(gnuplotPipe); // plots the data with the current commands
// we obtained a plot of data.temp
// now, we place an instruction to pause the program until a key is typed
getchar();
// add a plot to the current graph
fprintf(gnuplotPipe, "%s n", "replot 'data.temp' u 1:3 w lp");
pclose(gnuplotPipe); // close gnuplot if it won't be used anymore
return 0;
}
我想你可以使用multiplot命令
set multiplot
plot data1.temp
plot data2.temp
unset multiplot
相关文章:
- C++ 命名参数习惯用语 - 未设置字符串属性
- 如何在不传递命令行参数的情况下在 c++ 中设置环境变量
- 在 c++11 中为 pthread 设置调度参数
- 将矢量设置为常规参数
- 如何设置默认参数以防用户不输入另一个参数
- 如何在Visual Studio 2017上将类方法设置为参数并将它们与lambda一起使用?
- AI 忽略 Connect4 中的设置参数
- 了解sigwait()及其如何设置参数
- 如何使用 QProcess 设置参数以运行类文件?
- 具有构造函数的新对象数组,需要在C++中设置参数
- 使用OPENCV的GSTREAMER如何为ICECAST2设置参数
- 为 QtWebEngineProcess 设置参数
- 在功能中设置参数
- SimpleBlobDetection opencv C++设置参数出现符号问题
- 谷歌模拟:在EXPECT_CALL中设置参数
- 编译时配置文件解析和设置参数
- 使用c++源代码在gnuplot中设置参数
- 为通过引用设置参数的函数命名的样式
- Gmock设置参数
- -设置参数X大小的默认initializer_list参数Y,其中X和Y是函数参数