2
$\begingroup$

There is a way to put a legend into a graphics? For example, given the Graphics

Graphics[{{Black, 
   Line[{{1, 1}, {2, 3}, {-3, 4}}]}, {PointSize[0.015], {Red, 
    Point[{1, 1}]}, {Green, Point[{2, 3}]}, {Blue, Point[{-3, 4}]}}}, 
 Axes -> True, AxesLabel -> {"x", "y"}]

I was able to put it in a Legended

 Legended[Graphics[{{Black, 
        Line[{{1, 1}, {2, 3}, {-3, 4}}]}, {PointSize[0.015], {Red, 
         Point[{1, 1}]}, {Green, Point[{2, 3}]}, {Blue, Point[{-3, 4}]}}},
       Axes -> True, AxesLabel -> {"x", "y"}], 
     PointLegend[{Red, Green, Blue}, {"A", "B", "C"}]]

but unfortunately, I was not able to do the contrary. So is it possible to do it? Could someone help me, please?

$\endgroup$
9
  • $\begingroup$ perhaps it's Inset what you're looking for $\endgroup$
    – joka
    Nov 25 at 13:40
  • $\begingroup$ Unfortuantely it seems does not work. Ineed I just tried to put Graphics[{{Black, Line[{{1, 1}, {2, 3}, {-3, 4}}]}, {PointSize[0.015], {Red, Point[{1, 1}]}, {Green, Point[{2, 3}]}, {Blue, Point[{-3, 4}]}}}, Axes -> True, AxesLabel -> {"x", "y"}, Inset[PointLegend[{Red, Green, Blue}, {"A", "B", "C"}]]] but it does not work. $\endgroup$ Nov 25 at 13:44
  • $\begingroup$ Legends are used for plots. Are these legends going to be used as display items only? You can use a plot with any legend you want as p1 and then Inset that plot in your Graphics. $\endgroup$
    – Syed
    Nov 25 at 13:54
  • $\begingroup$ @Syed I have to create a graphics with a legend and so export it into an image png. $\endgroup$ Nov 25 at 13:55
  • $\begingroup$ @Syed The following could be a possible solution: however If I indate as potion Right in place of {2,4} the result is stranger. $\endgroup$ Nov 25 at 13:58
4
$\begingroup$
Graphics[{Black, Line[{{1, 1}, {2, 3}, {-3, 4}}], PointSize[0.015], 
  Red, Point[{1, 1}], 
  Green, Point[{2, 3}], 
  Blue, Point[{-3, 4}], 
  Inset[PointLegend[{Red, Green, Blue}, {"A", "B", "C"}], Scaled[{1.1, .5}]]},
 Axes -> True, AxesLabel -> {"x", "y"}]

enter image description here

$\endgroup$
3
$\begingroup$

Recommend using ListPlot

data = {{1, 1}, {2, 3}, {-3, 4}};

ListPlot[List /@ data,
 Prolog -> {Gray, Line[data]},
 AxesLabel ->
  (Style[#, 12, Bold] & /@ {"x", "y"}),
 PlotStyle -> {Red, Green, Blue},
 PlotMarkers -> Automatic,
 PlotLegends ->
  Placed[{"A", "B", "C"}, {0.3, 0.4}]]

enter image description here

$\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.