如何在单个Gnuplot窗口中为单个X轴绘制多个Y轴?

时间:2015-11-12 02:49:43

标签: gnuplot

我在Window 7" Professional"中使用32位版本的GNUPlot。操作系统环境(......可悲!)我想做一个"堆积图"对于ALL,只使用一个x轴的盒子,这是" TIME"以一系列"日期"的格式。

所有GNUPlot代码都可以使用,但每个图都使用自己独立的x轴,这会占用大量的图形空间。

我还需要能够为每个堆积图具有可变的y轴刻度......

这是"标有" (CSV)数据文件:

  

日期,时间,体重(kg),身高(cm),BMI,BP最大(mmHg),BP最小(mmHg),P / min,%脂肪09/09 / 2015,13:16:00 ,77.4,171,26.5,121,73,75,22.5 16/07 / 2015,09:14:34,76.9,170,26.6,111,70,76,23.5 26/06 / 2015,18:14:48 ,76.9,170,26.6,123,72,78,23.2 19/06 / 2015,08:45:42,77,172,26,96,60,89,22.1 15/06 / 2015,12:29:48,77.7 ,170,26.9,117,73,87,23.6 15/06 / 2015,12:15:58,77.8,170,26.9,127,76,77,23.7 15/06 / 2015,12:11:05,77.7 ,171,26.6,118,74,83,22.8 23/03 / 2015,16:39:55,78.6,170,27.2,119,72,78,24 20/03 / 2015,09:07:30,77.6 ,169,27.2,138,74,77,24.1 09/01 / 2015,14:30:00,79.2,170,27.4,114,71,75,24.1 07/10 / 2014,16:06:00,78.4 ,171,26.8,119,73,108,24.8 07/10 / 2014,16:08:00,78.4,170,27.1,109,72,75,25.1 15/09 / 2014,08:18:23,76.9,171 ,26.3,116,69,102,24.8 15/09 / 2014,09:20:27,76.7,172,25.9,132,76,91,21 04/09 / 2014,12:05:00,75.6,169,26.5 ,115,71,96,25.4 01/04 / 2014,11:18:00,76.2,171,26,115,69,70,22.9 19/03 / 2014,09:48:23,75.3,171,25.8,113 ,69,55,22.1 14/03 / 2014,10:39:29,75.6,170,26.2,108,69,78,22.5 05/03 / 2014,16:45:00,75.9,170,26.3,129 ,73,84,23.3 9月5日/ 2013,17:31:00,74.5,171,25.5,135,75,92,21

这是"当前"我用来生成5个堆积图的GNUPlot代码:

reset
set terminal windows size 1325, 625
set multiplot layout 5, 1 title "Individual Employee Biometric Data vs. Time"
set xlabel "DATE"
set timestamp
set key outside 
set key center right 
set pointsize 1.0 
set grid lw 1 
set timefmt "%d/%m/%Y" 
set xdata time 
set format x "%d/%m/%Y" 
set xrange [ "09/05/2013\t0000" : "09/09/2015\t0000" ] noreverse nowriteback 
set datafile sep ','
set arrow from 10.0,0 to 10.0, 0.5 lw 3
set label ' ' at 10.2,0.03
set label '(C) 2015' at 2050.0,-0.85 
set border lw 2

set yrange [73.0:80.0] 
set ylabel "(kg)" 
plot 'K8.dat' using 1:3 title "BODY\nWEIGHT" with linespoints lw 2 lt rgb 'red'

set yrange [25.0:30.0] 
set ylabel "kg/m^2" 
plot 'K8.dat' using 1:5 title "BODY\nMASS\nINDEX" with linespoints lw 2 lt rgb 'green'

set yrange [50.0:150.0] 
set ylabel "(mmHg)" 
plot 'K8.dat' using 1:6 title "SYS" with linespoints lw 2 lt rgb 'blue', \ 'K8.dat' using 1:7 title "DIAS" with linespoints lw 2 lt rgb 'coral'

set yrange [40.0:120.0] 
set ylabel "(bpm)" 
plot 'K8.dat' using 1:8 title "HEART\nRATE" with linespoints lw 2 lt rgb 'purple'

set xlabel "DATE" 
set yrange [15.0:30.0] 
set ylabel "(%)" 
plot 'K8.dat' using 1:9 title "BODY\nFAT" with linespoints lw 2 lt rgb 'orange'

PS - 此代码来自之前的GNUPlot例程,因此"借口" '#"注释出...

2 个答案:

答案 0 :(得分:2)

您可以使用multiplot将多个图堆叠在一起。您只需要为每个情节适当地关闭情节边框,请参阅help set border,并为除最下面的地块以外的所有情节取消设置横坐标xtics

set multiplot
set origin 0.1, 0.1
set size 0.9,0.3
set xrange [a:b]
plot "first"
set origin 0.1,0.4
unset xtics
set border 2 # only plot left border
plot "second"
set origin 0.1,0.7
plot "third"
unset multi

至关重要的是修复所有绘图的xrange,因为在关闭以下绘图的xtics之后,你无法看到它是否真的相同。

答案 1 :(得分:0)

(评论太长了)

好的,我现在得到堆叠图的意思。据我所知,在单个x轴上方有几个y轴(超过2个)是不可能的。

你可以做的是尝试通过在y(1)轴上绘制大约30 ... 150范围内的所有数据来伪造2个以上的轴,并且在15 ... 30范围内绘制所有数据y2轴。然而,线条将是所有类型的重叠而不是完全分开。

另一种替代方案是首先将所有数据标准化为例如0 ... 10范围通过减去最小值并除以最大值 - min,然后通过为第一行添加0,将第二行添加10,依此类推将它们堆叠在一起,依此类推。但是,你必须添加手工制作的y轴抽动(这是可能的,但有些麻烦)。

实际上,这里是我上面概述的发烧友解决方案的工作模板(为三个数据集实现,但可以扩展到基本上任意多个)

reset

set datafile separator ","

inputfile = 'data0.txt'

stats inputfile using 3 name 'STATS_WEIGHT'
STATS_WEIGHT_range = STATS_WEIGHT_max - STATS_WEIGHT_min

stats inputfile using 4 name 'STATS_HEIGHT'
STATS_HEIGHT_range = STATS_HEIGHT_max - STATS_HEIGHT_min

stats inputfile using 9 name 'STATS_FAT'
STATS_FAT_range = STATS_FAT_max - STATS_FAT_min

# more stats for further data -- apparently needs to be BEFORE the date/time stuff

set timefmt "%d/%m/%Y" 
set xdata time 
set format x "%d/%m/%Y" 
set xrange [ "09/05/2013\t0000" : "09/09/2015\t0000" ] noreverse nowriteback 


# define the offset at which the fake y-axes start; decrease or increase offsetIncrease for spacing (effectively: blank labels) between 'graphs'
startYTicsOffset = 0
numberOfFakeYTicsPerData = 6
scalingFactor = 1.0/(numberOfFakeYTicsPerData - 1.0)
offsetIncrease = numberOfFakeYTicsPerData + 0.5

#to get rid of actual yrange numbering, set a dummy label that will be overwritten
set ytics ("dummy" 0)

#increase total actual yrange factor as needed for additional series
set yrange [0: 3 * offsetIncrease] 

#add tics for weight, note that %.Xf prints the number with X decimals
do for[i=0:numberOfFakeYTicsPerData-1]{
  set ytics add (sprintf("%.0f kg", STATS_WEIGHT_min + i * scalingFactor * STATS_WEIGHT_range) startYTicsOffset+i)
}

#add tics for height
startYTicsOffset = startYTicsOffset + offsetIncrease
do for[i=0:numberOfFakeYTicsPerData-1]{
  set ytics add (sprintf("%.1f cm", STATS_HEIGHT_min + i * scalingFactor * STATS_HEIGHT_range) startYTicsOffset+i)
}

#add tics for fat - I couldn't figure out how to get gnuplot to print actual '%' character in sprintf directive (should be '%%' but doesn't appear to work)
startYTicsOffset = startYTicsOffset + offsetIncrease
do for[i=0:numberOfFakeYTicsPerData-1]{
  set ytics add (sprintf("%.1f percent", STATS_FAT_min + i * scalingFactor * STATS_FAT_range) startYTicsOffset+i)
}

###### ... add further tics ...


plot inputfile using 1:( 0 * offsetIncrease + ($3 - STATS_WEIGHT_min)/ (STATS_WEIGHT_range * scalingFactor) ) w lp title "weight",\
     inputfile using 1:( 1 * offsetIncrease + ($4 - STATS_HEIGHT_min)/ (STATS_HEIGHT_range * scalingFactor) ) w lp title "height",\
     inputfile using 1:( 2 * offsetIncrease + ($9 - STATS_FAT_min)   / (STATS_FAT_range * scalingFactor) )    w lp title "fat %"
### ... add further data ...
顺便提一下:如果您发布或编辑问题或答案,请尝试单击编辑窗口上方的图像图标。它将打开一个小窗口,您可以直接拖放图像而无需网络托管服务。像那样: enter image description here