gnuplot:如何从文件中绘制一组三角形?

chenzhongpu

在 gnuplot 中,我们可以set object polygon在给定坐标的情况下绘制一个多边形,包括三角形。

但是如何绘制一组三角形,其坐标存储在文件中,每行的格式为<x1> <y1> <x2> <y2> <x3> <y3>

至于矩形/圆,这个任务可以使用plotand with boxxy/ with circlesoptions来完成,但是with trianglesgnuplot中没有选项。

一种可能的解决方案是with vectors通过绘制每条边来使用,但是有点复杂,而且这种方法不支持颜色填充。

伊森

我想不出一步就能做到这一点的方法。数据格式与 gnuplot 的任何绘图样式都不匹配。

一种方法是通过临时文件转换数据。这是一个适用于 5.2 及更高版本的示例。如果您使用的是较新的 gnuplot,则可以替换with polygonswith filledcurves closed.

$DATA << EOD
1 1 2 2 3 1
11 11 14 14 17 11
21 21 22 22 23 21
15 5 16 6 17 5
6 6 7 7 8 6
EOD

set table "temp.dat"
plot $DATA using (sprintf("%g %g\n %g %g\n %g %g\n \n",$1,$2,$3,$4,$5,$6)) with table
unset table

unset key
set style fill solid noborder
plot "temp.dat" using 1:2 with filledcurves closed fillcolor "forest-green"

在此处输入图像描述

注意:我最初打算展示使用临时数据块而不是中间临时文件,但这不起作用,因为格式化的输出with table不会将换行符\n转换为空的数据块行。

编辑(显示可变颜色)

包含 RGB 颜色的额外数据字段必须出现在重新格式化数据的每个输入行中,但仅使用每个多边形的第一个顶点的值。此示例中的 sprintf 格式已被修改,以相应地从原始数据文件再现颜色(NB:十六进制整数值),其余多边形顶点中的虚拟值为零。

$DATA << EOD
1 1 2 2 3 1             0x00ffff
11 11 14 14 17 11       0x191970
21 21 22 22 23 21       0x2e8b57
15 5 16 6 17 5          0xffc020
6 6 7 7 8 6             0x8b000
EOD

set table "temp.dat"
plot $DATA using (sprintf("%g %g 0x%x\n %g %g 0\n %g %g 0\n \n",$1,$2,int($7),$3,$4,$5,$6)) with table
unset table

unset key
set style fill solid noborder
plot "temp.dat" using 1:2:3 with filledcurves closed fillcolor rgb variable

在此处输入图像描述

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章