3
$\begingroup$

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}}]

enter image description here

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

$\endgroup$
  • $\begingroup$ I'm not aware of better workaround, this is what I'm usually doing too. $\endgroup$ – Kuba Nov 15 '16 at 11:42
  • $\begingroup$ @Kuba damn, ok, well I will have to live with it then. Still thx! $\endgroup$ – Mauricio Fernández Nov 15 '16 at 12:02
  • $\begingroup$ @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... $\endgroup$ – matheorem Mar 15 '18 at 12:30
  • $\begingroup$ @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. $\endgroup$ – Kuba Mar 15 '18 at 12:38
4
$\begingroup$

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]

enter image description here

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}]

enter image description here

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

Have fun!

$\endgroup$
  • $\begingroup$ 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! $\endgroup$ – Mauricio Fernández Nov 15 '16 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.