如何在 Plots.jl 图的图例中添加标记?

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

代码

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

生成以下图:

图例中没有标记,只有数据系列的线条颜色。这使得将线条与图例中的标签匹配起来变得非常困难,特别是对于那些色盲或阅读黑白打印输出的人。有没有办法在图例中显示绘图标记和线条颜色?

可能有更有效、更直接的方法,但您可以尝试单独绘制线条/标记:

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")

label="" 抑制行

的图例条目

color=i确保lines/markers的颜色相同

我正在为后代添加答案 - 这已在 Plots 中修复,因此有效:

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