gnuplot: 根据 x 值更改线条的虚线类型

huangapple go评论125阅读模式
英文:

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

要获得图表,我使用了以下代码:

  1. plot 'input.dat' using 1:2 with lines dashtype 1

但是当然,整条线都是实线。有没有办法为给定的 x 范围定义不同的线型,例如:

  1. 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

  1. 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

  1. 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

  1. predicted(x) = (x >= 2022 ? x : 1/0)
  2. actual(x) = (x > 2022 ? 1/0 : x)
  3. 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

  1. predicted(x) = (x >= 2022 ? x : 1/0)
  2. actual(x) = (x > 2022 ? 1/0 : x)
  3. plot 'input.dat' using (actual($1)):2 with lines dashtype 1, '' using predicted($1)):2 with lines dashtype 0

答案2

得分: 0

在gnuplot中,你有linecolorpointtypepointsize这些可变属性(查看help points),但(还?)没有可变的dashtypelinewidth

不过,你可以使用可变的箭头样式来控制矢量的不同样式,包括不同的dashtypelinewidthlinecolor,尽管在绘图命令中需要一些额外的工作。你可以从起始点绘制矢量,使用一些dxdy。因此,你需要从当前点(x1, y1)和前一个点(x0, y0)的差值来计算这些dxdy

脚本:(适用于gnuplot >= 5.0.0)

  1. ### 模拟可变的dashtype和可变的linewidth
  2. reset session
  3. $Data <<EOD
  4. x y
  5. 1980 5
  6. 1990 10
  7. 2000 17
  8. 2010 27
  9. 2020 39
  10. 2022 44
  11. 2030 60
  12. EOD
  13. set style arrow 1 dt 1 lw 2 lc "web-green" nohead
  14. set style arrow 2 dt 3 lw 4 lc "red" nohead
  15. set grid x,y
  16. set key noautotitle
  17. plot x1=y1=NaN $Data u (x0=x1,x1=$1,x0):(y0=y1,y1=$2,y0): \
  18. (x1-x0):(y1-y0):((x1>2022)+1) w vec arrowstyle var
  19. ### 脚本结束

结果:

gnuplot: 根据 x 值更改线条的虚线类型

英文:

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)

  1. ### mimic variable dashtype and variable linewidth
  2. reset session
  3. $Data &lt;&lt;EOD
  4. x y
  5. 1980 5
  6. 1990 10
  7. 2000 17
  8. 2010 27
  9. 2020 39
  10. 2022 44
  11. 2030 60
  12. EOD
  13. set style arrow 1 dt 1 lw 2 lc &quot;web-green&quot; nohead
  14. set style arrow 2 dt 3 lw 4 lc &quot;red&quot; nohead
  15. set grid x,y
  16. set key noautotitle
  17. plot x1=y1=NaN $Data u (x0=x1,x1=$1,x0):(y0=y1,y1=$2,y0): \
  18. (x1-x0):(y1-y0):((x1&gt;2022)+1) w vec arrowstyle var
  19. ### end of script

Result:

gnuplot: 根据 x 值更改线条的虚线类型

huangapple
  • 本文由 发表于 2023年2月24日 16:30:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75554209.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定