the*_*ley 15
我也对让PlotLegend正常工作的困难感到失望.我写了自己的简短函数来制作我自己的自定义图例:
makePlotLegend[names_, markers_, origin_, markerSize_, fontSize_, font_] :=
Join @@ Table[{
Text[
Style[names[[i]], FontSize -> fontSize, font],
Offset[
{1.5*markerSize, -(i - 0.5) * Max[markerSize,fontSize] * 1.25},
Scaled[origin]
],
{-1, 0}
],
Inset[
Show[markers[[i]], ImageSize -> markerSize],
Offset[
{0.5*markerSize, -(i - 0.5) * Max[markerSize,fontSize] * 1.25},
Scaled[origin]
],
{0, 0},
Background -> Directive[Opacity[0], White]
]
},
{i, 1, Length[names]}
];
Run Code Online (Sandbox Code Playgroud)
它很灵活,但不是那么容易使用."names"是要在图例中呈现的字符串列表; "markers"是一个与Graphics对象的"names"长度相同的列表,表示要渲染的绘图标记或图形; "origin"是一个双元素列表,具有图例左上角的绝对水平和垂直位置; "markerSize"是将标记缩放到的点数; "fontSize"是字体大小; "font"是要使用的字体的名称.这是一个例子:
Plot[{x, x^2}, {x, 0, 2}, PlotStyle -> {Blue, Red},
Epilog -> makePlotLegend[
{x, x^2},
(Graphics[{#, Line[{{-1, 0}, {1, 0}}]}]) & /@ {Blue, Red},
{0.9, 0.3},
12,
12,
"Arial"
]
]
Run Code Online (Sandbox Code Playgroud)

我也对这个问题的答案很感兴趣.
告诉你PlotLegends有什么问题:它非常不稳定,在很多情况下根本不起作用.
这是一个PlotLegends完全搞砸的例子.输出来自Mathematica 7.0:
假设我们测量了一些与多个函数相对应的数据点,并且我们想要显示它们与理想函数的比较情况,或者它们与计算拟合的匹配程度.没问题!我们只是将数据点的ListPlot与[平滑图表]一起显示,对吧?
它可能看起来像这样:
Show[
Plot[{Sin[x], Sinh[x]}, {x, -Pi, Pi}],
ListPlot[Join[{#, Sin[#]} & /@ Range[-Pi, Pi, .5],
{#, Sinh[#]} & /@ Range[-Pi, Pi, .5]]]
]
Run Code Online (Sandbox Code Playgroud)

现在我们想在情节上加上一个传奇,所以读者会知道他们在看什么.先生说起来容易做起来难!让我们将PlotLegend添加到Plot []:
Show[
Plot[{Sin[x], Sinh[x]}, {x, -Pi, Pi}, PlotLegend -> {Sin[x], Sinh[x]}],
ListPlot[Join[{#, Sin[#]} & /@ Range[-Pi, Pi, .5],
{#, Sinh[#]} & /@ Range[-Pi, Pi, .5]]]
]
Run Code Online (Sandbox Code Playgroud)

这看起来很棒!马上发布!
对于这样一个基本且无处不在的功能,找到PlotLegend的替代方案确实有很多工作要做.到目前为止,我发现的最佳替代方案是精心构建一个绘图样式列表,然后手工构建图例,最后使用ShowLegend []将其与绘图一起显示.(例如,见这里)这是可能的,但很多工作.
因此,如果有人知道使PlotLegend工作的解决方法,一个更好的替代包,或只是一个简洁的方式来获得可以轻松实现自动化的传说,我将非常感激!这肯定会让生活变得更轻松.