有没有办法使用 gnuplot 在一张图上设置 3 个不同的 Y 轴?
Is there a way to have 3 different Y axes on one graph using gnuplot?
我正在使用带有天气 API 的 GNUplot 制作天气显示图表。我目前正在绘制未来 48 小时的温度和降雨量。
正如你在上图中看到的,温度是左边定义的轴的线;而降雨量由条形图(左下)描绘,其轴在右侧定义。 (0, 0.5, 1).
不过,我还想在图表中包含其他数据。我想要包括的第一件事是图表顶部的云量。再次作为条形图。
我制作的模型是一个图形编辑器:
有没有办法用 gnuplot 做到这一点,还是我必须使用另一个程序来完成它?
左边是 y1 轴,右边是 y2 轴。如果你想要第 3 个 y-axis,你必须以某种方式移动它。实现这一目标的一种方法是使用 multiplot
,基本上是几个地块相互叠加。
您必须确保所有图都在 canvas 上使用相同的(固定的)边距(automargin 可能不起作用)和相同的 xrange(第二个图从第一个图中获取)。使用一些随机数据检查以下示例。当然,可以进行一些微调。根据您的需要进行调整。
代码:
### Three y-axes
reset session
# create some test data
myTimeFmt = "%d.%m.%Y %H:%M:%S"
set print $Data
do for [i=1:48] {
myTime(i) = strftime(myTimeFmt, time(0)+i*3600)
myTemp(i) = sin(i/5.)*5 + 20 + rand(0)
myRain(i) = int(rand(0)+0.3) * rand(0)*20
myCloud(i) = rand(0)*50
print sprintf("%s %g %g %g",myTime(i),myTemp(i),myRain(i),myCloud(i))
}
set print
set key off
set margins screen 0.1, screen 0.8, screen 0.1, screen 0.94
set multiplot
set format x "%H:%M" timedate
set xtics 3600*6
set grid xtics, mxtics, ytics, mytics
##### first plot
set ylabel "Temperature °C" tc "red"
set yrange[10:30]
set ytics nomirror tc "red"
set y2label "Rain / mm" offset -1,0 textcolor rgb "blue"
set y2range[0:40]
set y2tics nomirror tc "blue"
set style fill solid 1.0
plot $Data u (timecolumn(1,myTimeFmt)):3 axes x1y1 w l lc "red", \
'' using (timecolumn(1,myTimeFmt)):4 axes x1y2 w boxes lc "blue"
unset xlabel
unset ylabel
unset y2label
unset tics
##### Second plot
set bmargin screen 0.73
set border 4
set xrange[GPVAL_X_MIN:GPVAL_X_MAX] # identical xrange like 1st plot
set y2range[100:0] reverse
plot $Data u (timecolumn(1,myTimeFmt)):5 axes x1y2 w boxes lc rgbcolor "grey"
##### Third plot (just for 3rd y-axis)
set rmargin at screen 0.9
set border 8 # only right border visible
set y2label "Cloud coverage" offset -1,0 textcolor rgb "black"
set y2tics nomirror offset 0,0
plot NaN # plot some dummy
unset multiplot
### end of code
结果:
我正在使用带有天气 API 的 GNUplot 制作天气显示图表。我目前正在绘制未来 48 小时的温度和降雨量。
正如你在上图中看到的,温度是左边定义的轴的线;而降雨量由条形图(左下)描绘,其轴在右侧定义。 (0, 0.5, 1).
不过,我还想在图表中包含其他数据。我想要包括的第一件事是图表顶部的云量。再次作为条形图。
我制作的模型是一个图形编辑器:
有没有办法用 gnuplot 做到这一点,还是我必须使用另一个程序来完成它?
左边是 y1 轴,右边是 y2 轴。如果你想要第 3 个 y-axis,你必须以某种方式移动它。实现这一目标的一种方法是使用 multiplot
,基本上是几个地块相互叠加。
您必须确保所有图都在 canvas 上使用相同的(固定的)边距(automargin 可能不起作用)和相同的 xrange(第二个图从第一个图中获取)。使用一些随机数据检查以下示例。当然,可以进行一些微调。根据您的需要进行调整。
代码:
### Three y-axes
reset session
# create some test data
myTimeFmt = "%d.%m.%Y %H:%M:%S"
set print $Data
do for [i=1:48] {
myTime(i) = strftime(myTimeFmt, time(0)+i*3600)
myTemp(i) = sin(i/5.)*5 + 20 + rand(0)
myRain(i) = int(rand(0)+0.3) * rand(0)*20
myCloud(i) = rand(0)*50
print sprintf("%s %g %g %g",myTime(i),myTemp(i),myRain(i),myCloud(i))
}
set print
set key off
set margins screen 0.1, screen 0.8, screen 0.1, screen 0.94
set multiplot
set format x "%H:%M" timedate
set xtics 3600*6
set grid xtics, mxtics, ytics, mytics
##### first plot
set ylabel "Temperature °C" tc "red"
set yrange[10:30]
set ytics nomirror tc "red"
set y2label "Rain / mm" offset -1,0 textcolor rgb "blue"
set y2range[0:40]
set y2tics nomirror tc "blue"
set style fill solid 1.0
plot $Data u (timecolumn(1,myTimeFmt)):3 axes x1y1 w l lc "red", \
'' using (timecolumn(1,myTimeFmt)):4 axes x1y2 w boxes lc "blue"
unset xlabel
unset ylabel
unset y2label
unset tics
##### Second plot
set bmargin screen 0.73
set border 4
set xrange[GPVAL_X_MIN:GPVAL_X_MAX] # identical xrange like 1st plot
set y2range[100:0] reverse
plot $Data u (timecolumn(1,myTimeFmt)):5 axes x1y2 w boxes lc rgbcolor "grey"
##### Third plot (just for 3rd y-axis)
set rmargin at screen 0.9
set border 8 # only right border visible
set y2label "Cloud coverage" offset -1,0 textcolor rgb "black"
set y2tics nomirror offset 0,0
plot NaN # plot some dummy
unset multiplot
### end of code
结果: