# Performance issues of Manipulate with PlotLegends

I always have had performance issues of Manipulate while using PlotLegends, for example take a look at the following minimal examples

man1 = Manipulate[
Plot[Evaluate@Table[a*x^n, {n, 1, 8}], {x, 0, 2},
PlotRange -> 2], {a, -1, 1}];
man2 = Manipulate[
Plot[Evaluate@Table[a*x^n, {n, 1, 8}], {x, 0, 2}, PlotRange -> 2,
PlotLegends -> Automatic], {a, -1, 1}];
man3 = Manipulate[
Grid[{{
Dynamic@
Plot[Evaluate@Table[a*x^n, {n, 1, 8}], {x, 0, 2},
PlotRange -> 2, ImageSize -> 350, PlotStyle -> ColorData[97]]
, LineLegend[Table[ColorData[97][ci], {ci, 1, 8}], Automatic]
}}]
, {a, -1, 1}
];
Grid[{{"man1", "man2 (slow)", "man3 (fixed, but cumbersome)"}, {man1,
man2, man3}}]


Here, in man1 a standard plot is manipulated, in man2 you have the same but only with a legend and in man3 I used a grid in order to embed the plot with Dynamic and the legend separately. man2 is preeeeeeeeetty slow / not as fluently responsive as man1, although the only difference to man1 is the call of PlotLegends. I am using Mathematica 11.0.1 for Windows.

Is there any way to fix this more elegantly than my grid in man3 (for larger programs within Manipulate this becomes somewhat annoying)?

• I'm not aware of better workaround, this is what I'm usually doing too. – Kuba Nov 15 '16 at 11:42
• @Kuba damn, ok, well I will have to live with it then. Still thx! – Mauricio Fernández Nov 15 '16 at 12:02
• @Kuba Why we don't put a "bug" tag on this post? I think it is a performance bug. I just notice the same thing today... – matheorem Mar 15 '18 at 12:30
• @matheorem plot legends just take long to generate, no one said they won't. But yes, it is annoying. From our side it is just a poor quality but if WRI Support confirms that this indeed is worse than it should then bugs can be added. At least that is how I see it. – Kuba Mar 15 '18 at 12:38

One idea may be to pre-calculate the element that takes time, and then to simply paste it in a ready-to-use form. Of course, if it is acceptable. In your example, if you may have a varying in the course of manipulation number of lines, it is not the way. If, however, the number of lines is always fixed, I would do as follows. First let us prepare the legends:

pl = Plot[Evaluate@Table[1*x^n, {n, 1, 8}], {x, 0, 2}, PlotRange -> 2, PlotLegends -> Automatic];
legends = Rasterize[pl[[2, 1]], RasterSize -> 180]


then let us go to manipulate:

    Manipulate[
Plot[Evaluate@Table[a*x^n, {n, 1, 8}], {x, 0, 2}, PlotRange -> 2,
Epilog -> Inset[legends, Scaled[{0.9, 0.5}]]], {a, -1, 1}]


This might be a way in some cases. I hope it helps.

Have fun!

• Hmmm .... interesting ... one thing I learned indirectly through your solution is that the second part of the plot pl (pl[[2]]) contains the commands in order to place the legend nicely, i.e., Placed and After. A combination with your solution would then be Manipulate[ Legended[Plot[Evaluate@Table[a*x^n, {n, 1, 8}], {x, 0, 2}, PlotRange -> 2], Placed[legends, After, Identity]], {a, -1, 1}] so you dont have to rescale within the picture. Thanks! – Mauricio Fernández Nov 15 '16 at 14:14