如何绘制一个文件中包含的多个数据块?

2024-06-16 09:54:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个文件,包含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或任何其他相关工具绘制此文件?在


Tags: 文件工具数据绘制matlabgnuploty1y2
3条回答

这里有一个方法可以在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")

假设你有档案“数据.dat“,假设您正在使用linux,并且可以使用awk,那么我建议您:

  • 使用awk删除文本并只显示要显示的数据块
  • 使用gnuplot内部for循环将数据显示为不同的绘图。在

如果您想自己尝试,这里有一个正在工作的gnuplot命令。我使用for循环来显示您的两个数据块,技巧是系统调用awk,awk变量a是“chunk index selector”

plot for [i=1:2] sprintf('< cat data.dat |  awk ''/./{ if ($1=="x") {a++} else if (a==%d) {print $0 }}''',i) u 1:2

最丑陋的解决方案是:) 但它很管用!!在

This is the resulting picture

编辑:你需要一个不错的gnuplot版本,我使用了gnuplot 4.6补丁级别3。在

在MathLab中创建两个矩阵

A=[0 -65.950939; 0.01417027519 -65.950969; 0.02834055037 -65.950946; 0.04251082556 -65.950961]

B=[0 -39.797446; 0.01417027519 -39.796663; 0.02834055037 -39.794279; 0.04251082556 -39.790951]

那么

^{pr2}$

也可以将plot函数用于多对矩阵参数。在

plot(x1,y1,x2,y2)

相关问题 更多 >