【问题标题】:Plotting a histogram with a conditional用条件绘制直方图
【发布时间】:2012-08-23 12:04:45
【问题描述】:

我正在尝试使用文件中的数据制作直方图,如下所示:

#Column 1   Column 2
#
0.0300      0.2126
1.0000e-4   0.0104
6.0000e-3   0.1299
1.0000e-4   8.0600e-3
1.0000e-4   0.0105
0.0190      0.2204
6.0000e-3   7.4900e-3
1.0000e-4   0.0952
6.0000e-3   7.4200e-3
1.0000e-4   0.0131
0.0190      0.3062
0.0190      0.2561
0.0300      0.9748
0.0300      0.9406
0.0300      0.0139
1.0000e-4   0.0281
0.0300      0.3625
1.0000e-4   0.0945
0.0300      0.5650
1.0000e-4   0.1045
6.0000e-3   0.2362
1.0000e-4   0.0180
1.0000e-4   0.1366
1.0000e-4   0.0195
0.0300      0.4652
0.0190      0.3505
0.0300      0.5146
0.0190      0.4319
6.0000e-3   0.2054
6.0000e-3   0.2377
0.0300      0.5281
1.0000e-4   0.1128
6.0000e-3   0.0623

如果我使用代码:

n=20    #number of intervals
max=0.03 #max value
min=0    #min value
width=(max-min)/n        #interval width
hist(x,width)=width*floor(x/width)+width/2.0

plot 'data' u (hist(\$1,width)):(1.0) smooth freq w boxes lc rgb "blue" lt 1 lw 0.5 notitle

我得到了正确的直方图:

但如果我使用条件行:

plot 'data' u (hist((\$2<=0.5?\$1:1/0),width)):(1.0) smooth freq w boxes lc rgb "blue" lt 1 lw 0.5 notitle

我明白了:

您可以看到 gnuplot 没有正确添加线条,而是将它们绘制为单独的列。

有没有办法解决这个问题?谢谢!

【问题讨论】:

  • 这是 bash “here”脚本的一部分吗?否则,我无法弄清楚您为什么要逃避“$
  • 是的,它是一个脚本。在任何情况下,这都不是真正的问题,因为转义 $ 不会对第一行 plot 产生任何问题(其中没有条件)
  • 当您尝试将内容粘贴到 gnuplot 脚本中运行时,转义 $ 会出现问题;)。不过不用担心。不过,我已经为这个(非常有趣的)问题添加了 1 个解决方案。

标签: gnuplot histogram


【解决方案1】:

我怀疑这是 gnuplot 如何处理“丢失”数据的症状。关于缺失数据,以下其实略有不同:

plot 'data' u 1:2 w lines    #connects lines across missing records
plot 'data' u 1:($2) w lines #doesn't connect lines when a missing record is encountered

我怀疑您看到此设计决策的症状略有不同。

不幸的是,它使典型的 gnuplot 数据过滤器在这里毫无用处:-(。幸运的是,您的情况很容易转移到 awk:

plot "< awk '{if ($2 <= 0.5) {print $0}}' test.dat " u (hist($1,width)):(1.0) smooth freq w boxes lc rgb "blue" lt 1 lw 0.5 notitle

现在 gnuplot 只能看到您想要的数据(因为它没有看到任何 NaN 值,所以它不会创建任何新的计数器)。

【讨论】:

  • 请告诉我,如果我理解你写那行的方式。据我所知,它的意思是:“如果文件 'test.dat' 的第 2 列中的值 。这是正确的吗?
  • 是的,awk 脚本只是删除第 2 列中元素值大于 0.5 的行。 (在awk 中,$0 是完整的行,所以它读起来像这样:if (column2_value &lt;= 0.5) then print full_line。我认为在awkprint $0 几乎相当于只是一个裸print,但我更喜欢前者,因为我认为它对正在发生的事情更加明确(只要你知道$0 代表什么......
  • 太棒了!非常感谢@mgilson,非常感谢您的帮助。
  • @Gabriel -- 没问题。一如既往的愉快。我喜欢回答 gnuplot 问题,因为我喜欢 gnuplot。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-28
  • 2019-02-22
  • 2012-04-13
  • 1970-01-01
相关资源
最近更新 更多