Gnuplot在一个折线图中用不同的颜色和破折号组合多种值类型 [英] Gnuplot combing multiple value types in one line graph with different colours and dashes
问题描述
我有3条不同的线,其中的颜色及其破折号表示我希望在一张图上显示的不同东西,而不是3条.我该怎么做?
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标题"USA/Social";带有线点dt 3 lw 1 pt 7 lc"blue"
-
plot'$ sample'u 1:2标题"UK/Social"带有线点dt 3 lw 5 pt 7 lc蓝色"
例如蓝色是社交",lw 1(细点)用于美国.
E.g. blue is "Social" and lw 1 (fine dots) is for USA.
推荐答案
请检查手册或帮助图
.同一图的图元素用逗号分隔.
Please check the manual or help plot
. Plot elements for the same plot are separated by comma.
语法:
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.
尝试一下:
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)
您可以使用三元运算符实现过滤器.选中 help三元
和 help strcol
.未通过过滤器的值将被设置为 NaN
.如果仍然希望将点与线连接起来,则需要设置缺少NaN的数据文件
.
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
.
代码:
### 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
或者如果在plot命令中第2、3、4列没有变化的话,请缩短一点...
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"
结果:
这篇关于Gnuplot在一个折线图中用不同的颜色和破折号组合多种值类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!