4
$\begingroup$

I would like to plot a graph with the legends at its side. However, I have managed to plot the legends, but the markers do not match the one in plot (picture bellow):

Legend is Marker shape insensitive!

My code for that plot is:

ListPlot[{Table[{(n - 1/2)*1/1000, VelocityU[[1, n]]}, {n, 1, NN}], 
  Table[{(n - 1/2)*1/1000, VelocityU[[SI, n]]}, {n, 1, NN}]}, 
 Joined -> True, Filling -> None, PlotMarkers -> Automatic, 
 PlotStyle -> {{Blue, Thin}, {Blue, Thin}}, 
 PlotRange -> {{0, (NN)*1/1000}, Automatic}, Frame -> True, 
 FrameLabel -> {"y [m]", "U(y) [m/s]"}, 
 GridLines -> {Range[1/1000, 1/100, 0.001], Automatic}, 
 GridLinesStyle -> {{Dashed, Lighter[Gray], Thin}, {Dashed, 
    Lighter[Gray], Thin}}, 
 FrameTicksStyle -> Directive[FontSize -> 8], 
 PlotLegends -> {"U(y, i = 1)", "U(y, i = 400)"}]

I have managed to solve the problem with the modified code: PlotStyle -> {{Blue, Thin}, {Red, Thin}}, but I still cannot plot proper markers, if I want to keep the blue color for both lines.

Problem partialy solved.

And the last question, how to reduce the font size in the legend?


My final and working solution on my OS is the following:

ListPlot[{Table[{(n - 1/2)*1/1000, VelocityU[[1, n]]}, {n, 1, NN}], 
  Table[{(n - 1/2)*1/1000, VelocityU[[SI, n]]}, {n, 1, NN}]}, 
 Joined -> True, PlotMarkers -> Automatic, Filling -> None, 
 PlotStyle -> {{Blue, Thin}, {Red, Thin}}, 
 PlotRange -> {{0, (NN)*1/1000}, Automatic}, Frame -> True, 
 FrameLabel -> {"y [m]", "U(y) [m/s]"}, 
 GridLines -> {Range[1/1000, 1/100, 0.001], Automatic}, 
 GridLinesStyle -> {{Dashed, Lighter[Gray], Thin}, {Dashed, 
    Lighter[Gray], Thin}}, 
 FrameTicksStyle -> Directive[FontSize -> 8], 
 PlotLegends -> 
  LineLegend[{"U(y, i = 1)", Row[{"U(y, i = ", ToString[SI], ")"}]}, 
   LegendMarkers -> {Automatic, Automatic}, LabelStyle -> 8]]

Final plot: Final solution of the problem in my case.

$\endgroup$
3
  • $\begingroup$ What version are you running? $\endgroup$
    – rcollyer
    Commented Apr 24, 2015 at 12:03
  • $\begingroup$ Mathematica v10.0.1.0 $\endgroup$
    – Mike
    Commented Apr 24, 2015 at 12:05
  • 1
    $\begingroup$ It's fixed on 10.0.2 and later, so I'd recommend an upgrade. $\endgroup$
    – rcollyer
    Commented Apr 24, 2015 at 12:18

1 Answer 1

7
$\begingroup$

Use the option LineLegend with sub-option LegendMarkers as the setting for PlotLegends:

NN = 15;
data = {Table[{(n - 1/2)*1/1000, 0}, {n, 1, NN}], 
   Transpose@{Table[(n - 1/2)*1/1000, {n, 1, NN}], -Sort[-RandomInteger[10, NN]]}};

ListPlot[data, Joined -> True, PlotMarkers -> Automatic, 
 PlotStyle -> {{Blue, Thin}, {Blue, Thin}}, 
 PlotRange -> {{0, (NN)*1/1000}, Automatic}, Frame -> True, 
 PlotLegends -> LineLegend[{"U(y, i = 1)", "U(y, i = 400)"}, 
                  LegendMarkers -> Automatic]]

enter image description here

$\endgroup$
6
  • $\begingroup$ Do you maybe know how to reduce the font size of the legend? $\endgroup$
    – Mike
    Commented Apr 24, 2015 at 11:32
  • $\begingroup$ @Mike, try the option LabelStyle, for example: LineLegend[{Red, Green, Blue}, {"label1", "label2", "label3"}, LabelStyle -> 10], or LabelStyle -> Directive[Red, Italic, 20]... $\endgroup$
    – kglr
    Commented Apr 24, 2015 at 11:34
  • $\begingroup$ Your answer does not work in my case. Markers in the legend remain the same. I use Mathematica 10.0.1.0. But it works with PlotLegends -> LineLegend[{"U(y, i = 1)", "U(y, i = 400)"}, LegendMarkers -> {Automatic, Automatic}] $\endgroup$
    – Mike
    Commented Apr 24, 2015 at 11:37
  • $\begingroup$ @Mike, it could be os differences: the code posted above works both in Version 9.0.1.0 (Windows 8 64-bit) and version 10 (wolfram programming cloud). LegendMarkers -> {Automatic,Automatic} also works in both versions. $\endgroup$
    – kglr
    Commented Apr 24, 2015 at 11:41
  • $\begingroup$ Do you maybe know, how can I write a code with grey background in the comments? I have tried <code> </code> and 'code', but nos success. $\endgroup$
    – Mike
    Commented Apr 24, 2015 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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