【问题标题】:how to move axes to center of chart?如何将轴移动到图表中心?
【发布时间】:2012-10-05 15:43:11
【问题描述】:

我需要在坐标系上绘制一个函数,该坐标系的原点位于屏幕中心(或靠近中心的某个位置,但不一定在中心),我需要绘制轴,以便它们在原点交叉.轴也应该有标签和抽动,以及箭头。

我不知道如何有效地做到这一点,到目前为止,在我的代码中,我为抽动手动设置偏移量,并使用偏移量手动绘制箭头。我还在轴标签上设置了偏​​移量。所有这些都非常脆弱,偏移量会根据终端设置而变化。

有人可以帮助我提供示例代码或解释如何正确执行此操作吗?

【问题讨论】:

    标签: gnuplot


    【解决方案1】:

    嗯,使用 _zeroaxis 是获取抽动和标签的“正确”方法:设置 _range 可以让您对称以使 0,0 居中...一旦您知道 _range,您就可以绘制箭头手动开启。

    set xzeroaxis
    set xtics axis
    set xrange [-10:10]
    set arrow 1 from -9,0 to -10,0
    set arrow 2 from  9,0 to  10,0
    
    set yzeroaxis
    set ytics axis
    set yrange [-1:1]
    set arrow 3 from 0,-.9 to 0,-1
    set arrow 4 from 0,.9  to 0,1
    
    set border 0
    
    plot sin(x)
    

    【讨论】:

    • +1。 set zeroaxis 似乎在一个命令中使两个轴都通过原点。也可以为轴指定你想要的粗细和线型:set zeroaxis linewidth 2 linetype 1
    • 您可能想先unset x2ticsunset y2tics。否则你会得到所有的抽动显示两次。
    【解决方案2】:

    黑客攻击!

    set term pngcairo truecolor size 300,300 font "Arial,12"
    set out 'plot.png'
    
    # x,y min/max and center
    xmin = -10 
    xc = 0 
    xmax = 10
    ymin = -2
    yc = 0 
    ymax = 2 
    # default borders
    tm = 1 
    bm = 1 
    rm = 4 
    lm = 4 
    # arrow scale factor to cover last tic 
    af = 1.05
    set arrow from xc,yc to xmin*af,yc filled size 0.6,30
    set arrow from xc,yc to xmax*af,yc filled size 0.6,30
    set arrow from xc,yc to xc,ymax*af filled size 0.6,30
    set arrow from xc,yc to xc,ymin*af filled size 0.6,30
    
    set multiplot layout 2,2 
    ## Plot 1, top left
    set key top left
    set xr [xmin:xc]
    set yr [yc:ymax]
    set tmargin tm
    set bmargin 0
    set rmargin 0
    set lmargin lm
    set border 9
    unset ytics
    set xtics nomirror
    plot sin(x)
    ## Plot 2, top right
    unset key
    set xr [xc:xmax]
    set lmargin 0
    set rmargin rm
    set border 3
    set ytics nomirror
    replot
    ## Plot 3, bottom left
    set xr [xmin:xc]
    set yr [ymin:yc]
    set bmargin bm
    set tmargin 0
    set lmargin lm
    set rmargin 0
    set border 12
    unset tics
    replot
    ## Plot 4, bottom right
    set xr [xc:xmax]
    set lmargin 0
    set rmargin rm
    set border 6
    set ytics nomirror
    replot
    unset multiplot
    

    我得到这个输出:

    也就是说,您可以查看zeroaxis 选项。有一个演示here。遗憾的是,此选项不会在零处绘制轴,而只是在该位置放置一条线。

    我不会说我的方法是“有效的”,但除了更改正在绘制的函数/数据之外,可能不需要太多努力来修改它,因为大部分工作都是由replot 命令完成的。我不知道让 gnuplot 将箭头放在轴的末尾或更改轴的本地绘制位置的选项。

    【讨论】:

      【解决方案3】:

      除了 Jim 的回答,如果需要,可以将抽动和轴放在前面

      # bring the grid over the plot
      set grid front
      
      # remove grid if not required
      unset grid
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-26
        • 2021-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多