首页 文章

如何将图例移动到Plots.jl(GR)中的绘图区域外?

提问于
浏览
4

我有以下图表,其中部分数据被图例模糊:

using Plots; gr()
using StatPlots
groupedbar(rand(1:100,(10,10)),bar_position=:stack, label="item".*map(string,collect(1:10)))

stacked bar plot

我可以看到,使用“图例”属性,图例可以移动到绘图区域内的各个位置,例如:

groupedbar(rand(1:100,(10,10)),bar_position=:stack, label="item".*map(string,collect(1:10)),legend=:bottomright)

stacked bar plot with legend at bottom right

有没有办法将绘图图例完全移动到绘图区域之外,例如在绘图右侧或下方?对于这些堆积的条形图,在情节区域内的传说真的没有好地方 . 到目前为止,我能够提出的唯一解决方案是在输入数据矩阵中创建一些“虚假”的空行,以便用一些零空间来创建空间,但这看起来有点像hacky并且需要一些摆弄来获得正确的每次绘图时额外的行数:

groupedbar(vcat(rand(1:100,(10,10)),zeros(3,10)),bar_position=:stack, label="item".*map(string,collect(1:10)),legend=:bottomright)

stacked bar plot with legend placed in extra white space

我可以看到,那里有some kind of a solution proposed for pyplot,有没有人知道GR后端的类似解决方案?我能想象的另一种解决方案 - 有没有办法将图例本身保存到不同的文件中,以便我可以将它们放回到Inkscape中?

1 回答

  • 1

    使用布局我可以创建一个解决方法,仅使用图例制作假图 .

    using Plots
    gr()
    l = @layout [a{0.001h}; b c{0.13w}]
    
    values = rand(1:100,(10,10))
    
    p1 = groupedbar(values,bar_position=:stack, legend=:none)
    p2 = groupedbar(values,bar_position=:stack, label="item".*map(string,collect(1:10)), grid=false, xlims=(20,3), showaxis=false)
    
    
    p0=plot(title="Title",grid=false, showaxis=false)
    
    plot(p0,p1,p2,layout=l)
    

    enter image description here

相关问题