1
$\begingroup$

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}]
$\endgroup$
  • $\begingroup$ You can Use Show like here. $\endgroup$ – Kuba Jul 8 '13 at 12:57
  • $\begingroup$ 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. $\endgroup$ – Jelle Jul 8 '13 at 13:25
  • $\begingroup$ What version of Mathematica are you using? $\endgroup$ – Pinguin Dirk Jul 8 '13 at 14:19
  • $\begingroup$ I'm using Mathematica 9. $\endgroup$ – Jelle Jul 8 '13 at 14:24
  • 1
    $\begingroup$ 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...) $\endgroup$ – Pinguin Dirk Jul 8 '13 at 14:27
2
$\begingroup$

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

$\endgroup$
2
$\begingroup$
  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

$\endgroup$
  • $\begingroup$ Fantastic, thanks! The black isn't terribly important, as th default colours are changed to grey-scale anyway. $\endgroup$ – Jelle Jul 8 '13 at 13:39
  • $\begingroup$ @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 :) $\endgroup$ – Kuba Jul 8 '13 at 13:41

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.