13
$\begingroup$

Consider this example, taken from the docs and modified for a larger font size using Style (V10.0):

ListPlot[Table[f, {f, {Sin[x], Cos[x]}}, {x, 0, 2 π, 0.1}],
PlotLegends -> (Style[#, 40] & /@ {"sin(x)", "cos(x)"})]

Mathematica graphics

The plot markers are clearly aligned at the bottom of the legend text, which does not seem optimal.

Is there a way to influence this aligning?

$\endgroup$
2
  • $\begingroup$ @Mr. Wizard a true \[Pi] serial edit - my bad for being lazy :D $\endgroup$
    – Yves Klett
    Sep 17, 2014 at 14:12
  • $\begingroup$ I think it's my problem for being obsessive about it. $\endgroup$
    – Mr.Wizard
    Sep 17, 2014 at 14:15

2 Answers 2

15
$\begingroup$
ListPlot[Table[f, {f, {Sin[x], Cos[x]}}, {x, 0, 2 π, 0.1}],
PlotLegends ->PointLegend[ (Style[#, 40] & /@ {"sin(x)", "cos(x)"}),Alignment->Center]]

enter image description here

Update: As noted by rcollyer in the comments Alignment is an undocumented option for PointLegend. As an alternative, the following old Pane trick works without relying on an undocumented option:

ListPlot[Table[f, {f, {Sin[x], Cos[x]}}, {x, 0, 2  π, 0.1}],
PlotLegends ->(Style[Pane[#, BaselinePosition -> Center],40]& /@ 
              {"sin(x)", "cos(x)"})]
(* same picture *)

Update 2: Works with ListLinePlot and LineLegend combination:

ListLinePlot[Table[f, {f, {Sin[x], Cos[x]}}, {x, 0, 2 π, 0.1}], 
 PlotLegends -> LineLegend[(Style[#, 40] & /@ {"sin(x)", "cos(x)"}), Alignment -> Center]]

enter image description here

$\endgroup$
5
  • $\begingroup$ Can we extend that to ListLinePlot as well? $\endgroup$
    – Yves Klett
    Sep 17, 2014 at 14:05
  • 2
    $\begingroup$ As a side note, Alignment is an undocumented option for PointLegend. $\endgroup$
    – rcollyer
    Sep 17, 2014 at 14:05
  • $\begingroup$ Just checked - works nicely with an otherwise identical ListLinePlot. $\endgroup$
    – Yves Klett
    Sep 17, 2014 at 14:25
  • $\begingroup$ For my sins, I forgot to accept ... $\endgroup$
    – Yves Klett
    Apr 14, 2016 at 14:14
  • 1
    $\begingroup$ One note here: in order to have it so that the labels aren't also centered (as they are in this answer), you need to separate out the horizontal and vertical alignments: Alignment -> {Automatic, Center}. $\endgroup$ Feb 13, 2020 at 17:05
6
$\begingroup$

A reasonable workaround is to use SwatchLegend with LegendMarkers set:

ListPlot[
 Table[f, {f, {Sin[x], Cos[x]}}, {x, 0, 2 π, 0.1}],
 PlotLegends -> 
  SwatchLegend[{"sin(x)", "cos(x)"}, LabelStyle -> 40, LegendMarkers -> "Bubble" ]
]

enter image description here

$\endgroup$
1
  • 1
    $\begingroup$ Thank you! Should the current aligning be considered as buggy in this case? And can we extend the aligning for ListLinePlot legends as well? $\endgroup$
    – Yves Klett
    Sep 17, 2014 at 14:06

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.