[英]Gnuplot and C - plot different symbols/colors
I would like to plot different points with different colors and symbols.我想 plot 不同点用不同的 colors 和符号。 Here is my Code in "C":
这是我在“C”中的代码:
float x[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
float y[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
FILE *gnuplot = popen("gnuplot", "w");
fprintf(gnuplot, "plot '-' pt 7 pointsize 2 lc \"blue\" notitle \n");
for (int i = 0; i < 6; ++i) {
fprintf(gnuplot,"%f %f\n", x[i], y[i]);
}
fprintf(gnuplot, "plot '-' pt 5 pointsize 2 lc \"red\" notitle \n");
for (int i = 6; i < 12; ++i) {
fprintf(gnuplot,"%f %f\n", x[i], y[i]);
}
fprintf(gnuplot, "e\n");
fflush(gnuplot);
getch();
fclose(gnuplot);
Unfortunately, all points have the same type and same color.不幸的是,所有的点都具有相同的类型和相同的颜色。 What is my mistake?
我的错误是什么? Thanks in advance!
提前致谢!
Edit:编辑:
All points are blue and have the pointtype 7 and the pointsize 2. The second command line to change the style has no effect:所有点都是蓝色的,点类型为 7,点大小为 2。更改样式的第二个命令行无效:
fprintf(gnuplot, "plot '-' pt 5 pointsize 2 lc \"red\" notitle \n");
Your code is like the following:您的代码如下所示:
plot '-' pt 7 ps 2 lc "blue" notitle
0 0
1 1
2 2
3 3
4 4
5 5
plot '-' pt 6 ps 2 lc "red" notitle
6 6
7 7
8 8
9 9
10 10
11 11
e
For inline data, ie special filename '-'
the data is ended by a line e
.对于内联数据,即特殊文件名
'-'
,数据以行e
结束。 Check help special-filenames
.检查
help special-filenames
。
So, your code will:因此,您的代码将:
plot '-' pt 6 ps 2 lc "red" notitle
will be interpreted as data line because inline data has not yet been ended by a line e
, hence, this line will be ignored because it does not contain numerical dataplot '-' pt 6 ps 2 lc "red" notitle
将被解释为数据行,因为行内数据尚未以行e
结束,因此,该行将被忽略,因为它不包含数字数据But it seems you want to have the following:但是您似乎想要以下内容:
Code:代码:
plot '-' pt 7 ps 2 lc "blue" notitle, \
'-' pt 6 ps 2 lc "red" notitle
0 0
1 1
2 2
3 3
4 4
5 5
e
6 6
7 7
8 8
9 9
10 10
11 11
e
Result:结果:
Addition : (as mentioned in the comments)添加:(如评论中所述)
Since gnuplot 5.0 you have datablocks which are including the data as well into the plotting code, check help inline
.从 gnuplot 5.0 开始,您有数据块,其中包括数据以及绘图代码,请查看
help inline
。 The advantage is that if you want to make plots of several columns you don't have to provide the data multiple times or split it into sequential 2 column data.优点是,如果您想绘制多列图,则不必多次提供数据或将其拆分为连续的 2 列数据。 The following examples will all result in the same plot:
以下示例都将产生相同的 plot:
Code:代码:
$Data1 <<EOD
0 0
1 1
2 2
3 3
4 4
5 5
EOD
$Data1 <<EOD
6 6
7 7
8 8
9 9
10 10
11 11
EOD
plot $Data1 u 1:2 w p pt 7, \
$Data2 u 1:2 w p pt 5
$Data <<EOD
0 0 6 6
1 1 7 7
2 2 8 8
3 3 9 9
4 4 10 10
5 5 11 11
EOD
plot $Data u 1:2 w p pt 7, \
'' u 3:4 w p pt 5
plot '-' u 1:2 w p pt 7, \
'-' u 1:2 w p pt 5,
0 0
1 1
2 2
3 3
4 4
5 5
e
6 6
7 7
8 8
9 9
10 10
11 11
e
plot '-' u 1:2 w p pt 7, \
'-' u 3:4 w p pt 5,
0 0 6 6
1 1 7 7
2 2 8 8
3 3 9 9
4 4 10 10
5 5 11 11
e
0 0 6 6
1 1 7 7
2 2 8 8
3 3 9 9
4 4 10 10
5 5 11 11
e
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.