Can I have multiple colors in a gnuplot label?

匿名 (未验证) 提交于 2019-12-03 01:00:01

问题:

I have this graph in gnuplot:

I want to include totals for each of the three colored line graphs. One possibility is to replace the key with this:

Can I plot this with a single label that switches colors? If not then I'd have to use six labels. In that case, how do I determine the coordinates of the labels given that the widths of the strings can vary? I could use a fixed width font and do some computation based on the number of digits in each of the totals but this seems tedious.

Is there a more clever way to indicate totals in a graph?

回答1:

You cannot change the textcolor inside a single label, you must use six different labels. However, there are some ways to simplify this:

  • Use left and a negative offset in x-direction for the colored label, and right and a positive offset for the number, like

    set label 1 right at graph 0.5, char 1 "FY2013" tc lt 1 offset char -0.5,0 set label 2 left at graph 0.5, char 1 "34,674" offset char 0.5,0 
  • Use set macros and define a string variable anchor="graph 0.5, char 1" which you use to set the anchor point of all labels.

    set macros anchor="graph 0.5, char 1" set label 1 at @anchor "FY2013" tc lt 1 offset char -0.5,0 
  • Shift all labels around a single anchor point, and parametrize the offsets from this point using two variables:

    set macros anchor="graph 0.5, char 1" ofs_x = 0.5 dx = 20 set label 1 right at @anchor "FY2013" tc lt 1 offset char -dx - ofs_x,0 set label 2 left at @anchor "34,674" offset char -dx + ofs_x,0 set label 3 right at @anchor "FY2014" tc lt 2 offset char -ofs_x,0 set label 4 left at @anchor "16,240" offset char ofs_x,0 set label 5 right at @anchor "FY2015" tc lt 3 offset char dx - ofs_x,0 set label 6 left at @anchor "6,863" offset char dx + ofs_x,0  set bmargin 3.5 plot -x 

That still isn't a fully automatic solution, but boils down to choosing appropriate values for the anchor and dy only.



回答2:

You can use enhanced text mode to overprint differently coloured labels:

set label 1 at 0,0 textcol rgb "red" "ONE" set label 2 at 0,0 textcol rgb "blue" "&{ONE}TWO" set label 3 at 0,0 textcol rgb "green" "&{ONETWO}THREE" 

Any text that is inside the brackets in &{SPACE} is replaced by an empty space with the width of the text "SPACE".

In gp versions <5 you need to enable enhanced text mode first, it is by default since 5.0

update: set xtics works similar in principle, but you cannot set two labels a the same position, they replace each other, and the textcolor option is valid for the whole axis. So this doesn't work:

set xtics left ("ONE"  1) textcol rgb "red" set xtics add ("&{ONE}TWO"  1.00001) textcol rgb "blue" set xtics add ("&{ONETWO}THREE"  1.00002) textcol rgb "green" 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!