# How can I save both the plot and the legend together?

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:

-
Strongly related: "Creating legends for plots with multiple lines?" – Alexey Popkov Nov 9 '13 at 9:55
Also related: mathematica.stackexchange.com/q/34357/280 – Alexey Popkov Nov 9 '13 at 10:00
How to position legends where you want them: mathematica.stackexchange.com/questions/7547/… – Mike Honeychurch Nov 9 '13 at 21:37

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

Another way would be to use Rasterize:

Rasterize[Plot[...]]


The legend and the graph is now one image.

-
I like this one, but I don´t know how to select legends and plot. The plot is easy, but I can´t select the 2 thing at same time. – Mika Ike Nov 9 '13 at 11:24
@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. – C. E. Nov 9 '13 at 11:50
@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. – Sjoerd C. de Vries Nov 9 '13 at 13:35
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. – user129412 Apr 22 at 14:30
@user129412 It doesn't include Out[], I just tried it to be sure. – C. E. Apr 22 at 14:37

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}]]


and export with

Export["/tmp/plot.png", plot]

-
+2 for code, -1 for the ugliest plot I've seen so far today! :) – cormullion Nov 9 '13 at 10:05
@cormullion You should have seen what I did yesterday at work ! – b.gatessucks Nov 9 '13 at 10:09

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}]]


-