You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

gnuplot图例颜色规则解析:如何用lc变量设置默认点颜色?

Fixing Legend Color Mismatch in Gnuplot 6.0 with lc variable

Legend Color Behavior in Gnuplot

When using lc variable, gnuplot generates one legend entry per plot command, using the first valid color value it processes from your dataset for that command. It does not automatically create multiple legend entries for different color values within a single plot call—even if your color expression produces distinct color ranges.

In your modified command, combining men and women into one plot means the legend only reflects the color of the first data point that passes your filter ($5 == 1 && $6 == 2). If the first valid point is male ($19 == 0), the legend uses $6 (e.g., value 2), while female points (using $6 + 4, e.g., value 6) don’t get their own entry. When filtering to $6 = 3, the legend appears "normal" because you’re working with a single base color value, but it still only shows one entry (whichever color is processed first).

Solution 1: Split into Separate Plot Commands

To get distinct legend entries for men and women, split your plot into two separate plot calls, each targeting one gender with explicit logic and titles:

# Plot men (V1=0) with original $6 color
plot "file.txt" u 2:($5 == 1 && $6 == 2 && $19 == 0 ? $10 : 1/0):($6) t "p12 Men" with points pt 7 ps 0.4 lc variable, \
# Plot women (V1=1) with shifted color ($6+4)
     "file.txt" u 2:($5 == 1 && $6 == 2 && $19 == 1 ? $10 : 1/0):($6+4) t "p12 Women" with points pt 7 ps 0.4 lc variable

Each command generates its own legend entry, using the first valid color value for that subset, which correctly represents their respective color ranges.

Solution 2: Manual Custom Legend

If you need to keep a single plot command but show multiple colors in the legend, manually add entries using set key add:

# Run your combined plot command without a title
plot "file.txt" u 2:($5 == 1 && $6 == 2 ? $10 : 1/0):($19 == 0 ? $6 : $6+4) t "" with points pt 7 ps 0.4 lc variable

# Add custom legend entries with correct colors
set key add entry "p12 Men" pt 7 ps 0.4 lc 2  # Replace 2 with your actual male color value
set key add entry "p12 Women" pt 7 ps 0.4 lc 6 # Replace 6 with your actual female color value
set key show

This lets you define legend entries with accurate colors even when using a single plot command.


内容的提问来源于stack exchange,提问作者revher

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.01 19:14:52