Question Details

No question body available.

Tags

gnuplot line legend

Answers (3)

July 17, 2025 Score: 2 Rep: 15,468 Quality: Medium Completeness: 80%

Here is a different approach that plots an actual function. The idea is to define a delta function that switches from -∞ to +∞ at x=4. Gnuplot won't handle a true infinite value without some work, so as a compromise I use -999 -> +999.

    set grid linetype 0 linewidth 1 dashtype 3 linecolor "grey50" # dotted
    set border linewidth 0.5
    set samples 1000
    set xrange [-10:10]
    set yrange [-10:10]

array M[5] = [0, -2, 3, 0.5, -0.8] array C[5] = [2, 0, 3.5, -4, -0.2] array NC[5] = ["dark-green", "orange-red", "steelblue", "dark-yellow", "brown"]

# Define a function with a vertical line at x=4 # Both ends of this line segment will be outside of yrange, so request clipping Delta4(x) = (x
July 14, 2025 Score: 1 Rep: 15,468 Quality: Medium Completeness: 80%

You can use keyentry to add a line to the plot key. The syntax for keyentry is the same as for an actual plot element.

    set grid linetype 0 linewidth 1 dashtype 3 linecolor "grey50" # dotted
    set border linewidth 0.5
    set samples 1000
    set xrange [-10:10]
    set yrange [-10:10]

array M[5] = [0, -2, 3, 0.5, -0.8] array C[5] = [2, 0, 3.5, -4, -0.2] array NC[5] = ["dark-green", "orange-red", "steelblue", "dark-yellow", "brown"]

set arrow from 4, -10 to 4, 10 nohead linewidth 3 linecolor "gray50"

set title "Straight Lines: 𝑦 = 𝑚𝑥 + 𝑐" plot for [i = 1:5:1] M[i]*x + C[i] linecolor rgbcolor(NC[i]) linewidth 3 title sprintf("%1.1f %s %1.1f", M[i], "𝑥 + ", C[i]), \ keyentry with lines lw 3 lc "gray50" title "x = 4"

enter image description here

July 18, 2025 Score: 1 Rep: 27,185 Quality: Medium Completeness: 100%

Here is an alternative suggestion with some differences to Chandra's and Ethan's solutions:

  • all parameters are provided in a datablock instead of arrays

  • the legend title is formatted differently for cases a=0, m=0 and c=0

All straight lines can be defined as ay = mx + c or

f(x,a,m,c) = a==0 ? 1e304sgn(x+c/m) : (mx + c)/a

We assume that ±1e304 is outside the plotting range. Furthermore, you have to

  • set samples 1000 or high enough that the vertical line appears "vertical enough"
  • set clip two that the line will be drawn and not suppressed
  • it might be advisable to set size ratio -1 such that e.g. the diagonal y=x appears as a 45° tilted line.

Script:

### plotting straight lines including vertical lines
reset session

straight lines: ay = mx + c

f(x,a,m,c) = a==0 ? 1e304sgn(x+c/m) : (mx + c)/a

a m c color

$Params