首页 文章

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

提问于
浏览
7

代码

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

产生以下情节:

enter image description here

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

2 回答

  • 3

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

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

    enter image description here

  • 2

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

    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="" 禁止该行的图例条目

    color=i 确保线条/标记的颜色相同

相关问题