Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

plotting legends in Mathematica

Tags:

How do you plot legends for functions without using the PlotLegends package?

like image 233
Wombat Avatar asked Aug 11 '10 22:08

Wombat


People also ask

How do you add a legend to a Mathematica plot?

To use PlotLegend, you first need to load the Plot Legends Package using Needs["PlotLegends`"]. PlotLegend->{text1,text2,…} assigns text to each line in the same fashion as PlotStyle. PlotLegend also enables Plot and related functions to accept options for Legend, which will modify the legend produced.

What is Plot legend?

Plot legends give meaning to a visualization, assigning meaning to the various plot elements. We previously saw how to create a simple legend; here we'll take a look at customizing the placement and aesthetics of the legend in Matplotlib.


2 Answers

I, too, was disappointed by the difficulty of getting PlotLegend to work correctly. I wrote my own brief function to make my own custom figure legends:

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

It is flexible, but not so easy to use. "names" is a list of strings to render in the legend; "markers" is a list with the same length as "names" of Graphics objects representing the plot markers or graphics to render; "origin" is a two-element list with the absolute horizontal and vertical position of the upper-left corner of the legend; "markerSize" is the number of points to scale the markers to; "fontSize" is the font size; "font" is the name of the font to use. Here is an example:

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

generated image

like image 109
the_one_smiley Avatar answered Sep 26 '22 10:09

the_one_smiley


I would also be very interested in an answer to this question.

To tell you what is wrong with PlotLegends: It is terribly unstable and in many instances doesn't work at all.

Here is an example where PlotLegends screws up completely. Output is from Mathematica 7.0:

Assume that we have measured some data points corresponding to a number of functions, and we want to show how well they compare to the ideal function, or maybe how well they match with a calculated fit. No problem! We'll just Show[] the smooth plot together with a ListPlot of the data points, right?

It could look something like this:

Show[  Plot[{Sin[x], Sinh[x]}, {x, -Pi, Pi}],  ListPlot[Join[{#, Sin[#]} & /@ Range[-Pi, Pi, .5],                 {#, Sinh[#]} & /@ Range[-Pi, Pi, .5]]]  ] 

alt text

Now we'd like to put a legend on the plot, so readers will know what on earth they're looking at. Easier said than done, mister! Let's add the PlotLegend to the 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]]]  ] 

alt text

This looks GREAT! Publish immediately!

For such a basic and ubiquitously needed functionality, it sure has been a lot of work to find an alternative to PlotLegend that just works. The best alternative I've found so far has been to meticulously construct a list of plotstyles, then construct the legend by hand, and finally to show it together with the plot using ShowLegend[]. (See for example here) It's possible, but a lot of work.

So if anyone knows of a workaround to make PlotLegend work, an alternative package that works better, or just a neat way to get legends that can be automated easily, I would be very grateful! It would certainly make life a little bit easier.

like image 27
James Avatar answered Sep 24 '22 10:09

James