从两个不同的文件绘制带有开始和结束的箭头 [英] Plotting arrows with start and end from two different files

查看:42
本文介绍了从两个不同的文件绘制带有开始和结束的箭头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个不同的.txt文件,它们的x和y坐标均相等.

I have two different .txt files with x and y coordinates of equal number of samples in both.

File 1
x y
1 2
5 4
4 6

File 2 
x y
5 6
3 4 
2 3

我想将文件1中的每个点与文件2中的相应点连接起来.我知道在两个点之间绘制一个箭头

I want to connect each of these points inFile 1 with the corresponding points in File 2. I know to draw an arrow between two points it is

set arrow from (x,y) to (c,d)

但是如何从两个不同的文件中获取这些点的坐标以绘制连接线/箭头?

But how do I get the coordinates of these points from two different files to draw connecting lines/ arrows?

推荐答案

尽管最快和最短的合并不同文件中的列的方法可能是通过外部工具,但是我总结了3种仅支持gnuplot的方法(即与平台无关)用于合并来自不同文件/数据集的列.

Although, the fastest and shortest way to merge columns from different files is probably via an external tool, I nevertheless summarize 3 gnuplot-only (i.e. platform independent) methods for merging columns from different files/datasets.

  • 方法1显然很慢.
  • 方法2稍好一些,但有点令人困惑.比方法1快5-6倍
  • 方法3仅适用于gnuplot> = 5.2,因为它使用数组,大约为.比方法1快20-30倍

欢迎改进和评论.

### merging columns from different files/datasets with gnuplot only
reset session

# creating some dummy data
TimeStart = time(0.0)
N = 5000
set table $Data1
    set samples N
    plot '+' u (rand(0)):(rand(0)) with table
unset table
set table $Data2
    set samples N
    plot '+' u (rand(0)):(rand(0)) with table
unset table
TimeEnd = time(0.0)
print sprintf("Creating data: %.3f sec",TimeEnd-TimeStart)
# end creating dummy data

# Method 1
TimeStart = time(0.0)
stats $Data1 nooutput
RowCount = STATS_records
set print $FinalData1
set table $DataM1_1
do for [i=0:RowCount-1] {
    plot $Data1 u (a=$1,$1):(b=$2,$2) every ::i::i w table
    plot $Data2 u (c=$1,$1):(d=$2,$2) every ::i::i w table
    print sprintf("%g\t%g\t%g\t%g",a,b,c,d)
}
unset table
set print
TimeEnd = time(0.0)
print sprintf("Method 1: %.3f sec",TimeEnd-TimeStart)
# print $FinalData1
# end Method 1


# Method 2
TimeStart = time(0.0)
set table $DataM2_1
    plot for [i=1:2] $Data1 u ($0+i*0.1):i w table
    plot for [i=1:2] $Data2 u ($0+i*0.3):i w table
unset table

set table $DataM2_2
    plot $DataM2_1 u 1:2 smooth frequency
unset table

x1 = y1 = x2 = y2 = NaN
Shift(x) = (x1 = y1, y1 = x2, x2 = y2, y2 = x)
set table $DataM2_3
    plot $DataM2_2 u (Shift($2),x1):(y1):(x2):(y2) with table
unset table

set table $DataM2_4
    plot $DataM2_3 u 1:2:3:4 every 4::3 with table
unset table
set table $FinalData2 # remove empty lines
    plot $DataM2_4 u 1:2:3:4 every ::0 with table
unset table

TimeEnd = time(0.0)
print sprintf("Method 2: %.3f sec",TimeEnd-TimeStart)
# print $FinalData2
# end Method 2


# Method 3
# requires gnuplot >=5.2
# get number of rows, assuming to be identical for $Data1 and $Data2 
TimeStart = time(0.0)
stats $Data1 nooutput
RowCount = STATS_records
array Col1[RowCount]
array Col2[RowCount]
array Col3[RowCount]
array Col4[RowCount]

set table $DataM3_1
    plot $Data1 u (Col1[$0+1]=$1,0):(Col2[$0+1]=$2,0) w table
    plot $Data2 u (Col3[$0+1]=$1,0):(Col4[$0+1]=$2,0) w table
unset table

set print $FinalData3
do for [i=1:RowCount] {
    print sprintf("%g\t%g\t%g\t%g",Col1[i],Col2[i],Col3[i],Col4[i])
}
set print
TimeEnd = time(0.0)
print sprintf("Method 3: %.3f sec",TimeEnd-TimeStart)
# print $FinalData3
# end Method 3

### end of code

这篇关于从两个不同的文件绘制带有开始和结束的箭头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆