cetz-plot icon indicating copy to clipboard operation
cetz-plot copied to clipboard

Allow setting names on plot lines

Open janosh opened this issue 9 months ago • 1 comments

in this example, i'd like to pass name to plot.add in order to draw lines between e.g. "n_B-T=1.50%" and "n_B-T=2.50%" but i'm getting an unexpected argument error. for now, i'm hard-coding the start/end points of the bezier curves

Image

#import "@preview/cetz:0.3.2": canvas, draw
#import "@preview/cetz-plot:0.1.1": plot
#import draw: content, line, bezier

#set page(width: auto, height: auto, margin: 8pt)

// Bose-Einstein distribution function
#let n_B(x, T) = {
  if x == 0 or T == 0 { return 0.5 }
  let ratio = x / T
  1 / (calc.exp(ratio) - 1) + 0.5
}

#canvas({
  draw.set-style(axes: (y: (label: (anchor: "south-east", angle: 90deg)), x: (label: (anchor: "north-east"))))

  plot.plot(
    size: (8, 7),
    x-label: $"Re"(p_0)$,
    y-label: $n_"B" (p_0)$,
    x-min: 0,
    y-min: 0,
    y-max: 5,
    x-tick-step: none,
    y-tick-step: none,
    axis-style: "left",
    {
      // Plot distributions for different temperatures
      for (T, color) in ((0.5, red), (1, orange), (2, blue)) {
        plot.add(
          style: (stroke: color + 1.5pt),
          domain: (0.01, 2),
          samples: 150,
          x => n_B(x, T),
          name: "n_B-T=" + str(T),  // --> this line raises unexpected argument
        )
      }
    },
  )

  // Add curved arrows between intersection points
  let arrow-style = (end: "stealth", stroke: 1pt, fill: black)

  // First arrow (T=0.5 to T=1)
  bezier(
    (1.8, 1.7), // start point
    (2.4, 2.4), // end point
    (2, 2.3), // control point
    mark: arrow-style,
    name: "arrow1",
  )
  content("arrow1.mid", text(size: 8pt)[$2 dot T$], anchor: "south-east")

  // Second arrow (T=1 to T=2)
  bezier(
    (2.4, 2.5), // start point
    (3.5, 3.2), // end point
    (2.9, 3.3), // control point
    mark: arrow-style,
    name: "arrow2",
  )
  content("arrow2.mid", text(size: 8pt)[$2 dot T$], anchor: "south-east")
})

janosh avatar Feb 22 '25 23:02 janosh

I've not added that initially because if clipped, a single plot can have a large number of separate lines. My Idea was that add-anchor is a more precise way of creating data-relative anchors. But I think I will add named plot-elements, although they might not be very useful in some scenarios.

johannes-wolf avatar Feb 23 '25 23:02 johannes-wolf