Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 619
tparker Avatar asked Jun 07 '17 01:06

tparker


People also ask

How do you put a marker on a plot?

Control Placement of Markers Along LineCreate a line plot with 1,000 data points, add asterisks markers, and control the marker positions using the MarkerIndices property. Set the property to the indices of the data points where you want to display markers.


2 Answers

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

like image 66
Michael K. Borregaard Avatar answered Oct 10 '22 22:10

Michael K. Borregaard


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

like image 32
Alain Avatar answered Oct 10 '22 22:10

Alain