英文:
gnuplot: change dashtype of line, depending on x-value
问题
我想绘制一条线,其中 x 值由年份定义。实际值应该由实线连接,但预测值只能用虚线表示。
我的数据如下:
x | y |
---|---|
1980 | 5 |
1990 | 10 |
2000 | 17 |
2010 | 27 |
2020 | 39 |
2022 | 44 |
2030 | 60 |
要获得图表,我使用了以下代码:
plot 'input.dat' using 1:2 with lines dashtype 1
但是当然,整条线都是实线。有没有办法为给定的 x 范围定义不同的线型,例如:
plot 'input.dat' using 1:2 with lines from 1980 dashtype 1 from 2022 dashtype 0
我认为一个可能的解决方案是使用多个绘图并绘制 2 条线,但这似乎有点过度,也许有一个更简单的解决方案?
英文:
I want to draw a line, where x-values are defined by years. The actual values should be a connect by a solid line, but the forecast only as dashed line.
My data would be
x | y |
---|---|
1980 | 5 |
1990 | 10 |
2000 | 17 |
2010 | 27 |
2020 | 39 |
2022 | 44 |
2030 | 60 |
To get the graph i use but of
plot 'input.dat' using 1:2 with lines dashtype 1
but of course, the whole line is solid.
Is there a way to define different dashtypes for a given xrange, e.g. something like
plot 'input.dat' using 1:2 with lines from 1980 dashtype 1 from 2022 dashtype 0
I think a possible solution would be to use multiplots and draw 2 lines, but that seems to be an overkill, maybe there is a simpler solution?
答案1
得分: 1
I think i found a simpler solution than multiplots with the help of https://stackoverflow.com/questions/33337394/how-to-plot-line graphs with different x range in one figure using gnuplot
predicted(x) = (x >= 2022 ? x : 1/0)
actual(x) = (x > 2022 ? 1/0 : x)
plot 'input.dat' using (actual($1)):2 with lines dashtype 1, '' using predicted($1)):2 with lines dashtype 0
英文:
I think i found a simpler solution than multiplots with the help of https://stackoverflow.com/questions/33337394/how-to-plot-line-graphs-with-different-x-range-in-one-figure-using-gnuplot
predicted(x) = (x >= 2022 ? x : 1/0)
actual(x) = (x > 2022 ? 1/0 : x)
plot 'input.dat' using (actual($1)):2 with lines dashtype 1, '' using predicted($1)):2 with lines dashtype 0
答案2
得分: 0
在gnuplot中,你有linecolor
、pointtype
和pointsize
这些可变属性(查看help points
),但(还?)没有可变的dashtype
和linewidth
。
不过,你可以使用可变的箭头样式来控制矢量的不同样式,包括不同的dashtype
、linewidth
和linecolor
,尽管在绘图命令中需要一些额外的工作。你可以从起始点绘制矢量,使用一些dx
和dy
。因此,你需要从当前点(x1, y1
)和前一个点(x0, y0
)的差值来计算这些dx
和dy
。
脚本:(适用于gnuplot >= 5.0.0)
### 模拟可变的dashtype和可变的linewidth
reset session
$Data <<EOD
x y
1980 5
1990 10
2000 17
2010 27
2020 39
2022 44
2030 60
EOD
set style arrow 1 dt 1 lw 2 lc "web-green" nohead
set style arrow 2 dt 3 lw 4 lc "red" nohead
set grid x,y
set key noautotitle
plot x1=y1=NaN $Data u (x0=x1,x1=$1,x0):(y0=y1,y1=$2,y0): \
(x1-x0):(y1-y0):((x1>2022)+1) w vec arrowstyle var
### 脚本结束
结果:
英文:
In gnuplot you have variable linecolor, pointtype and pointsize (check help points
), but not (yet?) variable dashtype and linewidth.
However, you have variable arrowstyle for vectors.
With this, you can set different styles with different dashtypes, linewidths and linecolor, however, at the cost of some extra effort in the plot command. You can plot vectors from a starting point with some dx
and dy
. So, you have to calculate this dx
and dy
from the difference of the current (x1,y1
) and the previous (x0,y0
) point.
Script: (works for gnuplot>=5.0.0)
### mimic variable dashtype and variable linewidth
reset session
$Data <<EOD
x y
1980 5
1990 10
2000 17
2010 27
2020 39
2022 44
2030 60
EOD
set style arrow 1 dt 1 lw 2 lc "web-green" nohead
set style arrow 2 dt 3 lw 4 lc "red" nohead
set grid x,y
set key noautotitle
plot x1=y1=NaN $Data u (x0=x1,x1=$1,x0):(y0=y1,y1=$2,y0): \
(x1-x0):(y1-y0):((x1>2022)+1) w vec arrowstyle var
### end of script
Result:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论