Add labels to scatter

Hello =)

I would like to add labels for each scatter in my code.

    f = Figure(; size=(500, 400))
    ax1 = Axis3(f[1, 2], aspect = (1, 1, 1.5),
    xlabel = "x [m]",ylabel = "y [m]",zlabel = "z [m]",
    title="t = $value days")
    hm = lines!(ax1, coord_x, coord_y, coord_z, color =:black, colormap = :black, linewidth = 5,  colorrange = (-50,300))  # , colorrange = (0,1200)
    S1b = scatter!(ax1,x_axe1,y_axe1,h1)
    S1m = scatter!(ax1,x_axe1,y_axe1,h2)
    S1h = scatter!(ax1,x_axe1,y_axe1,h3)
    S2b = scatter!(ax1,x_axe2,y_axe2,h1)
    S2m = scatter!(ax1,x_axe2,y_axe2,h2)
    S2h = scatter!(ax1,x_axe2,y_axe2,h3)
    S3b = scatter!(ax1,x_axe3,y_axe3,h1)
    S3m = scatter!(ax1,x_axe3,y_axe3,h2)
    S3h = scatter!(ax1,x_axe3,y_axe3,h3)
    f

I would like that the text sensor 1 appears next to S1b etc.
How can I do that ?
Best,

1 Like

I think you are using Makie but I am not sure. Can you please include a running example where you also include the using xxx for the package you are using.

If it is Makie you can try.

S1b = scatter!(ax1,x_axe1,y_axe1,h1, label = "sensor 1")
f[1, 3] = Legend(f, ax1)
f

See here or documentation Legend | Makie

1 Like

I just wanted to give a try with Claude. I am curious if it isn’t halucinated the answer ofc!

So:
To add labels for each scatter plot in your 3D visualization, you can use the text! function fr Makie. Here’s how you can modify your code to include labels for each scatter:

  f = Figure(; size=(500, 400))
  ax1 = Axis3(f[1, 2], aspect = (1, 1, 1.5),
  xlabel = "x [m]",ylabel = "y [m]",zlabel = "z [m]",
  title="t = $value days")
  hm = lines!(ax1, coord_x, coord_y, coord_z, color =:black, colormap = :black, linewidth = 5,  colorrange = (-50,300))  # , colorrange = (0,1200)
  S1b = scatter!(ax1,x_axe1,y_axe1,h1)
  S1m = scatter!(ax1,x_axe1,y_axe1,h2)
  S1h = scatter!(ax1,x_axe1,y_axe1,h3)
  S2b = scatter!(ax1,x_axe2,y_axe2,h1)
  S2m = scatter!(ax1,x_axe2,y_axe2,h2)
  S2h = scatter!(ax1,x_axe2,y_axe2,h3)
  S3b = scatter!(ax1,x_axe3,y_axe3,h1)
  S3m = scatter!(ax1,x_axe3,y_axe3,h2)
  S3h = scatter!(ax1,x_axe3,y_axe3,h3)

  # Add labels for each scatter
  text!(ax1, x_axe1[1], y_axe1[1], h1[1], text = "Sensor 1", align = (:left, :bottom))
  text!(ax1, x_axe2[1], y_axe2[1], h1[1], text = "Sensor 2", align = (:left, :bottom))
  text!(ax1, x_axe3[1], y_axe3[1], h1[1], text = "Sensor 3", align = (:left, :bottom))

  f

This modification adds three text! calls, one for each sensor. Here’s what each parameter does:

  1. We use the first point of each scatter (x_axe1[1], y_axe1[1], h1[1]) as the position for the label.
  2. The text parameter specifies the label content.
  3. align = (:left, :bottom) aligns the text to the left and bottom of the specified position.
  4. offset = (10, 10, 10) moves the text slightly away from the point in all three dimensions to avoid overlap.

You might need to adjust the offset values depending on your data and preferences. Also, if you want the labels to appear at a different position (e.g., the middle or end of each scatter series), you can change the indices used for positioning.

Remember to ensure that you have the necessary imports (like using GLMakie or using CairoMakie) at the top of your file

I am curious if it isn’t halucinated the answer ofc!

Why not just test yourself if it’s hallucinated the answer prior to sharing the result?

Yes, sorry. I actually tried to test. But the input values were missing. So I was like, the author maybe can test it.

But you are right I could ask the AI to fill the missing values from the question.

I don’t mind if code in an answer is LLM-generated, it’s just code after all, and it either works or it doesn’t, but the human behind it still needs to make sure it makes sense in context and works. If you just post answers in random threads to try out if your models generate good code it’s a bit like you’re using the forum as a playground for your AI adventures, testing on the people here.

2 Likes

It works very well, I will take note that if the quesiton isn’t comprehensive or contains error then I have to correct it too if LLMs are used:

using Makie
using GLMakie

f = Figure(; size=(500, 400))
ax1 = Axis3(f[1, 2], aspect = (1, 1, 1.5),
xlabel = "x [m]", ylabel = "y [m]", zlabel = "z [m]",
title="t = 5 days")

# Sample coordinate data for the black line
coord_x = Float32[0, 1, 2, 3, 4, 5]
coord_y = Float32[0, 1, 2, 1, 0, 1]
coord_z = Float32[0, 1, 2, 3, 2, 1]

hm = lines!(ax1, coord_x, coord_y, coord_z, color=:black, colormap=:black, linewidth=5, colorrange=(-50, 300))

# Sample data for sensor positions
x_axe1, y_axe1 = Float32[1], Float32[1]
x_axe2, y_axe2 = Float32[3], Float32[2]
x_axe3, y_axe3 = Float32[5], Float32[0]

# Sample height data for sensors
h1, h2, h3 = Float32[0.5], Float32[1.5], Float32[2.5]

S1b = scatter!(ax1, x_axe1, y_axe1, h1, color=:red)
S1m = scatter!(ax1, x_axe1, y_axe1, h2, color=:green)
S1h = scatter!(ax1, x_axe1, y_axe1, h3, color=:blue)
S2b = scatter!(ax1, x_axe2, y_axe2, h1, color=:red)
S2m = scatter!(ax1, x_axe2, y_axe2, h2, color=:green)
S2h = scatter!(ax1, x_axe2, y_axe2, h3, color=:blue)
S3b = scatter!(ax1, x_axe3, y_axe3, h1, color=:red)
S3m = scatter!(ax1, x_axe3, y_axe3, h2, color=:green)
S3h = scatter!(ax1, x_axe3, y_axe3, h3, color=:blue)

# Add labels for each scatter
text!(ax1, x_axe1[1], y_axe1[1], h3[1], text="Sensor 1", align=(:left, :bottom))
text!(ax1, x_axe2[1], y_axe2[1], h3[1], text="Sensor 2", align=(:left, :bottom))
text!(ax1, x_axe3[1], y_axe3[1], h3[1], text="Sensor 3", align=(:left, :bottom))

f

image

:smiley:

Thanks !
It works great :smiley:

1 Like

By the way, I want to emphasize that It isn’t for fun. I am using credits to answer someone question. (Even if it is extremly cheap)
I want to help the community actually.

Why? Because I got so much from the Julia community and I want to give back as much as possible.
I really sorry if the first tries cause problems by the way. I know there can be problems at the beginning. But in the long run there is an option that everyone in Julia… from beginner till middle level can get correct answer to his question and also senior can find information that isn’t searchable in the codebase ecosystem (sometimes even correct answers for the complex problems too :d ).