1

i want to use variables or arrays in plot command with for loop as follows

style[1]= "lines lt 4 lw 2"
style[2] = "points lt 3 pt 5 ps 2"
....

title[1]= "first title"
title[2]= "second title "
...

or

style="'lines lt 4 lw 2' 'points lt 3 pt 5 ps 2'"
title="'first title' 'second title'"

plot command

plot for [i=1:1000] 'data.txt' u 1:2 title title[i] with style[i]
plot for [i=1:1000] 'data.txt' u 1:2 title word(title,i) with word(style,i)

I was successful in the title part, but not in the with part. I realized that the problem was caused by quotes.

i=1
plot 'data.txt' u 1:2 title "first title" with "lines lt 4 lw 2"

When I use the array and word attributes, I get an error due to a quote error. I tried with sprintf but still no success.

sprintf("with %s", style'.i.')

I hope I could explain correctly. Any idea how I solve this? or how can i remove the quotes. Many thanks.

  • Welcome to StackOverflow! I guess you cannot simply change the plotting style in a `plot for` loop, but you can change the linestyle. Maybe this https://stackoverflow.com/a/63699595/7295599 or this https://stackoverflow.com/a/61692635/7295599 is helpful to you. – theozh May 28 '21 at 14:08
  • Thanks @theozh. I will carefully check the links you post. Thanks for your reply – sorupcikicam May 28 '21 at 14:43

2 Answers2

2

If the styles you want to select from consist only of lines and/or points then you can do this by defining the corresponding linestyle and then selecting it by number.

set style line 1 lt 4 lw 2 pt 0               # lines only, no points
set style line 2 lt 3 lw 0.001 pt 5 ps 2      # points only, no lines
array Title[2] = ["style 1", "style 2"]

plot for [i=1:2] sin(x/i) with linespoints ls i title Title[i]

Note: It probably won't work to suppress the lines by setting lw to exactly 0, as many terminals interpret this as "thinnest possible line" rather than "no line". So instead we set it to something very very thin that hopefully will not be visible.

enter image description here

If the styles you want to select from include something other than lines+points, then no I don't think it is possible to use this trick.

Ethan
  • 13,715
  • 2
  • 12
  • 21
  • Thanks for your answer @Ethan. probably this method works for me. I learned that we cannot easily return a value under the plot command. – sorupcikicam May 28 '21 at 17:23
0

You can try to build a complete plot command as a string and eval it afterwards:

max_i = 2
array title[max_i]
array style[max_i]

style[1]= "lines lt 4 lw 2"
style[2] = "points lt 3 pt 5 ps 2"

title[1]= "first title"
title[2]= "second title"


cmd="plot NaN notitle"  # dummy for starting iteration
do for [i=1:max_i] {
   # append the explicit plot command
   cmd = sprintf("%s, sin(x*%d) title \"%s\" with %s", cmd, i, title[i], style[i])
}
print cmd  # just for checking the final plot command

eval cmd   # run plot command

It might be slow (or even not work at all) for 1000 lines. But I would be a bit scared of having 1000 lines and titles within a single diagram, anyhow.

maij
  • 4,094
  • 2
  • 12
  • 28
  • Thanks @maij. Yesss this is almost exactly what I wanted. I guessed the solution was related to **sprintf** and **eval**. But i haven't been able to solve it. Thank you for your answer. – sorupcikicam May 29 '21 at 00:12