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.

I am plotting a number of functions (at least 4 in one plot) with a legend. Needless to say, it is important that it is clear from the legend which line belongs to which function. I tried to play around with the PlotStyle, but I'm not getting them to look all that different. The paper it will be published in demands grey-scale pictures, so I can't use different colours.

I was hoping to use PlotMarkers instead, but I found that these only exist within the ListPlot but not in Plot. In theory I could overlay a Plot with a ListPlot, but then it wouldn't show in the legend. What is the best way to add the markers in both the plot and the legend?

This is a minimal working example of something to which I'd to add PlotMarkers.

Needs["PlotLegends`"];
Plot[{Sin[x], Sin[x + π/4], Sin[x + π/2], Sin[x + 3π/4]}, {x, 0, 2π},
     PlotLegend -> {"#1", "#2", "#3", "#4"},
     PlotStyle -> {DotDashed, Dashed, Dashing[0.01], Dotted}]
share|improve this question
    
You can Use Show like here. –  Kuba Jul 8 '13 at 12:57
    
Could you be a bit more specific? I'd know a couple of uses for Show[], but none of them is extremely helpful here. That is what I meant in the question: when I use Plot and ListPlot within the same Show[] command, it doesn't quite cover it. –  Jelle Jul 8 '13 at 13:25
    
What version of Mathematica are you using? –  Pinguin Dirk Jul 8 '13 at 14:19
    
I'm using Mathematica 9. –  Jelle Jul 8 '13 at 14:24
1  
Then I suggest looking at PlotLegends (instead of the package you are loading). You can e.g. use it directly in @Kuba's answer (I personally always had problems with the PlotLegends package...) –  Pinguin Dirk Jul 8 '13 at 14:27
show 2 more comments

2 Answers

up vote 2 down vote accepted

If you have the functions, you can always make a ListPlot

f1[x_] := Sin[x];
f2[x_] := Sin[x + \[Pi]/4];
f3[x_] := Sin[x + \[Pi]/2];
f4[x_] := Sin[x + 3 \[Pi]/4];
(*some arbitrary functions*)

(*create table from a function*)
dataf[f_, xmin_, xmax_, ndata_] := Table[{x, f[x]}, {x, xmin, xmax, (xmax - xmin)/ndata}];

Needs["PlotLegends`"];
ListPlot[{dataf[f1, 0, 2 \[Pi], 20], dataf[f2, 0.01, 2 \[Pi], 20],
dataf[f3, 0.02, 2 \[Pi], 20], dataf[f4, 0.03, 2 \[Pi], 20]}, 
PlotLegend -> {"f1", "f2", "f3", "f4"},LegendPosition -> {1.1, -0.4},
LegendShadow -> False, Joined -> {True, False, True, False},
PlotMarkers -> Automatic]

It will give you this

plot

share|improve this answer
add comment
  h = Graphics[{Black, #1}, PlotRange -> #2] &; 

  ListPlot[Transpose@Table[{Sin[x], 
                            Sin[x + \[Pi]/4], 
                            Sin[x + \[Pi]/2], 
                            Sin[x + 3 \[Pi]/4]}, {x, 0, 2 \[Pi], .1}], 
           PlotLegend -> {"#1", "#2", "#3", "#4"}, Joined -> True, 
           PlotStyle -> Directive@{Black, Thick},  ImageSize -> 500,
           PlotMarkers -> {{h[Disk[], 1], .04}, {h[Disk[], 2], 0.01}, 
                           {h[Rectangle[{-1, -1}, {1, 1}], .5], .04}, 
                           {h[{Thick, 
                               Line[{{0, 0}, {0, 10}}]}, {{-1, 1}, {0, 10}}], .05}}]

enter image description here

share|improve this answer
    
Fantastic, thanks! The black isn't terribly important, as th default colours are changed to grey-scale anyway. –  Jelle Jul 8 '13 at 13:39
    
@Jelle I have used custom markers because in grayscale automatic are not clear imo. Next time be patient with accpeting, I'm sure someone will show something nicer :) –  Kuba Jul 8 '13 at 13:41
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.