gnuplot:在一个字符串列中绘制基于颜色的值的点,并在图例中显示字符串

时间:2012-01-31 16:08:05

标签: graph plot gnuplot

我想绘制分类结果并标记真正的类。所以,基本上我需要的是为字符串列中的每个点指定颜色。

数据集如下所示:

  

5.1 3.5 1.4 0.2 Iris-setosa

我最终得到了以下解决方案的脚本(感谢这里的答案:How to make points one color when a third column equals zero, and another color otherwise, in Gnuplot?

set palette model RGB defined (0 "red",1 "blue", 2 "green")
plot 'iris.data' using 1:2:5 notitle with points pt 2 palette
原始数据集中的

我用数字替换了字符串标签,因为我不知道如何使用gnuplot中的字符串。有没有办法将字符串映射到颜色?

目前输出如下: gnuplot coloring points

但是我不喜欢渐变调色板,因为在这种情况下它没有意义。我更喜欢普通的传奇,只有一种颜色和类的名称。知道怎么做吗?

3 个答案:

答案 0 :(得分:5)

如何使用awk

使用数据文件Data.csv

5.4452 4.6816 blue
1.2079 9.4082 red
7.4732 6.5507 red
2.3329 8.2996 red
3.4535 2.1937 green
1.7909 2.5173 green
2.5383 7.9700 blue

和这个脚本:

set pointsize 3
plot "< awk '{if($3 == \"red\") print}' Data.csv" u 1:2 t "red" w p pt 2, \
     "< awk '{if($3 == \"green\") print}' Data.csv" u 1:2 t "green" w p pt 2, \
     "< awk '{if($3 == \"blue\") print}' Data.csv" u 1:2 t "blue" w p pt 2

你得到这个情节:

enter image description here

awk做的只是检查数据文件的第三个参数,如果它有一些值,则只打印该行:如红色或蓝色。

您还可以使用渐变来消除调色板。

使用gnuplot iterations可以进一步改进脚本。

答案 1 :(得分:2)

调色板可用于获取任何点颜色(针对各个点)

plot file using 1:2:3 with points palette

现在设置一个调色板,为您提供所需的色阶。您可以设置调色板以计算颜色,例如使用HSV颜色模型或查看gnuplot演示网站。

为了完整起见,请查看gnuplot pm3d colors demo page上的最后一个示例:

color bar with distinct colors in gnuplot

theta(x) = x<0 ? 0 : 1
r(x) = 4*x*(1-theta(x-0.25))
g(x) = 0.5*theta(x-0.25)*(1-theta(x-0.5))
b(x) = x
set palette model RGB functions r(gray),g(gray),b(gray)
set title "set palette model RGB functions\n4*x*(1-theta(x-0.25)), 0.5*theta(x-0.25)*(1-theta(x-0.5)), x"
splot f(x)enter code here

答案 2 :(得分:0)

将字符串映射到颜色或调色板索引可以在gnuplot中本地完成,关键是使用stringcolumn()和用户定义的映射函数。

以下是使用调色板的示例:

#!/usr/bin/gnuplot -persist

# define a palette with an exact number of colors
set palette maxcolors 3
set palette model RGB defined ( \
  0 "red", \
  1 "blue", \
  2 "green")

# Define palette labels, the range matches the number of colors defined above
set cbrange [0:3]
set cbtics offset 0,+4 ( \
  'color1' 0, \
  'color2' 1, \
  'color3' 2, \
  '' 3)

# define a function to map strings to palette indices
map_color(string) = ( \
  string eq 'color1' ? 0 : \
  string eq 'color2' ? 1 : \
  string eq 'color3' ? 2 : \
  3)

plot '-' using 1:2:(map_color(stringcolumn(3))) notitle with points pt 2 palette
5.4452 4.6816 color1
1.2079 9.4082 color2
7.4732 6.5507 color2
2.3329 8.2996 color2
3.4535 2.1937 color3
1.7909 2.5173 color3
2.5383 7.9700 color1
EOF

enter image description here

可以采用类似的方法直接将颜色映射到

#!/usr/bin/gnuplot -persist

# define a function to map strings to palette indices
map_color(string) = ( \
  string eq 'color1' ? 0xff0000 : \
  string eq 'color2' ? 0x0000ff : \
  string eq 'color3' ? 0x00ff00 : \
  0x000000)

plot '-' using 1:2:(map_color(stringcolumn(3))) notitle with points pt 2 lc rgbcolor variable
5.4452 4.6816 color1
1.2079 9.4082 color2
7.4732 6.5507 color2
2.3329 8.2996 color2
3.4535 2.1937 color3
1.7909 2.5173 color3
2.5383 7.9700 color1
EOF