46
$\begingroup$

In this example, how can I put the legend inside the graph? Currently, when I save the graph, only the graph is saved, not the legend.

Expr1 = -2 p + 1
Expr2 = 2 p - 1
Expr3 = p - 1/2
Plot[{Expr1, Expr2, Expr3}, {p, 0, 1},
 BaseStyle -> AbsoluteThickness[4], 
 PlotLegends -> 
  LineLegend["Expressions", BaseStyle -> AbsoluteThickness[4]]]
Solve[Expr1 == Expr2, p]

Also, how can I insert the solution in the last line, also inside the graph?

Here you can see the result:

Screenshot of graph

$\endgroup$
4

4 Answers 4

45
$\begingroup$

This is how to save the graph, legend and all:

saves

Another way would be to use Rasterize:

Rasterize[Plot[...]]

The legend and the graph is now one image.

$\endgroup$
14
  • 1
    $\begingroup$ @MikaIke Why do you need to select them at the same time? If you save it this way, the plot and the legend will be saved together as if you had selected it as one picture. btw. I didn't include how to put the solution in, but I would've done it like b.gatessucks did it. $\endgroup$ Commented Nov 9, 2013 at 11:50
  • 3
    $\begingroup$ @MikaIke you can drag the cursor over both the legend and the plot. Start slightly to the right of the legend, click down, drag over both until both are selected (they are semi-transparently colored), then release mouse button. $\endgroup$ Commented Nov 9, 2013 at 13:35
  • 3
    $\begingroup$ While this method works, it still includes the Out[] part in the image, which I would rather not have. I understand this is a very old post, but instead of making a new one I figured it would be good to ask here first. $\endgroup$ Commented Apr 22, 2016 at 14:30
  • 1
    $\begingroup$ For me it includes the Out[..]= if I select the cell or bluntly highlight the content. If I try to carefully highlight exactly two items using shift+arrows, I can get it to save without the Out[..]=. $\endgroup$ Commented May 24, 2017 at 14:07
  • 1
    $\begingroup$ The WidowSize error can be fixed by making sure the zoom setting in your notebook is set to 100%. $\endgroup$ Commented Apr 21, 2018 at 3:22
12
$\begingroup$

You can define :

sol = Solve[Expr1 == Expr2, p][[1, 1]] ;
plot = Plot[{Expr1, Expr2, Expr3}, {p, 0, 1}, 
        BaseStyle -> AbsoluteThickness[4], 
        PlotLegends -> Placed[LineLegend["Expressions", BaseStyle -> AbsoluteThickness[4]], {Left, Center}], 
        Epilog -> Inset[sol, {0.5, 0.5}]]

plot

and export with

Export["/tmp/plot.png", plot]
$\endgroup$
3
  • 14
    $\begingroup$ +2 for code, -1 for the ugliest plot I've seen so far today! :) $\endgroup$ Commented Nov 9, 2013 at 10:05
  • 3
    $\begingroup$ @cormullion You should have seen what I did yesterday at work ! $\endgroup$ Commented Nov 9, 2013 at 10:09
  • 1
    $\begingroup$ e.g. 1 Plot[{Sin[x], Cos[x]}, {x, 0, 2 \[Pi]}, PlotLegends -> Placed[{Sin[x], Cos[x]}, {Center, Top}]] $$~$$ e.g. 2 Plot[{Sin[x], Cos[x]}, {x, 0, 2 \[Pi]}, PlotLegends -> Placed["Expressions", {Center, Top}]] $$~$$ Using placed. $$~$$ Placed[expr,pos] represents an expression expr placed at relative position pos in a chart or other display. $$~$$ contourplot: Using Rasterize $$~$$ Rasterize[ Labeled[ContourPlot[1/(1/P + 1/R), {P, 0, 1}, {R, 0, 1}, PlotLegends -> Automatic], {"P", "R"}, {Left, Bottom}]] $$~$$ Then you can use Save Image As ... to save plot. $\endgroup$ Commented Oct 15, 2019 at 1:42
3
$\begingroup$

Or you can use the full form position specification with Placed to position legend "inside" the graph and use Legended to put any additional legends.

Legended[Plot[{Expr1, Expr2, Expr3}, {p, 0, 1}, 
  BaseStyle -> AbsoluteThickness[4], 
  PlotLegends -> 
   Placed[LineLegend["Expressions", 
     BaseStyle -> AbsoluteThickness[4]], {1.05, 0.5}]], 
   Placed[First@First@Solve[Expr1 == Expr2, p], {0.5, 1}]]

legended graph

$\endgroup$
2
  • 1
    $\begingroup$ Alternatively, Placed[...,{Right,Right}] works as well. $\endgroup$ Commented Apr 21, 2018 at 3:24
  • 1
    $\begingroup$ This also works for the Save Graphic As... with PDFs! I recommend the simple solution by @Kai. Just put the usual PlotLegends where the dots are. $\endgroup$ Commented Apr 16, 2020 at 20:36
2
$\begingroup$

I'm late into this, but

plot = Plot[..., PlotLegends -> {...}]
Export["file.pdf", plot]

works fine for me in v12. (C.E.'s answer of Save selection as... puts an "Out[#]" on the side). So exporting to a PDF file could be a solution if one does not want to rasterize.

$\endgroup$

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.