如何将字符串映射到RGB值?

发布于 2025-01-30 12:18:20 字数 489 浏览 1 评论 0 原文

数据文件如下:

# ID,Value, Region
A,30,North
B,26,North
C,49,South
D,55,East
...

在这里,我想将字符串(例如 region )映射到RGB值,该值将用作 lc color BoxXy 。它可以描述为伪代码:

if (region eq "North") {
   "0x1b9e77"
} else if (region eq "South") {
   "0xd95f02"
} else if (region eq "East") {
   "0x7570b3"
} else  {
   "0xe7298a"
}

如何在gnuplot中获得此类映射?实际上,在这里,使用选项将 $ 3 映射到中的十六进制字符串。

The data file is like the following:

# ID,Value, Region
A,30,North
B,26,North
C,49,South
D,55,East
...

Here I would like to map the string (e.g., Region) to a rgb value which will be used as the lc color for boxxy. It can be described as the pseudo-code:

if (region eq "North") {
   "0x1b9e77"
} else if (region eq "South") {
   "0xd95f02"
} else if (region eq "East") {
   "0x7570b3"
} else  {
   "0xe7298a"
}

How can I achieve such mapping in gnuplot? Here, in fact, it is to map $3 to a hex string in using option.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜爱皱眉﹌ 2025-02-06 12:18:20

您可以在伪代码上定义基于映射功能:

colormapper(x)=(x eq "North"?0x1b9e77:(x eq "South"?0xd95f02:(x eq"East"?0x7570b3:0xe7298a)))

之后,可以将其应用于列。

plot 'map.dat' u 1:2:(colormapper(stringcolumn(3))) pt 7 ps 3 lc rgb variable notitle

解决方案的关键时刻:

  • 使用 x eq” north“ ... ,因为我们比较字符串
  • lc rgb变量让我们控制 使用 string> StringColumn(3)而不是 $ 3 ,可以使用变量(无代码> RGB 您可以访问标准行颜色)的线颜色(无代码> rgb )
  • 非常重要。

You can define a mapping function basing on your pseudocode:

colormapper(x)=(x eq "North"?0x1b9e77:(x eq "South"?0xd95f02:(x eq"East"?0x7570b3:0xe7298a)))

after that, you can apply it on your column.

plot 'map.dat' u 1:2:(colormapper(stringcolumn(3))) pt 7 ps 3 lc rgb variable notitle

Sample based on my data

The key moments of the solution:

  • Using x eq "North"... because we compare strings
  • lc rgb variable let us to control the line color by a variable (without rgb you can access to the standard line colors)
  • Using stringcolumn(3) instead of $3 is very important.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文