tparker
tparker

Reputation: 724

How do you add markers to the legend of a Plots.jl plot?

The code

using Plots
pyplot(markershape = :auto)
for i in 1:4
  plot!(rand(10), label = "Series " * string(i))
end
savefig("Plot.png")

produces the following plot:

enter image description here

The markers do not appear in the legend, only the data series's line color. This makes it significantly harder to match the lines up with the labels in the legend, especially for those who are colorblind or reading off of a black-and-white printout. Is there a way to display the plot markers as well as line colors in the legend?

Upvotes: 10

Views: 4724

Answers (2)

Michael K. Borregaard
Michael K. Borregaard

Reputation: 8044

I am adding an answer for posterity - this has been fixed in Plots, so this works:

plot(rand(10,4), markershape = :auto)

enter image description here

Upvotes: 7

Alain
Alain

Reputation: 883

There's probably a more efficient, straightforward way but you can try plotting the line / markers separately:

using Plots
pyplot(markershape = :auto)
for i in 1:4
    x = rand(10)
    plot!(x, color=i, marker=false, label="")
    scatter!(x, color=i, markersize=10, label = "Series " * string(i))
end
savefig("Plot.png")

plot

label="" suppresses the legend entry for the line

color=i ensures that the color of the lines/markers are the same

Upvotes: 3

Related Questions