如何按列值为散点着色并显示图例?gnuplot实现疑问
问题原因与解决方案
为什么图例不显示?
你的代码里用了notitle参数,并且将所有数据作为单个绘图系列处理(通过lc variable动态指定线型)。gnuplot的图例默认是为每个plot子命令生成条目,单条命令加notitle自然不会生成任何图例内容。
两种纯gnuplot实现方案
方案1:按类别拆分绘图系列(自动生成图例)
直接筛选每个标签对应的数据,分系列绘制,每个系列指定标题,gnuplot会自动生成对应图例:
set linetype 1 linecolor 'red' pt 5 # C3_V1对应红色 set linetype 2 linecolor 'blue' pt 5 # C3_V2对应蓝色 set linetype 3 linecolor 'green' pt 5 # C3_V3对应绿色 set linetype 4 linecolor 'black' pt 5 set key right top set xrange [0:200] set yrange [0:200] plot "gnuplot_data.txt" using 1:2:(stringcolumn(3) eq "C3_V1") with points lt 1 title "C3_V1", \ "" using 1:2:(stringcolumn(3) eq "C3_V2") with points lt 2 title "C3_V2", \ "" using 1:2:(stringcolumn(3) eq "C3_V3") with points lt 3 title "C3_V3" pause -1
注:using中的第三个表达式是筛选条件,仅满足条件的点会被绘制。
方案2:保留单条绘图命令,手动添加图例
如果不想拆分绘图命令,可通过set key add手动指定图例条目:
set linetype 1 linecolor 'red' pt 5 # C3_V1对应红色 set linetype 2 linecolor 'blue' pt 5 # C3_V2对应蓝色 set linetype 3 linecolor 'green' pt 5 # C3_V3对应绿色 set linetype 4 linecolor 'black' pt 5 set key right top # 手动添加图例标签与对应线型的映射 set key add ( "C3_V1", 1, "C3_V2", 2, "C3_V3", 3 ) set xrange [0:200] set yrange [0:200] plot "gnuplot_data.txt" using 1:2:(stringcolumn(3) eq "C3_V1" ? 1 : stringcolumn(3) eq "C3_V2" ? 2 : stringcolumn(3) eq "C3_V3" ? 3 : 4) with points lc variable notitle pause -1
内容的提问来源于stack exchange,提问作者user2150989
相关产品推荐
相关产品推荐

