3

创建一个标准堆栈图非常好,现在我想为适合特定范围的框使用不同的颜色。

为此,我采用了以下方法:

set palette maxcolors 2
set palette defined  ( 0 '#C0504D', 1 '#00B059')

plot dataFileCity using (rounded(stringcolumn(1) eq city  ? $2 : NaN)):
(100 / (bin_width * STATS_records)):($2 > 1300 ? 0 : 1) 
smooth frequency with boxes palette

如果第 2 列的值高于 1300,我希望使用不同的颜色。

它基于: gnuplot 中的归一化直方图,添加了函数 plot 以及 Gnuplot 中某些特定值的不同颜色的颜色条

但是,我担心平滑的频率会使事情无法正常工作。如何传递创建不同颜色的值?

4

2 回答 2

0

添加到@TKF 的答案......无需将smooth freq数据拆分为两个表。相反,将其绘制到一个表中,lc rgb variable并通过定义适当的函数来设置颜色。以下示例适用于 gnuplot>=5.2,但在早期版本中也进行了一些修改。

代码:

### histogram with split colors
reset session

# create some random test data
set print $Data
    do for [i=1:2000] {
        print sprintf("%g",int(invnorm(rand(0))*100))
    }
set print

stats $Data u 1 nooutput
xmin = STATS_min
xmax = STATS_max
N = 20
myWidth = (xmax-xmin)/N
bin(col) = myWidth*floor(column(col)/myWidth)+myWidth/2.

set boxwidth myWidth
set key noautotitle
set style fill solid 0.3
set grid x,y

set table $Histo
    plot $Data u (bin(1)) smooth freq 
unset table

myColor(col) = column(col)<0 ? 0xff0000 : 0x00cc00

plot $Histo u 1:2:(myColor(1)) w boxes lc rgb var 
### end of code

结果:

在此处输入图像描述

于 2021-08-11T08:20:47.570 回答
0

我知道这已经快 8 岁了,但我遇到了同样的问题,根据上面克里斯托夫的评论,我能够找到一种方法。

下面是我想要的图表:

在此处输入图像描述

然而,仅通过三元选择某些行并且与NaN没有很好的配合smooth freq,而且我的直方图是错误的(似乎箱是相互绘制的,并且频率没有达到应有的高)。

这不起作用:

plot \
    'filename' using ($1 >= 0 ? $1 : NaN) notitle smooth freq with boxes fillcolor rgb "green", \
    'filename' using ($1 <  0 ? $1 : NaN) notitle smooth freq with boxes fillcolor rgb "red"

在 gnuplot 5.4.2 的手册中,本节描述了一个实验功能,结合set table,使我能够实现上图。

[实验] 要仅选择数据点的子集进行制表,您可以在命令末尾提供输入过滤条件 (if )。请注意,输入过滤器可能会引用不属于输出的数据列。在出现在 gnuplot 的已发布版本中之前,此功能可能会发生重大变化。

plot <file> using 1:2:($4+$5) with table if (strcol(3) eq "Red")

-- p207 gnuplot v5.4.2 手册

所以方法是:

  • 用于set table $my_data_block_green设置下一个绘图命令输出到$my_data_block_green数据块。我们将为每种颜色创建一个数据块,这是第一个。
  • 用于plot <file> with table if (<condition_for_green>)仅将匹配的行写入绿色数据块<condition_for_green>
  • 使用set table $my_data_block_red(如第 1 点)。
  • 用于plot <file> with table if (<condition_for_red>)仅向红色数据块写入匹配的行<condition_for_red>
  • 停止将绘图命令写入带有unset table.
  • 正常绘图,引用数据块而不是<file>.

相关代码(不是上图的完整代码):

set table $db1
plot <filename> using 7:(1) with table if ($7 >= 0)

set table $db2
plot <filename> using 7:(1) with table if ($7 < 0)

unset table

plot \
    '$db1' using $1:.. .. fillcolor rgb "green", \
    '$db2' using $1:.. .. fillcolor rgb "red"

希望可以节省几分钟。

于 2021-08-08T06:11:59.030 回答