Area limitation of plot in Plots.jl (GR) not works for series annotations. This is a bug or expected behavior?

Viewed 38

If we want to draw the plot not over the entire set of values, but only over the selected area, this restriction only works for points, but does not work for their annotations. Is this the expected behavior? And if this is the expected behavior, then how should the annotation output area be limited?

Example:

x = (1:1:30)
plot(x, x, zcolor = x, seriestype = :scatter, title = "My plot", 
xlim=(5, 25), ylim=(3, 30), markersize=5, label = "", 
series_annotations = text.(x, :bottom))

Result:

enter image description here

1 Answers

I believe this is the expected behavior -- I've exploited this before to generate annotations "outside" the plot area.

To fix this you'll probably need to manually loop through the plot points and check if they're in bounds or not, i.e.:

x = (1:1:30); xlims=(5,25)
p = plot(x, x, zcolor = x, seriestype = :scatter, title = "My plot", 
xlim=xlims, ylim=(3, 30), markersize=5, label = "")

for i=1:length(x)
    if x[i]>xlims[1] && x[i]<xlims[2]
        p = annotate!(x[i],x[i]+2,text("$(x+x[i])"))
    end
end
display(p)

Note that I've added +2 to the y on the annotations so that they're above the actual points themselves (have to specify this according to your actual use case / what you think looks best), which produces the desired result I think:

enter image description here

Note that the text command can take other arguments as well, i.e. doing text("$(x[i])",:Courier,10,:red) produces:

enter image description here

Related