# Error when placing legends

Consider the following plot

plot = ListLinePlot[{{{0, 0}, {1, 1}}, {{0, 0}, {1, .8}}}, PlotLegends -> Placed[{"1", "2"}, {.8, .4}]]


When I try to apply Image[plot] I get the following error

If I remove the Placed option, however, this is fixed. Any idea how to fix this? I'm using Mathematica version 13.0.0.

• Image@Rasterize@plot Commented Jan 11, 2022 at 18:47
• or Rasterize[plot, "Image"]
– chuy
Commented Jan 11, 2022 at 19:45

p2 = ListLinePlot[{{{0, 0}, {1, 1}}, {{0, 0}, {1, .8}}}
, Epilog ->
Inset[LineLegend[{ColorData[97][1], ColorData[97][2]}, {"1",
"2"}], {.8, .4}]
]

Image[p2]


EDIT-1 From the docs: Inset[obj,pos] is one of the available syntax. In response to the OP's comment, a manipulate is being added to show usage. As soon as Placed is used, the Head changes to Legended.

Manipulate[
p3 = ListLinePlot[{
{{0, 0}, {1, 1}}
, {{0, 0}, {1, .8}}
}
, ImagePadding -> {{10, 10}, {10, 10}}
, Epilog -> Inset[LineLegend[
{ColorData[97][1]
, ColorData[97][2]}
, {"1", "2"}], {i, j}
]
],
{{i, 0.5, "x", Appearance -> Labeled}, 0.1, 1.0}
, {{j, 0.5, "y", Appearance -> Labeled}, 0.1, 1.0}
]

• Epilog and Inset is a nice solution, but I wonder what the placement here means, what exactly are those units? With Placed, it was easy to see because the position corresponded to cartesian coordinates, here I'm not sure. Any idea? Commented Jan 11, 2022 at 19:32
Rasterize[plot]


Head @ %

Image

• Is it possible to define the resolution of such image with Rasterize? Commented Jan 12, 2022 at 12:28
• @samwolfe, you can play with the options ImageResolution and RasterSize in Rasterize.
– kglr
Commented Jan 12, 2022 at 20:21

Note that "plot" has a head of "Legended".

I do not know a straight way to change this into an image: However, a work around could be to save the plot as a PNG file and then importing as an image. However, the quality could be better.

Export["d:/tmp/test.png", plot]
img = Import["d:/tmp/test.png"]


• Do you agree that I should add a bug tag?
– yode
Commented Jan 11, 2022 at 19:03
• @yode - I do not agree with a bug tag. The documentation for Image indicates that its argument should either be an array of data or a graphics. Since this plot does not have a Head of either Graphics or Graphics3D; it is not a valid input as indicated in the resulting error message. Commented Jan 11, 2022 at 19:36

## Simple solution:

ImportString[ExportString[plot, "PNG"], "PNG"]


## Lossless solution:

Export["test.svg", plot]
img = ResourceFunction["SVGImport"]["test.svg"]