【问题标题】:Is it possible to use indices as xtic values in a histogram?是否可以在直方图中使用索引作为 xtic 值?
【发布时间】:2018-10-05 22:54:52
【问题描述】:

在具有以下结构的数据的文件中:

# [year]
"Town A" population
"Town B" population
"Town C" population

# [year2]
"Town A" population
"Town B" population
"Town C" population

# [year3]
"Town A" population
"Town B" population
"Town C" population

我已经设法使用以下方法创建了一个直方图:

set style data histogram
set style histogram columnstacked
p 'file.dat' index '[year]' u 2:key(1) ,\
'' index '[year2]' u 2,\
'' index '[year3]' u 2;

以前的设置几乎按照我需要的方式生成图形,但是,我希望能够使用索引名称(标签?)作为 xtic 值,到目前为止,xtic 值是 0、1、和 2 而不是 year、year2 和 year3。 gnuplot 可以在具有这种数据结构的直方图中使用索引值作为 xtics 吗?

【问题讨论】:

    标签: indexing gnuplot histogram


    【解决方案1】:

    file.dat 中的数据块之间需要两个空行:

    # [year]
    "Town A" 123
    "Town B" 543
    "Town C" 789
    
    
    # [year2]
    "Town A" 234
    "Town B" 666
    "Town C" 1000
    
    
    # [year3]
    "Town A" 345
    "Town B" 600
    "Town C" 800
    

    Gnuplot 命令(需要 5.2.5 或更高版本)

    set style data histogram
    set style histogram columnstacked
    set style fill solid border lc black
    plot 'file.dat' index '[year]' u 2:key(1) title strcol(-2),\
         '' index '[year2]' u 2 title strcol(-2),\
         '' index '[year3]' u 2 title strcol(-2)
    

    请注意,第 -2 列是指索引值。这并不是什么新鲜事,但 5.2.5 版现在允许您在 strcol() 函数(stringcolumn() 的简称)中使用它。标题和列间距可以改进。

    【讨论】:

    • 它适用于 SVG 终端,但在使用 pngcairo 时无效:
    • 它在 QT 终端中有效,但当我将终端设置为 pngcairo 时无效:set term pngcairo enhanced color transparent background '#FFFFFF' 在 [this] (i.postimg.cc/SxZNvBcc/qttermpopulation.png) 中使用 QT 终端时导致 [this] (i.postimg.cc/pTm9C279/poblacion.png)
    • 这对我来说毫无意义。所涉及的代码均不依赖于终端输出层。
    • 是的,我注意到了。此外,手册中关于索引命名的建议,如您所见,使 xlabels 看起来很好,很难看,是否必须命名索引 [string] 或 ==string==?
    • 我不知道。该建议只是某人根据我自己的经验和使用模式提出的建议。
    【解决方案2】:

    我看不到在plot 命令本身内设置它的方法。这是一个好主意,我将把它作为 gnuplot 的功能请求推送。我能建议的最好的就是这个,它假设你事先知道索引字符串的集合。 我将其显示为对 N 的迭代,因为我假设您可能确实有超过 3 个,并且对于更大的数字,迭代的输入要少得多。

    N = 3
    array INDEX[N] = [ "[year1]", "[year2]", "[year3]" ]
    set xtics 1
    set for [i=1:N] xtics add (INDEX[i] i-1)
    
    plot 'file.dat' index INDEX[1] u 2:key(1) ,\
         for [i=2:N] '' index INDEX[i] u 2
    

    【讨论】:

    • 感谢您的澄清。
    • 该功能已添加到最新版本 (gnuplot 5.2.5) 中,但对旧版本没有帮助。
    • 我编译了 5.2.5 版本,我将如何使用新功能 xtic(something) key(something)?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-05
    相关资源
    最近更新 更多