如何使用2个csv文件使用GNUPlot绘制填充曲线图 [英] How to draw a filledcurve graph with GNUPlot by using 2 csv files
问题描述
如果我有2个csv文件("CSV1.csv" dataname_1
和"CSV2.csv" dataname_2
),如何从2个csv文件的数据绘制填充曲线图.这些CSV文件的格式相同,其中2是时间戳记,5是值,因此using 2:5
If I have 2 csv files ("CSV1.csv" dataname_1
and "CSV2.csv" dataname_2
), how can I draw filled curve graph from the data of 2 csv files. The formats of these CSV files are identical, where 2 is timestamps and 5 is the value thus the using 2:5
我正在尝试:
plot dataname_2 using 2:5 title "Above" with filledcurves above lc rgb 'blue',\
dataname_1 using 2:5 title "Below" with filledcurves below lc rgb 'red',\
dataname_2 using 2:5 title "Engine Starts" with lines lc rgb "#1E90FF",\
dataname_1 using 2:5 title "Engine Hours" with lines lc rgb "#FF1493"
我需要修改上面的代码,以便输出为:
I need to modify the code above so that the output is:
推荐答案
一种可能一直有效的解决方案是使用gnuplot可以处理和绘制数据的方式使用任何外部工具准备数据.我知道gnuplot的理念是专注于绘图,而不必专注于绘图的数据准备.但是,最好具有最少的功能集来进行一些基本的数据准备.
A solution which will probably always work is to prepare the data with whatever external tool in such a way that gnuplot can handle and plot it. I'm aware that the philosophy of gnuplot is to concentrate on plotting and not necessarily on data preparation for plotting. However, it is always good to have a minimum set of features to do some basic data preparation.
在您的情况下,您有几个问题,好吧,我们称它为挑战;-)
In your case you have several problems, well, let's call it challenges ;-)
-
with filledcurves
需要同一文件或数据块中的数据 - 但是,gnuplot无法轻松地逐行合并数据文件(可以采用一些解决方法,请参见: https://stackoverflow.com/a/61559658/7295599 ). gnuplot只需附加即可.
- 后者对您没有帮助,因为
with filledcurves
的上下曲线需要相同的x
,即x,y1,y2
,并且您的数据具有x1,y1
和x2,y2
- 但是,gnuplot无法轻松地对数据重新采样(可以通过一些变通方法,请参见:使用gnuplot重新采样数据)
-
with filledcurves
无法直接使用非单调递增x填充曲线(数据情况并非如此.此处仅出于说明目的)(可以通过一些变通方法参见:https://stackoverflow.com/a/53769446/7295599 或 https://stackoverflow.com/a/56176717/7295599 )
with filledcurves
requires data within the same file or datablock- however, gnuplot cannot easily merge datafiles line by line (it can with some workaround, see: https://stackoverflow.com/a/61559658/7295599). Simply appending would be no problem with gnuplot.
- the latter doesn't help you since
with filledcurves
needs identicalx
for upper and lower curve, i.e.x,y1,y2
and your data hasx1,y1
andx2,y2
- however, gnuplot cannot easily resample data (it can with some workaround, see: Resampling data with gnuplot)
with filledcurves
cannot directly fill curves with non-monotonic increasing x (not the case with your data. Here just for illustration purposes) (it can with some workaround see: https://stackoverflow.com/a/53769446/7295599 or https://stackoverflow.com/a/56176717/7295599)
所有这些的解决方法如下(适用于gnuplot 5.2,也许可以进行调整以使用早期版本):
So a workaround for all this could be the following (works with gnuplot 5.2, maybe can be tweaked to work with earlier versions):
假设:
-
两个文件或数据块中的
- 数据
x1,y1
和x2,y2
- 数据不一定具有相同的
x
,即x1,y1
和x2,y2
- 数据可能包含非单调的
x
- 两条曲线只有一个 交点(嗯,下面的解决方法将只取第一个)
- Data
x1,y1
andx2,y2
in two files or datablocks - Data has not necessarily identical
x
, i.e.x1,y1
andx2,y2
- Data may contain non-monotonic
x
- the two curves have only one intersection (well, the workaround below will just take the first one)
过程:
-
(如果还没有的话)将数据放入数据块中.
if not already, get the data into a datablock.
找到曲线的交点.
创建新的数据块:Filled1
从起点到交点使用Data1,从交点到起点向后使用Data2. Filled2
从末尾向交叉点使用Data1,从交点到末端使用Data2.
Create new datablocks: Filled1
using Data1 from the beginning to the intersection point and using Data2 backwards from the intersection point to the beginning. Filled2
using Data1 from the end backwards to the intersection point and using Data2 from the intersection point to the end.
图$Data1
和$Data2
with lines
和$Filled1
和$Filled2
with filledcurves
除非有专门的功能,否则在另一种编程语言中,步骤2和3可能不会很短.
Steps 2 and 3, probably will not be much shorter in another programming language unless there are dedicated functions.
将文件获取到数据块:(另请参见此处 gnuplot:将数据文件1:1加载到数据块中)
Get files to datablock: (see also here gnuplot: load datafile 1:1 into datablock)
# get files to datablocks
set table $Data1
plot 'myFile1.dat' u 1:2 w table
set table $Data2
plot 'myFile2.dat' u 1:2 w table
unset table`
代码 :(复制并粘贴gnuplot> = 5.2)
Code: (copy&paste for gnuplot >=5.2)
### fill intersecting curves from two files not having identical x
reset session
$Data1 <<EOD
1 1
2 0
4 1
3 3
5 5
6 6
8 8
9 9
EOD
$Data2 <<EOD
1 3
3.5 5
7.5 1
9 7
EOD
# orientation of 3 points a,b,c: -1=clockwise, +1=counterclockwise
Orientation(a,b,c) = sgn((word(b,1)-word(a,1))*(word(c,2)-word(a,2)) - \
(word(c,1)-word(a,1))*(word(b,2)-word(a,2)))
# check for intersection of segment a-b with segment c-d,
# 0=no intersection, 1=intersection
IntersectionCheck(a,b,c,d) = \
Orientation(a,c,b) == Orientation(a,d,b) || \
Orientation(c,a,d) == Orientation(c,b,d) ? 0 : 1
# coordinate of intersection
M(a,b) = real(word(a,1)*word(b,2) - word(a,2)*word(b,1))
N(a,b,c,d) = (word(a,1)-word(b,1))*(word(c,2)-word(d,2)) - \
(word(a,2)-word(b,2))*(word(c,1)-word(d,1))
Px(a,b,c,d) = (M(a,b)*(word(c,1)-word(d,1)) - (word(a,1)-word(b,1))*M(c,d))/N(a,b,c,d)
Py(a,b,c,d) = (M(a,b)*(word(c,2)-word(d,2)) - (word(a,2)-word(b,2))*M(c,d))/N(a,b,c,d)
Intersection(a,b,c,d) = sprintf("%g %g", Px(a,b,c,d), Py(a,b,c,d))
stop = 0
do for [i=1:|$Data1|-1] {
a = $Data1[i]
b = $Data1[i+1]
do for [j=1:|$Data2|-1] {
c = $Data2[j]
d = $Data2[j+1]
if (IntersectionCheck(a,b,c,d)) {
i0 = i; j0 = j
stop=1; break }
}
if (stop) { break }
}
# create the datablocks for the outline to be filled
set print $Filled1
do for [k=1:i0] { print $Data1[k] }
print Intersection(a,b,c,d)
do for [k=j0:1:-1] { print $Data2[k] }
set print $Filled2
do for [k=|$Data1|:i0+1:-1] { print $Data1[k] }
print Intersection(a,b,c,d)
do for [k=j0+1:|$Data2|] { print $Data2[k] }
set print
set key top left
plot $Filled1 u 1:2 w filledcurves lc rgb 0x3f48cc, \
$Filled2 u 1:2 w filledcurves lc rgb 0xed1c24, \
$Data1 u 1:2 w lp pt 7 lw 5 lc rgb 0x99d9ea, \
$Data2 u 1:2 w lp pt 7 lw 5 lc rgb 0xff80c0
### end of code
结果:
这篇关于如何使用2个csv文件使用GNUPlot绘制填充曲线图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!