【问题标题】:Fill the area between two curves in gnuplot填充gnuplot中两条曲线之间的区域
【发布时间】:2020-12-14 11:32:40
【问题描述】:

我的两个功能:

f(x) = x**2
g(x) = -(x-4)*x

我需要计算这两条曲线之间的面积,因为我想要可视化。 WolframAlpha 正是我想要的:https://www.wolframalpha.com/input/?i=area+between+the+curves+y%3Dx%5E2+and+y%3D-x%5E2%2B4x,但是,我需要在 gnuplot 中制作图形。到目前为止,这是我的代码:

set border linewidth 1
set style line 1 linecolor rgb '#0060ad' linetype 1 linewidth 1
set style line 2 linecolor rgb '#dd181f' linetype 1 linewidth 1
set key at 6.1,1.3
set xlabel 'x'
set ylabel 'y'
set xrange [-0.5:3]
set yrange [0:5]
set xtics 2
set ytics 2
set tics scale 2
f(x) = x**2
g(x) = -(x-4)*x
set grid ytics lt 0 lw 1 lc rgb "#bbbbbb"
set grid xtics lt 0 lw 1 lc rgb "#bbbbbb"
plot f(x) title 'f(x)' with lines linestyle 1, \
     g(x) title 'g(x)' with lines linestyle 2

它产生这个输出:

有什么方法可以像 WolframAlpha 那样突出显示这两条曲线之间的区域?假设我们知道它们相交的位置(0 和 2),除非 gnuplot 可以自行完成。

如果这不是 vanilla gnuplot 能够做到的,我深表歉意,在这种情况下,一些可以做到这一点的资源的指针将不胜感激。

【问题讨论】:

  • 你检查过gnuplot手册和help filledcurves吗?还有这个:gnuplot.sourceforge.net/demo_5.4/fillcrvs.html
  • @theozh 不幸的是,由于我对 gnuplot 很陌生,所以我无法理解它。不过,我认为你只能填充一条曲线下的区域,而我想填充两条曲线之间的区域。

标签: plot latex gnuplot


【解决方案1】:

这可以使用filledcurves 来实现(参见文档:http://gnuplot.sourceforge.net/docs_4.2/node245.html)。您只需要使用below 选项来确保两条曲线之间的区域被填充。

举个例子,试试这个:

plot '+' using 1:(f($1)):(g($1)) title '' with filledcurves below lc rgb 'green', \
    f(x) title 'f(x)' with lines linestyle 1, \
    g(x) title 'g(x)' with lines linestyle 2

这是基于它在常见问题解答中描述的两条曲线的方式:http://www.gnuplot.info/faq/#x1-460005.3

【讨论】:

  • 谢谢,这有点工作。两者之间的区域现在充满了绿色。但是,'+' using 1:(f($1)):(g($1)) 在右上角显示为字符串。这是当前的情节:i.imgur.com/3kzxslX.png 这是预期的行为,还是我做错了什么,如果是,我该如何关闭该标签?
  • 尝试添加title '',更新示例。