16
$\begingroup$

Given this plot:

enter image description here

ListLinePlot[
 {
  berea[[All, {1, 2}]]
  , beryl[[All, {1, 2}]]
  , graphite[[All, {1, 2}]]
  , nb[[All, {1, 2}]]
  , nu0[[All, {1, 2}]]
  , nunim[[All, {1, 2}]]
  }
 , Filling -> {5 -> {6}}
 , Frame -> True
 , FrameLabel -> {
   "!(*SubscriptBox[\"a\", \" \ \"])/!(*SubscriptBox[\"b\", \" \"])", "[Chi]/!(*SuperscriptBox[\"[Chi]\", \"*\"])"
   }
 , RotateLabel -> False
 , PlotStyle -> {{Dotted, Red}, Black, 
   Dashed, {DotDashed, Thick}, {Black, Thick}, {Black, Thick}}
 , PerformanceGoal -> "Quality"
 , PlotLegend -> {"Berea sandstone", "Beryl rock", "Graphite/epoxy", "Nb !(*SubscriptBox[\"Se\", \"2\"])", "[Nu]=0", "[Nu]=0.5"}
 , LegendShadow -> None
 , LegendPosition -> {-0.75, -0.225}
 , LegendBorder -> White
 , LegendBorderSpace -> Automatic
 , LegendTextSpace -> 3.25
 , LegendSpacing -> 0.1
 ]

How can I change the spacing of lines in the legend?

How to make the legend elements to be near to each other?

$\endgroup$
7
  • 3
    $\begingroup$ What version of Mathematica are you using? Are you aware of the new built-in legending functionality, available since version 9? $\endgroup$
    – Szabolcs
    Commented Aug 14, 2015 at 13:24
  • $\begingroup$ I don't understand what you are asking. You set the vertical spacing of the legends to 0.1 with LegendSpacing -> 0.1 and you got that spacing. If you want the legends closer together use a smaller value, say, .05 $\endgroup$
    – m_goldberg
    Commented Aug 14, 2015 at 18:06
  • $\begingroup$ @Szabolcs: Hi there, can you kindly elaborate on this functionality via an answer. I am having the same problem in this question and I am not satisfied with the answer below. :) $\endgroup$ Commented Sep 6, 2019 at 22:30
  • $\begingroup$ @H.R. Can you explain what it wrong with the answer below? I meant exactly what Verbeia has shown. $\endgroup$
    – Szabolcs
    Commented Sep 6, 2019 at 22:53
  • $\begingroup$ @Szabolcs: I want to do this in a Show command. Furthermore, there are around 12 curves and when I do the solution suggested by Verbeia, the legends appear in two columns while there is enough space to put them into one. By the way, why Spacings turns into red while using it in LineLegend? $\endgroup$ Commented Sep 7, 2019 at 7:42

1 Answer 1

22
$\begingroup$

For completeness, I present how to do this in versions 9 and above, using the built-in legending functions. The trick is captured in a previous question: use the Spacings option, even though it shows up as red when you use it in a LineLegend expression.

ListLinePlot[
 Table[PDF[BetaDistribution[2, j/3], i], {j, 1, 6, 1}, {i, 0.05, 0.95,
    0.05}], Filling -> {5 -> {6}}, Frame -> True, 
 RotateLabel -> False, 
 PlotStyle -> {{Dotted, Red}, Black, 
   Dashed, {DotDashed, Thick}, {Black, Thick}, {Black, 
    AbsoluteThickness[2]}}, PerformanceGoal -> "Quality", 
 PlotLegends -> 
  Placed[LineLegend[
    Automatic, {"Berea sandstone", "Beryl rock", "Graphite/epoxy", 
     "NB", "\[Nu]=0", "\[Nu]=0.5"}, Spacings -> 0.15], {0.2, 0.8}], 
 ImageSize -> 500]

enter image description here

Something to note is that if you set the first argument of LineLegend to Automatic, you need to ensure that the line styles are distinct. Otherwise not all the lines will be legended.

$\endgroup$
1
  • $\begingroup$ And another undocumented option for LineLegend. -.- Thank you, this was exactly what I was looking for. $\endgroup$ Commented Jan 17, 2020 at 10:20

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.