Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting names on plot lines #111

Open
janosh opened this issue Feb 22, 2025 · 1 comment
Open

Allow setting names on plot lines #111

janosh opened this issue Feb 22, 2025 · 1 comment
Assignees

Comments

@janosh
Copy link

janosh commented Feb 22, 2025

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")
})
@johannes-wolf johannes-wolf self-assigned this Feb 23, 2025
@johannes-wolf
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants