Gnuplot:如何绘制对立条形图或金字塔条形图

问题描述 投票:0回答:2

是否可以在 gnuplot 中使用两个正 y 轴?这只是这个问题的一个简单例子。

plot.gp:

reset
set style fill solid 1
set boxwidth 0.8 relative
plot 'data1.dat' index 1 using 1:2     with boxes title 'A' ,\
              '' index 2 using 1:(-$2) with boxes title 'B'

我想在plot.gp的最后一行使用

1:(-$2)
,而不是使用
1:2

数据1.dat:

0.12   0.024
0.15   0.132
0.18   0.241
0.22   0.136


0.12   0.039
0.15   0.219
0.18   0.197
0.22   0.155

来自:

enter image description here

致:

enter image description here

gnuplot
2个回答
2
投票

这是一个具有单个绘图的解决方案,但是 y 轴抽动位于不同的一侧。 也许有一些简单的方法可以让它们站在同一边。

脚本:

### two "positive" y-axes
reset session

$Data <<EOD
0.12   0.024
0.15   0.132
0.18   0.241
0.22   0.136


0.12   0.039
0.15   0.219
0.18   0.197
0.22   0.155
EOD

set style fill transparent solid 0.5
set boxwidth 0.8 relative

set yrange[-0.3:0.3]
set ytics 0,0.1
set mytics 2
set y2range[0.3:-0.3]
set y2tics 0,0.1
set my2tics 2
set y2tics mirror
set xzeroaxis ls -1

plot $Data u 1:2 index 0 axis x1y1 w boxes title 'A' ,\
        '' u 1:2 index 1 axis x1y2 w boxes title 'B'
### end of script

结果:

enter image description here

补充:

这是第二个建议,一侧有所有 ytic 标签。 一个小缺点是它不能自动缩放,您必须通过

set for [...] ytics (...)
“手动”设置范围和步骤。

脚本:

### y-axis with two "positive" directions
reset session

$Data <<EOD
0.12   0.024
0.15   0.132
0.18   0.241
0.22   0.136


0.12   0.039
0.15   0.219
0.18   0.197
0.22   0.155
EOD

set style fill transparent solid 0.5
set boxwidth 0.8 relative
set xzeroaxis ls -1

Min  = -0.3
Max  =  0.3
Incr =  0.1
set yrange[Min:Max]
set for [i=floor(Min/Incr):ceil(Max/Incr)] ytics (sprintf("%g",abs(i*Incr)) i*Incr)

plot $Data u 1:2     index 0 w boxes lc 3 title 'A' ,\
        '' u 1:(-$2) index 1 w boxes lc 4 title 'B'
### end of script

结果:

enter image description here


1
投票

theoz 的答案相同,只是 y2 标签移至图的左侧,并且

set link y2
命令用于概括轴反转。

您可以调整偏移量

graph -1.03
以叠加“0”标签,然后通过将y2 tic范围更改为
set y2tics 0.1,0.1
来删除重复的“0”标签。

$Data <<EOD
0.12   0.024
0.15   0.132
0.18   0.241
0.22   0.136


0.12   0.039
0.15   0.219
0.18   0.197
0.22   0.155
EOD

set style fill transparent solid 0.5
set boxwidth 0.8 relative
set xzeroaxis ls -1

set yrange[-0.3:0.3]
set ytics 0,0.1
set mytics 2

# Set y2 axis to exact mirror of y1
# Shift tic labels to the left and use right-justified text
set link y2 via -y inv -y
set y2tics 0,0.1
set y2tics offset graph -1.03 right

plot $Data u 1:2 index 0 axis x1y1 w boxes title 'A' ,\
        '' u 1:2 index 1 axis x1y2 w boxes title 'B'

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.