如何从两个测量时间绘制一个绘图连接点?

2024-05-28 22:38:14 发布

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

第一个问题! 我有两列数据,每行是一对值。我想垂直绘制第一列和第二列,并用一条线连接每对值,如下链接中的图所示:

http://www.sciencedirect.com/science/article/pii/S0300957297000440#gr1enter image description here

如果您知道如何使用任何工具,如R、python、perl、excel,请告诉我!在


Tags: 工具数据comhttp链接wwwarticle绘制
3条回答

下面是Python中一个非常基本的尝试:

import pylab as pl

data = pl.array([[1,2],[2,3],[1,3],[2,1],[5,3],[3,2],[3,2],[1,1]])

first = data[:,0]
second = data[:,1]

xs = []
ys = []

for r in data:
   ys += list(r)
   ys.append(None)
   xs += [1.3,1.7]
   xs.append(None)

pl.plot([1.3]*len(first),first,'o',[1.7]*len(second),second,'o',xs,ys)
pl.boxplot(data)
pl.ylim([min(min(first),min(second))-.5,max(max(first),max(second))+.5])
labels = ("first", "second")
pl.xticks([1,2],labels)

pl.show()

将导致: enter image description here

下面是一个使用ggplot2的R方法,有点快而且有点脏:

library(ggplot2)

df <- data.frame(baseline=c(1,1,2,2,3,3,4,5,6,7,8,9,10,11),
                 sixmos  =c(5,6,5,7,8,9,10,12,12,2,1,5,2,3))

data <- data.frame(group = factor(1:nrow(df)), 
                   cat=c(rep('baseline',nrow(df)), 
                   rep('sixmos',nrow(df))), 
                   values=c(df$baseline,df$sixmos))

ggplot(data, aes(x=cat, y=values)) + 
  geom_line(aes(group=group)) + 
  geom_point(aes(group=group)) +
  geom_boxplot(data=df, aes(x='baselin', y=baseline)) + 
  geom_boxplot(data=df, aes(x='sixmos2', y=sixmos))

boxplots and slopegraph with ggplot2 in R

另请参见以下答案: Line charts by group

以及另一种使用matpoints和{}(和boxplot)的R方法

dd <- data.frame(x=rnorm(15), y= rnorm(15))

boxplot(dd, boxwex = 0.3)
# note that you need to transpose `dd`
matpoints(y= t(dd), x= c(1.17,1.83),pch=19, col='black')
matlines(y= t(dd), x= c(1.2,1.8), lty=1, col = 'black')

enter image description here

相关问题 更多 >

    热门问题