2
$\begingroup$

I wonder if PlotLegends is part of a figure. Indeed in the code

Show[
 Plot[Sin[x], {x, 0, 2 π}, PlotStyle -> Black, 
  PlotLegends -> LineLegend[{Black}, {"Sin"}]],
 Plot[Cos[x], {x, 0, 2 π}, PlotStyle -> Red, 
  PlotLegends -> LineLegend[{Red}, {"Cos"}]],
 BaseStyle -> {FontSize -> 15, FontFamily -> "LM Roman 10"}
 ]

it does not seem to be affected by BaseStyle and not shown in the exported figure.

Is that a bug or am I missing something?

enter image description here

$\endgroup$
1
  • $\begingroup$ The short answer is no. If you make a plot with legends and type Head[%], it will say Legended instead of Graphics or Graphics3D etc. $\endgroup$ – Jens Jan 24 '16 at 19:12
3
$\begingroup$

The answer to your question is, it depends. If the legend is placed inside the area of the graphic, then the graphic, including the legend, can be highlighted and saved by right-clicking and choosing Save Graphics As...

enter image description here

The code used to produce this graphic is

Plot[{Sin[x], Cos[x]}, {x, 0, 2 π}, PlotStyle -> {Black, Red}, 
    PlotLegends -> Placed[LineLegend[{Sin, Cos}, 
            LabelStyle -> {FontFamily -> "Magneto"}], {.6, .8}], 
    BaseStyle -> {FontSize -> 15, FontFamily -> "Goudy Stout"}]

Note that the LineLegend uses the FontSize from the plot BaseStyle, but not the FontFamily. Consequently, it must be specified separately, as shown. I used two distinctive fonts, so that their difference would be obvious. (I do not have the LM Roman 10 font.) More generally, LineLegend has many options that can be used to create stylized legends.

$\endgroup$
5
  • $\begingroup$ Actually, you're not using "LM Roman 10" neither for the axes nor for the legends. Do you have that font or your solution is not working? $\endgroup$ – Fabio Jan 24 '16 at 13:51
  • $\begingroup$ @Fabio Good point. I do not have that font. Also, the legend is ignoring the FontFamily in Baseline, even though it is not ignoring the FontSize. I shall give this more thought. $\endgroup$ – bbgodfrey Jan 24 '16 at 14:19
  • $\begingroup$ @Fabio To specify the legend font, it appears necessary to use PlotLegends -> Placed[LineLegend[{Sin5, Cos}, LabelStyle -> {FontFamily -> "Magneto"}], {.6, .8}] I used a distinctive font here, so that I would be sure that it changed. I shall edit it into my answer soon. Thanks for catching this issue. $\endgroup$ – bbgodfrey Jan 24 '16 at 14:29
  • $\begingroup$ I had the same conclusion, but still I' still convinced that BaseStyle should be inherited by all of the text in a plot. $\endgroup$ – Fabio Jan 24 '16 at 14:49
  • $\begingroup$ @Fabio Version 10 introduced a number of formatting bugs in PlotLegends. Perhaps, not all of them have been corrected. See, for instance, my earlier question 55150. $\endgroup$ – bbgodfrey Jan 24 '16 at 14:53
2
$\begingroup$

I always had problems with that. I use Export and the expression in order to get the legend in the exported figure.

plot = Show[
  Plot[Sin[x], {x, 0, 2 π}, PlotStyle -> Black, 
   PlotLegends -> LineLegend[{Black}, {"Sin"}]], 
  Plot[Cos[x], {x, 0, 2 π}, PlotStyle -> Red, 
   PlotLegends -> LineLegend[{Red}, {"Cos"}]], 
  BaseStyle -> {FontSize -> 15, FontFamily -> "LM Roman 10"}]

Export[NotebookDirectory[] <> "fig.png", plot]

plot

$\endgroup$

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.