# Adding legends in a graphic [closed]

I have several datasets, and I successfully plotted them as points in a Graphic. Then, how can I automatically create legends similar to PlotLegends with a color and a label for each dataset in the graphic? And the colors of the dataset points and the legend are consistent.

My goal is to plot a graph like this Note: the intervals of x-value of my dataset are not equal, therefore I prefer not to use ListPlot.

Extract your data using the form:

mydata = (Point[{{1, 2}, {3, 4}, {5, 6}}] /. Point -> List)

then ListPlot[mydata, Legended->True]

or

PlotLegends -> {"mydata1","mydata2"}

• I prefer not to use ListPlot, though. Because the x-value of my dataset is not equally spaced. – chika Jul 17 '18 at 3:14
• ListPlot is perfect for datasets in which the $x$-axis data is not evenly distributed. – David G. Stork Jul 17 '18 at 3:15
• @chika, ListPlot nicely draws the data in form {{x,y},...}. The alignment of the legend inside the plot can be defined by PlotLegends->Placed[LineLegend[{styles},{labels}],{position}] – Rom38 Jul 17 '18 at 4:28
• ListPlot really works! Thanks everybody. – chika Jul 17 '18 at 15:32

Have you checked the documentation yet? Here.

This is an example there.

ListLinePlot[Table[Accumulate[RandomReal[{-1, 1}, 250]], {3}],
Filling -> Axis, PlotLegends -> {"one", "two", "three"}] Fake datasets with a structure similar to the structure of your datasets:

SeedRandom
graphics = Table[{PointSize[.001],
Point @ Transpose[{x = Sort @ RandomInteger[1000, 5000],
RandomInteger Sin[RandomReal[2 Pi] + RandomReal[2 Pi] x / 500] +
RandomReal[5, 5000]}]}, {5}];
g1 = Graphics[graphics, AspectRatio -> 1/GoldenRatio,
ImageSize -> 400, Frame -> True];


Colored version of the same:

colors = ColorData[63, "ColorList"][[;; Length@graphics]];
g2 = Graphics[Transpose[{colors, graphics}],
AspectRatio -> 1/GoldenRatio, ImageSize -> 400, Frame -> True]


using LineLegend and Legended

labels = Style["dataset " <> ToString[#], 16] & /@ Range[Length@graphics];
legend = LineLegend[Directive[#, AbsoluteThickness] & /@ colors, labels];
Row[{g1, Legended[g2, Placed[legend, {Left, Top}]]}, Spacer] 