自定义图例(或文本)gnuplot

时间:2013-11-27 14:38:10

标签: colors gnuplot legend

我有一个包含3列的文件,前两个是位置x y,第三个用于定义颜色,所以我有这样的颜色:

set palette model RGB defined ( 1 'black', 2 'blue', 3 'green', 4 'red')
unset colorbox

plot "file" u 2:1:3 w points pt 14 ps 2 palette, "file2" u 2:1:3 w points pt 14 ps 2        palette 

现在的问题是:是否有可能有这种点和颜色的正确传说? 由于这些点将具有不同的颜色(根据托盘),我想指定图例中每种颜色的含义。

我想到的唯一解决方案是在图中的某处写一些带有点的字符的文本(在本例中为第14页)并指定颜色......但这不是一个真正的解决方案吗?

所以请帮忙!

2 个答案:

答案 0 :(得分:6)

没有选择,你需要摆弄一下。这是YAGH(又一个gnuplot hack);)

假设您的值间隔相等,您可以使用'+'特殊文件名和labels绘图样式。

要仅显示自定义键,请考虑以下示例:

labels="first second third fourth"
set xrange[0:1] # must be set for '+'
set yrange[0:1]
set samples words(labels)   # number of colors to use
key_x = 0.8     # x-value of the points, must be given in units of the x-axis
key_y = 0.8
key_dy = 0.05
set palette model RGB defined ( 1 'black', 2 'blue', 3 'green', 4 'red')
unset colorbox
plot '+' using (key_x):(key_y + $0*key_dy):(word(labels, int($0+1))):0 \
    with labels left offset 1,-0.1 point pt 7 palette t ''

这给出了(用4.6.4):

enter image description here

由于set samples不会影响数据图,您可以将其直接集成到绘图命令中:

...
unset key
plot "file" u 2:1:3 w points pt 14 ps 2 palette, \
     "file2" u 2:1:3 w points pt 14 ps 2 palette, \
     '+' using (key_x):(key_y - $0*key_dy):(word(labels, int($0+1))):0 \
         with labels left offset 1,-0.1 point pt 14 ps 2 palette

但您需要设置正确的xrange,yrange以及key_xkey_ykey_dy的值。

这不是最直观的方式,但它有效:)

答案 1 :(得分:1)

我在这里发布了另一种解决方案: Using Gnuplot to plot point colors conditionally

基本上,您在没有图例条目的情况下绘制一次,然后为每个点颜色/标签创建虚拟图(没有数据)。

相关问题