微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 gnuplot 中用 x1、y1 和 x2、y2 轴绘制水平条形图?

如何解决如何在 gnuplot 中用 x1、y1 和 x2、y2 轴绘制水平条形图?

我想用 Gnuplot 创建一个水平条形图,它由 2 个 x 轴和 2 个 y 轴和 2 个坐标原点组成。数据集 1 x1/y1 的坐标原点应在左上角,数据集 2 x2/y2 的坐标原点应在右下角(见下图)。

Example of a horizontal bar chart with 2 different data sets

这完全可以用 Gnuplot 实现吗?如果不是,可以选择使用哪个程序?非常感谢。

解决方法

由于您似乎是一个新的 gnuplot 用户(但不是新的 StackOverflow 成员,因此您现在应该期待一些代码),因此可能难以根据您的要求调整评论中的链接答案。绘图样式为 with boxxyerror,勾选 help boxxyerror,轴分配勾选 help axes。 此外,为了让您的数字“可读”,我不知道 gnuplot 有一些用于千位分隔符的通用格式,至少不适用于 Windows,但显然适用于 Linux(请参阅 gnuplot: How to enable thousand separators (digit grouping)?)。

检查以下示例:

代码:

### two way horizontal bars (and thousand separators)
reset session

$Data1 <<EOD
USA           1368102
Russland      541353
Australien    403382
China         324068
Polen         222394
Vietnam       199876
Pakistan      176739
Mongolei      119426
Kanada        118270
Indien        38971
Deutschland   36500
Indonesien    27998
Serbien       13074
Brasilien     12587
Rumänien      9640
Kosovo        9262
Argentinien   7300
EOD

$Data2 <<EOD
"Bosnien \\& Herzegowina"   14.0
Thailand                 14.9
Laos                     15.9
Rumänien                 23.6
Bulgarien                30.3
Griechenland             36.1
Serbien                  37.5
Tschechien               39.2
Australien               45.1
Indien                   45.3
USA                      51.7
Polen                    58.6
Indonesien               60.0
Russland                 80.0
Türkei                   85.0
China                    150.0
Deutschland              166.3
EOD

set xrange [220:0]
set xtics nomirror
set x2range [0:2000000]
set x2tics () nomirror  # remove x2tics do it manually later
set yrange [17:-1]
set ytics out
set y2range [17:-1]
set y2tics out nomirror
myBoxWidth = 0.8     # relative boxwidth
yLow(i)  = i - myBoxWidth/2.
yHigh(i) = i + myBoxWidth/2.
unset key
set style fill solid 0.5

# simple workaround for thousand separators (only for integer number strings)
ThousandSep(s) = (_tmp= '',sum [_i=0:strlen(s)-1] (_i%3==0 && _i!=0 ? _tmp="'"._tmp : 0,\
                  _tmp=s[strlen(s)-_i:strlen(s)-_i]._tmp,0),_tmp)

# manual x2tics with thousand separators
do for [i=0:1500000:300000] {
    myX2tic = ThousandSep(sprintf('%.0f',i))
    set x2tics add (myX2tic i)
}

set label 1 at second 600000,graph 0.7 "Bestand"            left font ",20"
set label 2 at first     140,graph 0.4 "Jahres-\nförderung" left font ",20"

plot $Data1 u (0):0:(0):2:(yLow($0)):(yHigh($0)):ytic(1) axes x2y1 w boxxy lc rgb 0x8ffd8c,\
     '' u 2:0:(ThousandSep(strcol(2))) axes x2y1 w labels left offset 1,\
     $Data2 u (0):0:(0):2:(yLow($0)):(yHigh($0)):y2tic(1) axes x1y2 w boxxy lc rgb 0x8d7bde,\
     '' u 2:0:2 axes x1y2 w labels right offset -1,\
### end of code

结果:

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。