【问题标题】:Gnuplot, linespoints + histogram, one graph, two datafilesGnuplot,线点 + 直方图,一张图,两个数据文件
【发布时间】:2018-09-14 19:02:53
【问题描述】:

阅读 gnuplot 手册并没有帮助我解决这个问题,我的互联网搜索也没有取得成果。我正在使用gnuplot 5.2 patchlevel 0

我正在使用文件中的数据绘制下图,并添加了计算得出的移动平均值(代码如下) 使用的文件是这个,graph.dat:

20180607-1200 20.4
20180611-1200 23.2
20180617-1200 22.1

我想在图表底部添加一个直方图,基于另一个具有相同 x 值的文件 histogram.dat

20180607-1200 18.95
20180611-1200 18.52
20180617-1200 18.76

我已经尝试使用以下代码:

set terminal png size 640,396
set border front lw 1

# Make left y-axis ticks and numbers disappear
set ytics scale 0
set ytics textcolor rgb "white"

set y2tics
set y2range [18.5:25.7]

set output 'graph.png'

set xdata time
set timefmt "%Y%m%d-%H%M"
set xtics timedate
set xtics format "%Y-%m-%d"
set xtics rotate
set mxtics 7
# A week is 604800 seconds
set xtics "20180607-1200",604800,"20180617-1200"
set offsets graph 0.015, graph 0.015, 0, 0

# For moving average
samples(x) = $0 > 4 ? 5 : ($0+1)
avg5(x) = (shift5(x), (back1+back2+back3+back4+back5)/samples($0))
shift5(x) = (back5=back4, back4=back3, back3=back2, back2=back1, back1=x)

# Initialize the running sum
init(x) = (back1 = back2 = back3 = back4 = back5 = sum = 0)

# Plot data, running average, and histogram
plot sum = init(0), \
"graph.dat" using 1:2 axes x1y2 title "graph" with linespoints, \
'' using 1:(avg5($2)) axes x1y2 title "moving average" with linespoints, \
"histogram.dat" using 1:2 axes x1y2 title "histogram" with histogram clustered

但在添加最后一行(以"histogram.dat" 开头)后,它会返回以下错误消息:

plot sum = init(0), "graph.dat" using 1:2 axes x1y2 title "graph" with linespoints, '' using 1:(avg5($2)) axes x1y2 title "moving average" with linespoints, "histogram.dat" using 1:2 axes x1y2 title "histogram" with histogram clustered
                                                                                                                                                                                                                                  ^
"grapher.gp", line 35: Too many columns in using specification

我不明白。 我的目标是在底部有一个小直方图,就像在这个构造的例子中一样: 问题:我做错了什么?我如何根据一张图中显示的两个不同文件执行linespointshistogram? [我后来发现为此必须使用boxes 而不是histogram - 请参阅下面我自己的答案]

额外问题:有没有比我将它们变成白色更好的方法来避免显示 y1 轴数?

【问题讨论】:

    标签: gnuplot histogram


    【解决方案1】:

    到目前为止没有人回答,同时我已经设法解决了第一个问题,所以我会在这里分享它,以防它可以帮助某人。我听说 Stack Overflow 鼓励我们在这种情况下回答自己的问题。

    为了让它发挥作用,我使用了以下技术的组合:

    http://psy.swansea.ac.uk/staff/carter/gnuplot/gnuplot_time_histograms.htm

    并研究 gnuplot 5.2 手册,以及来自互联网的大量提示。 这是结果图:

    这是生成图表的代码。有点长,可能需要滚动一下:

    set output 'graph.png'
    set terminal png size 640,396
    set origin 0, 0.25             # Origin for graph+margins. (Screen coordinates)
    set size 1, 0.75               # X/Y scaling of size of graph+labels+margins
    set rmargin at screen 580.0/640   # Distance from border to canvas right edge
    set bmargin at screen 120.0/396   # Distance from border to canvas bottom edge
    
    set border front lw 1
    
    # Make left y-axis ticks and numbers disappear (Is there a better way?)
    set ytics scale 0
    set ytics textcolor rgb "white"
    
    set y2tics               # Put tics and numbers on rightmost y-axis
    set y2range [18.5:25.7]  # Range for rightmost y-axis
    
    set xdata time
    set timefmt "%Y%m%d-%H%M"
    set xtics timedate
    set xtics format "%Y-%m-%d"
    set xtics rotate
    set mxtics 7
    # One week is 604800 seconds
    set xtics "20180607-1200",604800,"20180617-1200"
    
    # Distance between border and the plotted data. Left, right, top, bottom.
    # 1/4 day is 21600 seconds
    set offsets 21600, 21600, 0, 0
    
    # For moving average
    samples(x) = $0 > 4 ? 5 : ($0+1)
    avg5(x) = (shift5(x), (back1+back2+back3+back4+back5)/samples($0))
    shift5(x) = (back5=back4, back4=back3, back3=back2, back2=back1, back1=x)
    
    # For initializing the running sum
    init(x) = (back1 = back2 = back3 = back4 = back5 = sum = 0)
    
    
    set multiplot
    
        # Plot data and running average
        plot sum = init(0), \
        "graph.dat" using 1:2 axes x1y2 title "graph" with linespoints, \
        '' using 1:(avg5($2)) axes x1y2 title "moving average" with linespoints
    
    
        # Prepare for the histogram
        unset key      # No key/legend for the histogram
        unset xtics    # Avoid "doubled" labels on the x-axis
        unset y2tics   # Avoid "doubled" labels on the y2-axis
        unset border   # No need to draw the border twice
    
        # 1/8 day is 10800 seconds
        set boxwidth 10800 absolute
        set style fill solid
        # Distance between border and the plotted data. Left, right, top, bottom.
        # First plot's left/right offsets minus boxwidth is 10800
        set offsets 10800, 10800, 0, 0
    
        # Plot histogram
        plot "histogram.dat" using 1:2 with boxes lc rgb "#383880" axes x1y2
    
    unset multiplot
    

    我不太确定比率的概念是什么,在 rmarginbmargin 命令中。涉及一些试验和错误。

    如果有人可以解释这一点,或者对我原帖中的“奖金问题”有答案,请随时。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-25
      • 2013-07-28
      • 2020-02-23
      • 2012-11-09
      相关资源
      最近更新 更多