Gnuplot combing multiple value types in one line graph with different colours and dashes

假如想象 提交于 2021-01-29 10:03:18

问题


I have three different lines, where colours and their dashes means different things which I want on one plot, instead of three. How do I accomplish that?

set datafile separator comma
$sample <<EOD
2020-01-01,4,UK,Business
2020-02-01,4,UK,Business
2020-01-01,4,UK,Social
2020-02-01,15,UK,Social
2020-01-01,1,USA,Social
2020-02-01,25,USA,Social
EOD

set format x '%Y'
set xdata time
set timefmt "%Y-%m-%d"
plot '$sample' u 1:2 title "UK/Business" with linespoints dt 3 lw 5 pt 7 lc "red"
  • plot '$sample' u 1:2 title "USA/Social" with linespoints dt 3 lw 1 pt 7 lc "blue"
  • plot '$sample' u 1:2 title "UK/Social" with linespoints dt 3 lw 5 pt 7 lc "blue"

E.g. blue is "Social" and lw 1 (fine dots) is for USA.


回答1:


Please check the manual or help plot. Plot elements for the same plot are separated by comma.

Syntax:

  plot {<ranges>} <plot-element> {, <plot-element>, <plot-element>}

To improve readability you can split long code lines into several lines by using \. Note that no character is allowed after \ in the same line except carriage return/line feed.

Try this:

plot '$sample' u 1:2 title "UK/Business" w lp dt 3 lw 5 pt 7 lc "red", \
     '' u 1:2 title "USA/Social" w lp dt 3 lw 1 pt 7 lc "blue", \
     '' u 1:2 title "UK/Social" w lp dt 3 lw 5 pt 7 lc "blue"

Addition: (a filter depending on two (string)-columns)

You can implement a filter by using the ternary operator. Check help ternary and help strcol. A value which does not pass the filter will be set to NaN. If you still want to your points being connected with lines, you need to set datafile missing NaN.

Code:

### filtered data with different line colors
reset session
set datafile separator comma

$sample <<EOD
2020-01-01,4,UK,Business
2020-02-01,4,UK,Business
2020-01-01,4,UK,Social
2020-02-01,15,UK,Social
2020-01-01,1,USA,Social
2020-02-01,25,USA,Social
EOD

myTimeFmt = "%Y-%m-%d"
set format x '%d.%m.%Y' timedate
set key top left
set datafile missing NaN

myFilter(colData,colFilter1,key1,colFilter2,key2) = (strcol(colFilter1) eq key1) && (strcol(colFilter2) eq key2) ?  column(colData) : NaN

plot $sample u (timecolumn(1,myTimeFmt)):(myFilter(2,3,"UK",4,"Business")) w lp dt 3 lw 5 pt 7 lc "red" title "UK/Business", \
     '' u (timecolumn(1,myTimeFmt)):(myFilter(2,3,"USA",4,"Social")) w lp dt 3 lw 1 pt 7 lc "blue" title "USA/Social" , \
     '' u (timecolumn(1,myTimeFmt)):(myFilter(2,3,"UK",4,"Social")) w lp dt 3 lw 5 pt 7 lc "blue" title "UK/Social"
### end of code

or a bit shortened if the columns 2,3,4 do not change within the plot command...

...
...
myTime(col) = timecolumn(col,myTimeFmt)
myFilter(key1,key2) = (strcol(3) eq key1) && (strcol(4) eq key2) ?  column(2) : NaN

plot $sample u (myTime(1)):(myFilter("UK","Business")) w lp dt 3 lw 5 pt 7 lc "red" title "UK/Business", \
     '' u (myTime(1)):(myFilter("USA","Social")) w lp dt 3 lw 1 pt 7 lc "blue" title "USA/Social" , \
     '' u (myTime(1)):(myFilter("UK","Social")) w lp dt 3 lw 5 pt 7 lc "blue" title "UK/Social"

Result:



来源:https://stackoverflow.com/questions/63572115/gnuplot-combing-multiple-value-types-in-one-line-graph-with-different-colours-an

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