Gnuplot文本颜色调色板

3

我正在尝试设置gnuplot标签的textcolor属性,使其过渡到一系列颜色。
更准确地说,我希望标签的每个字母,例如"连接数",都是不同的颜色,但要遵循我指定的颜色调色板。
我尝试使用以下方法,但它失败了,只为字符串使用范围中间的颜色。

set palette model RGB defined ( \
0 '#F46D43',\
1 '#FDAE61',\
2 '#FEE08B',\
3 '#E6F598',\
4 '#ABDDA4',\
5 '#66C2A5' )
set y2label "Number of Connections" textcolor palette

嗨@Schorsch,我想让y轴上的标签颜色随着调色板而改变,例如,变成这些之一http://gnuplot.sourceforge.net/demo/pm3dcolors.html。因此,每个字母的颜色都与另一个不同,但是遵循整体模式,就像其中一个图中所示。 - Nikolay Melnikov
谢谢澄清。这很有趣,我有一个想法,但可能需要一点时间。 - Schorsch
有一个例子:类似于这里的文本 http://vps2.etotheipiplusone.com:30176/redmine/attachments/13(直接)。 - Nikolay Melnikov
Nikolay,我花了一些时间,但请看下面我的答案。 - Schorsch
1个回答

3

很不幸,gnuplot 只能给整个字符串"Number of Connections"上色。您可以使用附加的frac选项来影响颜色。
但是,下面是一种实现您所需的方法。尽管需要一些手动设置,我将在下面进行说明:

# define the location of your plot:
bm = 0.15
lm = 0.12
rm = 0.75
tm = 0.90

# letter spacing - play with this as needed:
STRDIST = 0.03

# set up the plot window:
set lmargin at screen lm
set rmargin at screen rm
set bmargin at screen bm
set tmargin at screen tm

# place the colorbar in a defined location:
set colorbox vertical user origin rm+0.1,0.15 size .05,tm-bm

# define your palette:
set palette model RGB defined ( \
0 '#F46D43',\
1 '#FDAE61',\
2 '#FEE08B',\
3 '#E6F598',\
4 '#ABDDA4',\
5 '#66C2A5' )

# your label
LABEL = "Number of Connections"
# the 'length' of LABEL, unfortunately counted manually:
LEN_LABEL = 21.0 # IMPORTANT, declare as float

# use a loop to individually place each char of the string on the plot:
do for [i=1:LEN_LABEL]{\
set label i LABEL[i:i] at screen 0.8,bm+((i-1.)*STRDIST) \
    rotate by 90 textcolor palette frac i/LEN_LABEL\
}

# dummy function plot (so that there's something to see):
plot '+' using ($1):(sin($1)):(0.5*(1.0+sin($1))) w l lw 3 lc pal not

发生了什么事:
  1. 定义图的位置和颜色条的位置:这样你就可以精确地知道它们在哪里,并可以准确地放置“伪”标签。
  2. 变量STRDIST用于间隔每个字母。这很笨拙,但你可以尝试调整以获得良好的结果。
  3. 不幸的是,似乎gnuplot无法计算字符串的长度,因此我硬编码了它,LEN_LABEL
  4. 使用do for循环将标签字符串的每个字母放置在图中,并使用附加的frac选项从颜色板中分配颜色。frac 0.0是颜色板上最低的颜色,frac 1.0是“最高”的颜色。在这里,我们利用循环计数器从调色板中给出均匀分布的颜色。注意:这就是为什么将LEN_LABEL声明为浮点数很重要,而不是整数或除了最后一次迭代之外的所有迭代都将导致frac 0
  5. plot '+' ...命令来自这个网站

当你复制/粘贴上面的示例时,得到的图形如下:

colorful label

尝试调整“标签”的起点以及STRDIST来生成/放置您喜欢的标签。


谢谢!这正是我所需要的!太好了。你给我提供的关于“+”解释的博客帖子也非常有启发性。 - Nikolay Melnikov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接