如何在Julia pyplot中将图例放置在绘图区域之外

时间:2019-06-11 16:02:51

标签: matplotlib julia legend

我正在尝试找到一种方法来将Julia pyplot后端图例放置在绘图区域之外。我找到了一些讨论此问题的帖子,但找不到一致的答案。例如,我发现这篇文章:https://discourse.julialang.org/t/location-of-the-legend-in-pyplot/1311

但是问题在于,似乎pyplot中不再可用axes()。

P.S。用户ImportanceOfBeingErnest要求我输入代码。以下代码来自我放置链接的帖子:

x = 0:0.1:3*pi
y = sin(x)
z = cos(x)
ax = axes()
plot(x,y,label="sin(x)")
plot(x,z,label="cos(x)")
grid("on")
legend(bbox_to_anchor=[1.05,1],loc=2,borderaxespad=0)
ax[:set_position]([0.06,0.06,0.71,0.91])

我得到的错误:

MethodError: no method matching axes()
Closest candidates are:
  axes(!Matched::Core.SimpleVector) at essentials.jl:593
  axes(!Matched::Core.SimpleVector, !Matched::Any) at essentials.jl:594
  axes(!Matched::Base.Generator) at generator.jl:52
  ...

Stacktrace:
 [1] top-level scope at In[3]:7

1 个答案:

答案 0 :(得分:1)

假设您使用的是Julia 1.1,则可以使用以下代码:

const input = document.getElementById('email');
const defaultEmail = '@gmail.com';
input.value = defaultEmail;

function handleInput() {
    const val = input.value;
    const sel = input.selectionStart;
    if (val.length - defaultEmail.length < sel) {
        input.value = val.slice(0, sel)
        input.removeEventListener('input', handleInput);
        input.removeEventListener('focus', handleInput);
        input.removeEventListener('click', handleInput);
    }
}

input.addEventListener('input', handleInput);
input.addEventListener('focus', handleInput);
input.addEventListener('click', handleInput);