Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Here is my problem : I have a table composed of 3 "under"-Lists and I'd like to plot it with legends, but I can't find a way to do it.

DistSpat20084D = {
   {7, 7, 44, 79, 121, 148, 152, 158, 153, 165, 182, 160, 163, 164, 169, 177, 198, 190, 177, 
   173, 159, 155, 147, 126, 142, 127, 129, 117, 13}, 
   {7, 2, 10, 36, 53, 93, 108, 116, 134, 117, 115, 123, 84, 123, 127, 126, 153, 119, 147, 50, 
   43, 27, 23, 26, 21, 22, 28, 16, 18, 12,, 4, 5, 4, 3, 4, 1, 3, 0, 2, 4, 3, 4, 2, 5, 4,  0}, 
   {7, 1, 6, 12, 3, 3, 2, 5, 4, 1, 3, 2, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0}}; 

My command to plot and add legends :

ListLinePlot[Table[{km/10, DistSpat20084D[[j, km]]}, {j, 3}, 
  {km, Length[DistSpat20084D[[j]]]}],PlotLegend -> 
  {{"Speed1", "Speed2", "Speed3"}}, AxesOrigin -> {0, 0}]
share|improve this question
    
In case you are on v9, you could try: ListLinePlot[ Table[{km/10, DistSpat20084D[[j, km]]}, {j, 3}, {km, Length[DistSpat20084D[[j]]]}], PlotLegends -> {"Speed1", "Speed2", "Speed3"}] –  Pinguin Dirk Jul 23 '13 at 8:40
add comment

4 Answers 4

up vote 1 down vote accepted

First Input of PlotLegend V8:

 Needs["PlotLegends`"]

Then you can use your code with small changes (flattening the {{}}, LegendPosition and so on):`

ListLinePlot[Table[{km/10, DistSpat20084D[[j, km]]}, {j, 3}, 
  {km, Length[DistSpat20084D[[j]]]}], PlotLegend -> 
  {"Speed1", "Speed2", "Speed3"}, AxesOrigin -> {0, 0}, 
  LegendPosition -> {0.5, -.2}, ShadowBackground -> White]

enter image description here

share|improve this answer
    
Thank you vey much, it works ;) –  Sébastien Jul 23 '13 at 9:14
1  
Oh really? It's in the documentation though. –  Öskå Jul 23 '13 at 9:15
    
Haha, I looked in it buddy, "Needs" command was the key. I hadn't find it in the doc.And please Stop trolling, it took you only 10 min.. not a big deal . You should be proud of helping people –  Sébastien Jul 23 '13 at 9:24
2  
@Sébastien Welcome. I don't know why feel that he is "trolling" -- after all, he did answer your question! Please understand that here people are encouraged to read the documentation before posting questions, and questions that are too easily answered with a simple documentation search may be closed. In this case I'll not close this as it's pretty easy to overlook the Needs line and it's already gotten answers. –  Mr.Wizard Jul 23 '13 at 10:50
    
I understand the need to read the documentation before asking questions on this forum, thing I did. I use to handle this kind of little problems on my own for my work --with documentation--, but I got stuck on this one. Hence my question here... That's all –  Sébastien Jul 23 '13 at 12:55
add comment
ListLinePlot[
 Table[{km/10, DistSpat20084D[[j, km]]}, {j, 3}, {km, 
   Length[DistSpat20084D[[j]]]}], 
 PlotLegends -> {"Speed1", "Speed2", "Speed3"}, AxesOrigin -> {0, 0}]

enter image description here

share|improve this answer
    
I don't have mathematica v9... excuse me I didn't specified it –  Sébastien Jul 23 '13 at 8:46
add comment

This can also be solved very easily using the function autoLegend in my answer to Creating legends for plots with multiple lines.

lp = ListLinePlot[
  Table[{km/10, DistSpat20084D[[j, km]]}, {j, 3}, {km, 
    Length[DistSpat20084D[[j]]]}], AxesOrigin -> {0, 0}];

autoLegend[lp, {"Speed1", "Speed2", "Speed3"}]

legends

It's a one-liner. But you have to load all the definitions from the linked post first, of course.

Edit

Another way to label curves is by using the following code from this answer - which I'm repeating here for simplicity:

Options[burnTooltips] = {ImageSize -> 360, 
   "LabelFunction" -> (Framed[#, FrameStyle -> None, 
       RoundingRadius -> 8, Background -> RGBColor[1, .8, .4]] &)};

burnTooltips[plot_, opt : OptionsPattern[]] := 
 DynamicModule[{ins = {}, wrapper = OptionValue["LabelFunction"], 
   toolRule = 
    Function[{arg}, 
     Tooltip[t__] :> 
      Button[Tooltip[t], 
       AppendTo[arg, 
        Inset[wrapper[Last[{t}]], MousePosition["Graphics"]]]], 
     HoldAll]}, 
  EventHandler[
   Dynamic@Show[plot /. toolRule[ins], Graphics@ins, 
     ImageSize -> OptionValue[ImageSize]], {"MouseUp", 
     2} :> (toolRule = {} &)]]

To apply this, create the ListLinePlots as a table of three individual plots, each with its own Tooltip for its single displayed line, and combine with Show. Then use the above function to create an interactive display of the plot in which you can click to place the labels directly on each line:

lp = Show[
   Table[
    ListLinePlot[
     Tooltip[Table[
       {km/10, DistSpat20084D[[j, km]]}, {km, 
        Length[DistSpat20084D[[j]]]}], {"Speed1", "Speed2", 
        "Speed3"}[[j]]]
     , AxesOrigin -> {0, 0}, PlotStyle -> ColorData[1][j], 
     PlotRange -> All
     ],
    {j, 3}
    ]
   ];

burnTooltips[lp]

labeled 2

Remember to right-click the mouse in order to finish the interactive labeling. Only then will the display turn into an inert Graphics object that you can copy and paste.

share|improve this answer
add comment

This seems like an interesting graphics exercise so I thought I'd try my hand at it. The data have several aspects that would benefit from individual treatment. First, the curves are well separated so they can easily be directly labeled. Legends are really a distraction and I'll say more about them below. The third data set has quite low values and it tends to get lost along the axis. To solve that I scaled it up by 5 and used a right hand axis for its values. The light tan color that Mathematica automatically uses is a bit weak so I switched to the next color, green. I also labeled the curve and the right hand tick values in green to make the identification. I don't know if the missing value in the second data set was truly missing or an error but I left it in. I don't know what the ordinate values represent so I decided to jazz it up and make it the speed of cheetahs. I have grown to dislike the PlotLabel option, finding the Mathematica Labeled routine much superior. (Or sometimes one can write plot labels directly on a blank space in the plot.)

Naturally I used Presentation and in defense I wonder how many users would consider a treatment like this with regular Mathematica "set-piece" plots.

<< Presentations`

DistSpat20084D = {{7, 7, 44, 79, 121, 148, 152, 158, 153, 165, 182, 
    160, 163, 164, 169, 177, 198, 190, 177, 173, 159, 155, 147, 126, 
    142, 127, 129, 117, 13}, {7, 2, 10, 36, 53, 93, 108, 116, 134, 
    117, 115, 123, 84, 123, 127, 126, 153, 119, 147, 50, 43, 27, 23, 
    26, 21, 22, 28, 16, 18, 12, , 4, 5, 4, 3, 4, 1, 3, 0, 2, 4, 3, 4, 
    2, 5, 4, 0}, {7, 1, 6, 12, 3, 3, 2, 5, 4, 1, 3, 2, 0, 1, 0, 0, 0, 
    0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0}};

I thought it was easier to generate the individual point data sets.

distSpatTable = 
  Table[{km/10, DistSpat20084D[[j, km]]}, {j, 3}, {km, 
    Length[DistSpat20084D[[j]]]}];

The following creates the graphic.

rightTicks = 
  CustomTicks[5 # &, {0, 12, 3, 1}, 
   CTNumberFunction -> (Style[#, ColorData[1][4]] &)];
 Draw2D[
   {ListLineDraw[distSpatTable[[1]], PlotStyle -> ColorData[1][1]],
    ListLineDraw[distSpatTable[[2]], PlotStyle -> ColorData[1][2]],
    ListLineDraw[{1, 5} # & /@ distSpatTable[[3]], 
     PlotStyle -> ColorData[1][4]],
    Text["Chettah 1", {2.5, 160}],
    Text["Cheetah 2", {2.35, 80}],
    Text[Style["Cheetah 3", ColorData[1][4]], {1, 38}]},
   AspectRatio -> 1/2,
   Axes -> False,
   Frame -> True,
   FrameTicks -> {{Automatic, rightTicks}, {Automatic, Automatic}}] //
   Labeled[#, 
    Style["Fig.1 Speed (km/h) vs. distance (km) for three cheetah \
runs.", 12, FontFamily -> "Helvetica"], {Bottom, Left}] &

enter image description here

Legends are a weak method for conveying information. They are another plot that distracts from the main data presentation. They require the viewer to carry information from one location to another location. It is much better if curves (or points) can be directly labeled. But what happens if you have, say, 20 curves that are rather tightly intertwined? Then, if you are creating a printed graphics, a legend is about the only choice. Bur how well can a reader extract information from one curve when it is being clobbered by 19 other curves and perhaps data points? On the other hand, if you are communicating via Mathematica notebooks (or giving a presentation from a Mathematica notebook) there is a much better choice. You could use a CheckboxBar, or a RadioButtonBar to dynamically select one or several curves and gray out the other curves. It's easy to do and a much superior way to examine such data sets. Someday technical communications will routinely be done through Mathematica notebooks.

share|improve this answer
    
I agree on the labels vs legend part, but as far as the superimposition of the two plots goes, you could easily use Overlay... –  rm -rf Jul 23 '13 at 17:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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