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

时间:2017-07-14 07:53:24

标签: julia plots.jl

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

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中?

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

现在可以在Plots.jl中轻松启用它:

示例:

plot(rand(10), legend = :outertopleft)

enter image description here