Custom plots

Hello, I would like to understand how I can mark highest and lowest value on the y-axis in a plot. Assume a simple plot

using Plots
x = 1:10; y = rand(10)
plot(x, y) 

I will appreciate your guidance on, how
a. I can mark (dot or circle) and label the lowest and highest y-axis value
b. Give a custom name to the legend.

x = 1:10; y = rand(10)
maxpt = reverse(findmax(y))  # reversing so it's (x,y) rather than (y, x)
minpt = reverse(findmin(y))
plot(x, y)
scatter!([maxpt, minpt], label = "extrema")

More generally, findmax/findmin return (extremum, index). So if x didn’t conveniently go from 1-10, you would index into it with the appropriate index to make minpt/maxpt

4 Likes

Thank you! May I check a minor point with you? The label/legend sometimes overlap with the plot. Is there a way to align its position? I can’t see anything in the documentation.

You’re probably looking for legend = :bottomright/:bottomleft/etc

There are other legend related stuff in the docs, e.g. as relates to subplots (most of this stuff also applies to non-sub-plots)
https://docs.juliaplots.org/latest/generated/attributes_subplot/

Thank you again!

1 Like

Apologies, I have hit another problem. How can I add an external data point to the plot? For example, assume the plot is made using this code and I want to plot the data point (1.2,3) where x=1.2 and y =3

plot(x, y, linestyle = :dot, label = "Random", xlim = (-0.5,10),ylim = (0,5))```

I cannot use scatter!([1.2,3],label = "External") as it will plot it as two y-axis values rather than as one data point.

Either scatter!([1.2],[3],label = "External") or scatter!([(1.2,3)],label = "External")

Thanks! I was getting cannot convert float64 to series error when I tried to do it without