Domanda

I am trying to set textcolor property of a label in gnuplot to transition through a palette of colours.
To be more precise, I want each letter of the label, say "Number of Connections", to be a different color but following the color palette I specify.
I tried using the following method, but it failed, using only the color in the middle of the range for the string.

set palette model RGB defined ( \
0 '#F46D43',\
1 '#FDAE61',\
2 '#FEE08B',\
3 '#E6F598',\
4 '#ABDDA4',\
5 '#66C2A5' )
set y2label "Number of Connections" textcolor palette
È stato utile?

Soluzione

Unfortunately, gnuplot can only color the entire string "Number of Connections". You can influence the color using the additional frac option.
However, here's a way to achieve what you were looking for. It involves some manual settings, though, as I'll explain below:

# define the location of your plot:
bm = 0.15
lm = 0.12
rm = 0.75
tm = 0.90

# letter spacing - play with this as needed:
STRDIST = 0.03

# set up the plot window:
set lmargin at screen lm
set rmargin at screen rm
set bmargin at screen bm
set tmargin at screen tm

# place the colorbar in a defined location:
set colorbox vertical user origin rm+0.1,0.15 size .05,tm-bm

# define your palette:
set palette model RGB defined ( \
0 '#F46D43',\
1 '#FDAE61',\
2 '#FEE08B',\
3 '#E6F598',\
4 '#ABDDA4',\
5 '#66C2A5' )

# your label
LABEL = "Number of Connections"
# the 'length' of LABEL, unfortunately counted manually:
LEN_LABEL = 21.0 # IMPORTANT, declare as float

# use a loop to individually place each char of the string on the plot:
do for [i=1:LEN_LABEL]{\
set label i LABEL[i:i] at screen 0.8,bm+((i-1.)*STRDIST) \
    rotate by 90 textcolor palette frac i/LEN_LABEL\
}

# dummy function plot (so that there's something to see):
plot '+' using ($1):(sin($1)):(0.5*(1.0+sin($1))) w l lw 3 lc pal not

What is going on:

  1. Define the location of your plot and of the colorbar: That way you will know exactly where they are and can place a "pseudo"-label accurately.
  2. The variable STRDIST is used to space the individual letters. This is clumsy, but you get the gist. Play with it to achieve good results.
  3. Unfortunately, it seems that gnuplot cannot compute the length of a string, so I hard-wired it, LEN_LABEL.
  4. Use a do for-loop to place each letter of the label string on the plot, assigning a color from the color palette using the additional frac option. frac 0.0 is the lowest and frac 1.0 the "highest" color on the color palette. Here, we exploit the loop-counter to give evenly spaced colors from the palette. Note: This is why it is important to declare LEN_LABEL as a float, not integer or everything but the last iteration will result in frac 0.
  5. The plot '+' ... command is borrowed from this site.

The plot you get when you copy/paste the above example looks like this:

colorful label

Play with the starting point of the "label" as well as the STRDIST to generate/place a label of your liking.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top