gpt4 book ai didi

gnuplot 等高线图阴影线

转载 作者:行者123 更新时间:2023-12-04 14:22:06 25 4
gpt4 key购买 nike

我正在使用 gnuplot 绘制多个函数的等高线图。这是针对优化问题。
我有3个功能:

  • f(x,y)
  • g1(x,y)
  • g2(x,y)

  • 两者 g1(x,y)g2(x,y)是约束条件,想在 f(x,y) 的等高线图上绘制.

    这是教科书的例子:

    enter image description here

    感谢@theozh,这是我在 gnuplot 中复制它的尝试。
    ### contour lines with labels
    reset session

    f(x,y)=(x**2+y-11)**2+(x+y**2-7)**2
    g1(x,y)=(x-5)**2+y**2
    g2(x,y) = 4*x+y

    set xrange [0:6]
    set yrange [0:6]
    set isosample 250, 250
    set key outside

    set contour base
    set cntrparam levels disc 10,30,75,150,300,500,850,1500
    unset surface
    set table $Contourf
    splot f(x,y)
    unset table

    set contour base
    set cntrparam levels disc 26
    unset surface
    set table $Contourg1
    splot g1(x,y)
    unset table

    set contour base
    set cntrparam levels disc 20
    unset surface
    set table $Contourg2
    splot g2(x,y)
    unset table

    set style textbox opaque noborder

    set datafile commentschar " "
    plot for [i=1:8] $Contourf u 1:2:(i) skip 5 index i-1 w l lw 1.5 lc var title columnheader(5)
    replot $Contourg1 u 1:2:(1) skip 5 index 0 w l lw 4 lc 0 title columnheader(5)
    replot $Contourg2 u 1:2:(1) skip 5 index 0 w l lw 4 lc 0 title columnheader(5)

    enter image description here

    我想复制 gnuplot 示例中的教科书图片。如何在函数上做阴影标记 g1g2 ,上图中的粗黑线。

    @theozh 在下面提供了一个很好的解决方案。但是,该方法不适用于陡峭的曲线。举个例子
    reset session
    unset key

    set size square

    g(x,y) = -0.8-1/x**3+y

    set xrange [0:4]
    set yrange [0:4]
    set isosample 250, 250
    set key off

    set contour base
    unset surface

    set cntrparam levels disc 0
    set table $Contourg
    splot g(x,y)
    unset table

    set angle degree
    set datafile commentschar " "

    plot $Contourg u 1:2 skip 5 index 0 w l lw 2 lc 0 title columnheader(5)

    set style fill transparent pattern 4
    replot $Contourg u 1:2:($2+0.2) skip 5 index 0 w filledcurves lc 0 notitle

    产生下图。有没有办法使用不同的偏移量,例如 x < 1.3 的偏移 x 值和 x > 1.3 的偏移 y 值。这将产生更好的填充曲线。我正在寻找的 matlab 实现可以在这里找到: https://www.mathworks.com/matlabcentral/fileexchange/29121-hatched-lines-and-contours .

    enter image description here

    在复制@Ethans 程序时,我得到以下信息,与@Ethan 相比,dashtype 相对较粗,不知道为什么,我使用的是 gnuplot v5.2 和 wxt 终端。

    enter image description here

    当我复制@theozh 代码时,除了闭合轮廓外,它工作得很好,不知道为什么?见下面的例子:
    f(x,y)=x*exp(-x**2-y**2)+(x**2+y**2)/20
    g1(x,y)= x*y/2+(x+2)**2+(y-2)**2/2-2

    set xrange [-7:7]
    set yrange [-7:7]
    set isosample 250, 250
    set key outside

    set contour base
    unset surface

    set cntrparam levels disc 4,3.5,3,2.5,2,1.5,1,0.5,0
    set table $Contourf
    splot f(x,y)
    unset table

    set cntrparam levels disc 0
    set table $Contourg1
    splot g1(x,y)
    unset table

    # create some extra offset contour lines
    # macro for setting contour lines
    ContourCreate = '\
    set cntrparam levels disc Level; \
    set table @Output; \
    splot @Input; \
    unset table'

    Level = 0.45
    Input = 'g1(x,y)'
    Output = '$Contourg1_ext'
    @ContourCreate


    # Macro for ordering the datapoints of the contour lines which might be split
    ContourOrder = '\
    stats @DataIn skip 6 nooutput; \
    N = STATS_blank-1; \
    set table @DataOut; \
    do for [i=N:0:-1] { plot @DataIn u 1:2 skip 5 index 0 every :::i::i with table }; \
    unset table'

    DataIn = '$Contourg1'
    DataOut = '$Contourg1_ord'
    @ContourOrder

    DataIn = '$Contourg1_ext'
    DataOut = '$Contourg1_extord'
    @ContourOrder


    # Macro for reversing a datablock
    ContourReverse = '\
    set print @DataOut; \
    do for [i=|@DataIn|:1:-1] { print @DataIn[i]}; \
    set print'

    DataIn = '$Contourg1_extord'
    DataOut = '$Contourg1_extordrev'
    @ContourReverse

    # Macro for adding datablocks
    ContourAdd = '\
    set print @DataOut; \
    do for [i=|@DataIn1|:1:-1] { print @DataIn1[i]}; \
    do for [i=|@DataIn2|:1:-1] { print @DataIn2[i]}; \
    set print'

    DataIn1 = '$Contourg1_ord'
    DataIn2 = '$Contourg1_extordrev'
    DataOut = '$Contourg1_add'
    @ContourAdd


    set style fill noborder
    set datafile commentschar " "
    plot \
    for [i=1:8] $Contourf u 1:2:(i) skip 5 index i-1 w l lw 1.5 lc var title columnheader(5), \
    $Contourg1 u 1:2 skip 5 index 0 w l lw 2 lc 0 title columnheader(5), \
    $Contourg1_add u 1:2 w filledcurves fs transparent pattern 5 lc rgb "black" notitle

    enter image description here

    最佳答案

    另一种可能性是使用自定义破折号模式,如下所示:
    顺便说一句,使用“重新绘图”来组成单个图形几乎永远不会正确。

    # Additional contour levels displaced by 0.2 from the original
    set contour base
    set cntrparam levels disc 20.2
    unset surface
    set table $Contourg2d
    splot g2(x,y)
    unset table

    set contour base
    set contour base
    set cntrparam levels disc 26.2
    unset surface
    set table $Contourg1d
    splot g1(x,y)
    unset table

    set linetype 101 lc "black" linewidth 5 dashtype (0.5,5)

    plot for [i=1:8] $Contourf u 1:2:(i) skip 5 index i-1 w l lw 1.5 lc var title columnheader(5), \
    $Contourg1 u 1:2:(1) skip 5 index 0 w l lw 1 lc "black" title columnheader(5), \
    $Contourg2 u 1:2:(1) skip 5 index 0 w l lw 1 lc "black" title columnheader(5), \
    $Contourg1d u 1:2:(1) skip 5 index 0 w l linetype 101 notitle, \
    $Contourg2d u 1:2:(1) skip 5 index 0 w l linetype 101 notitle

    enter image description here

    修改以显示轮廓偏移的使用,以便虚线仅位于线的一侧。

    关于gnuplot 等高线图阴影线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57118566/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com