【问题标题】:How to plot several chunks of data contained in a single file?如何绘制单个文件中包含的几块数据?
【发布时间】:2014-03-13 12:39:28
【问题描述】:
我有一个文件,其中包含分成块的 2 列。每个块都有一个标题,后面跟着一些数据。这些块由一个空行相互分隔。比如:
x y1
0 -65.950939
0.01417027519 -65.950969
0.02834055037 -65.950946
0.04251082556 -65.950961
x y2
0 -39.797446
0.01417027519 -39.796663
0.02834055037 -39.794279
0.04251082556 -39.790951
: :
如何使用 MATLAB、xmgrace、gnuplot 或任何其他相关工具绘制此文件?
【问题讨论】:
标签:
python
r
matlab
matplotlib
gnuplot
【解决方案1】:
这是 R
中的一种方法
# Your data
df <- read.table(text="x y1
0 -65.950939
0.01417027519 -65.950969
0.02834055037 -65.950946
0.04251082556 -65.950961
x y2
0 -39.797446
0.01417027519 -39.796663
0.02834055037 -39.794279
0.04251082556 -39.790951")
# Create indicator for blocks
df$tag <- cumsum(grepl("[[:alpha:]]",df$V1))
# Reomve letters from columns
df <- df[!grepl("[[:alpha:]]",df$V1),]
# Convert to numerics
df[] <- sapply(df , as.numeric)
#plot
library(ggplot2)
ggplot(df , aes(V1 , V2 )) + geom_point() + facet_wrap(~tag , scales="free")
【解决方案2】:
假设您有一个文件“data.dat”,并假设您在 linux 上工作并且可以使用 awk,这就是我的建议:
- 使用 awk 删除文本并仅显示您要显示的数据块
- 使用 gnuplot 内部 for 循环将数据显示为不同的图。
如果您想自己尝试,这里有一个有效的 gnuplot 命令。我使用for循环来显示你的两个数据块,诀窍是系统调用awk,awk变量a是“块索引选择器”
plot for [i=1:2] sprintf('< cat data.dat | awk ''/./{ if ($1=="x") {a++} else if (a==%d) {print $0 }}''',i) u 1:2
这是我发现的最难解决的问题 :)
但它有效!
编辑:你需要一个不错的 gnuplot 版本,我使用的是 gnuplot 4.6 patchlevel 3。