如何将轴移动到图表的中心?

时间:2012-10-05 15:43:11

标签: gnuplot

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

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

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

3 个答案:

答案 0 :(得分:12)

好吧,使用_zeroaxis是获得抽动和标签的“正确”方法:设置_范围为您提​​供对称性以使0,0居中...并且一旦您知道_范围,您就可以绘制箭头手动。

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)

enter image description here

答案 1 :(得分:4)

黑客攻击!

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

我得到这个输出: enter image description here

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

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

答案 2 :(得分:1)

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

# bring the grid over the plot
set grid front

# remove grid if not required
unset grid