32
$\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$
32
$\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$
  • 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$ – C. E. Nov 9 '13 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$ – Sjoerd C. de Vries Nov 9 '13 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$ – user129412 Apr 22 '16 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$ – Džuris May 24 '17 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$ – Kai Apr 21 '18 at 3:22
9
$\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$
  • 9
    $\begingroup$ +2 for code, -1 for the ugliest plot I've seen so far today! :) $\endgroup$ – cormullion Nov 9 '13 at 10:05
  • 3
    $\begingroup$ @cormullion You should have seen what I did yesterday at work ! $\endgroup$ – b.gates.you.know.what Nov 9 '13 at 10:09
  • $\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$ – Nick Dong 2 days ago
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$
  • $\begingroup$ Alternatively, Placed[...,{Right,Right}] works as well. $\endgroup$ – Kai Apr 21 '18 at 3:24

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.