我目前正在做一个项目,在我的地方绘制湿度和温度的发展图。因此,我使用一个覆盆子π2与拉斯皮恩杰西和一个DHT-2。
最后,它归结为一个文件每天,存储所有30秒的测量。该文件名为: 2016-01-30_Temp_Hum_data,并包含以下数据
2016-01-30-19:30:03 22.0 50.2
2016-01-30-19:30:34 22.0 50.2
2016-01-30-19:31:04 22.0 50.3
2016-01-30-19:31:35 22.0 50.3
2016-01-30-19:32:05 22.0 50.2第一部分是时间戳,第二个值由一个空间隔开,然后是温度,然后是湿度。
现在,我用下面的脚本来绘制这幅图,这个脚本由bash文件中的for-循环调用,该脚本遍历我前面描述的所有数据文件。
!/usr/bin/gnuplot
reset
# This command works for a linux computer. In linux, you need to specify the exact location of
# the font you want to use
set terminal png notransparent rounded giant font "/usr/share/fonts/msttcore/arial.ttf" 24 \
size 2600,960
# nomirror means do not put tics on the opposite side of the plot
set xtics nomirror
set ytics nomirror
# Line style for axes
# Define a line style (we're calling it 80) and set
# lt = linetype to 0 (dashed line)
# lc = linecolor to a gray defined by that number
set style line 80 lt 0 lc rgb "#808080"
# Line style for lines
set style line 1 lt 1 lc rgb "#A00000" lw 4
set style line 2 lt 1 lc rgb "#00A000" lw 4
# Add line at 70
# Draw a line from the right end of the graph to the left end of the graph at
# the y value of 70
# The line should not have an arrowhead
# Linewidth = 4
# Linecolor = black
# It should be in front of anything else drawn
set arrow from graph 0,first 70 to graph 1, first 70 nohead lw 2 lc rgb "#500000" front
set arrow from graph 0,first 50 to graph 1, first 50 nohead lw 2 lc rgb "#005000" front
set arrow from graph 0,first 20 to graph 1, first 20 nohead lw 2 lc rgb "#005000" front
# Put a label 80% hum at 80% the width of the graph and y = -2 (it will be just above the line drawn)
set label "70%" at graph 0.8, first 72
set label "50%" at graph 0.8, first 48
set label "20°C" at graph 0.8, first 22
# Define data
set xdata time
set timefmt "%Y-%m-%d-%H:%M:%S"
set format x "%H"
set xlabel "time"
set ylabel "values"
set yrange [10:80]
heading="Kueche Temperatur und Luftfeuchtigkeit " . timestamp
set title heading
set key reverse Left outside
set grid
set style data lines
plot datafile using 1:2 ls 1 title "",datafile using 1:3 ls 2 title ""它像这样工作得很好,但现在每当我开始做饭或洗衣机时,我都做了一个节点,并制作了一个这样的文件:
2016-01-30-15:00:00 cooking
2016-01-22-19:00:00 washing machine
2016-01-23-12:30:00 washing machine现在,我想添加以下功能。如果烹饪/洗涤文件中有一个条目属于地块的x范围,我想要一个从底部到顶部的垂直线,上面写着“烹饪”或“洗衣机”。
不幸的是,在某一时刻,我甚至没有得到一条垂直线。我尝试了set arrow from 15,0 to 15,100 nohead lw 2 lc rgb "#500000" front和set arrow from 2016-01-30-19:00:00,0 to 2016-01-30-19:00:00,100 no head lw 2 lc rgb "#500000" front,以及我在网上找到的其他几个例子,但是都没有效果。
有谁能帮我找个办法
提前谢谢!!
发布于 2016-01-31 05:21:21
我不打算在这里复制你的整个剧本,但是集中在你要问的部分--增加垂直线。
我将在图的顶部添加标签,这样做将需要添加一些额外的顶部边距,以使它们适合。如果你把它们放在图表本身,你就不需要这个额外的上边距。此外,绘制垂直线变得容易得多,因为你已经固定了范围。我们仍然可以在不这样做的情况下完成这项工作,但是我们需要创建一个图,计算出它所使用的yrange,然后使用该计算的yrange绘制新的图,或者使用stats命令来计算出它(我们实际上需要stats命令稍后才能计算出xrange)。我们用固定的范围来避免这一点。
我需要演示的最小设置如下:
set xdata time
set timefmt "%Y-%m-%d-%H:%M:%S"
set format x "%m/%d"
set yrange [10:80]
set tmargin at screen .9
unset key当您将其编织到完整的脚本中时,您可以添加附加细节(标题、轴标签等)。我还更改了x轴标签,以明确线是在正确的地方绘制(您将使用您的原始标签)。您可能需要调整标题的偏移量,以避免行标签和标题之间的冲突。
最后,我们可以用以下方法绘制垂直行和标签(您希望将其与现有的绘图命令结合起来)
plot datafile u 1:(10):(0):(70) with vectors nohead,\
"" u 1:(80):2 with labels offset 0,char 1.1 rotate by 45我们使用向量样式(help vectors),它需要四个值: x、y、x_delta和y_delta。由于我们想要垂直线,我们将x_delta设置为0,当我们从底部(y=10)到顶部(y=80)时,我们可以将y设置为10,而y_delta设置为70 (80-10)。
当然,标签要简单得多。在这里,我们将标签放置在图形的顶部(向上移动1.1个字符将其置于图形之上),并旋转以避免重叠标签。

不幸的是,这里有一个问题。垂直行文件的范围可能会破坏从原始文件确定的范围。这可以通过使用stats命令来修复,然后修复范围。stats命令不喜欢时间序列数据,但是我们可以作为第一个步骤(在set xdata time之前)手动解析时间序列。
如果我们将以下两个命令放在脚本的最顶端
stats datafile u (strptime("%Y-%m-%d-%H:%M:%S",strcol(1))) nooutput
set xrange[STATS_min:STATS_max]它将根据数据文件中的范围修复xrange,因此我们没有这个问题。
原始示例数据与绘制垂直线所提供的数据没有任何重叠。为了演示这一点,我在数据文件的开头增加了一行,
2016-01-30-12:30:03 30.0 70.2强迫有重叠。因此,接受原始脚本,在开头添加这些额外的命令(stats和xrange),并将两个新的绘图命令添加到现有的plot命令中(该命令现在读取)。
plot datafile1 using 1:2 ls 1 title "",\
"" using 1:3 ls 2 title "", \
datafile2 u 1:(10):(0):(70) with vectors nohead title "",\
"" u 1:(80):2 with labels offset 0,char 1.1 rotate by 45 title ""),我们获得

这里我没有使用你的set终端命令,所以字体看起来可能不一样。您可能需要对大小和偏移进行一些调整,以生成您满意的最终绘图,但这将增加您的原始需求。我也没有在这里添加额外的页边距,因为标题自动做到了这一点。但是,如果您需要将标题向上移动以避免使用标签(在本例中我没有这样做),那么您可能需要添加一些额外的空白。
https://stackoverflow.com/questions/35105672
复制相似问题