Create legend for dodge boxplot in Makie


I am trying to get a legend for a box plot created in Makie. For the example provided in the website boxplot · Makie (attached is the box plot), how to get a legend inside the figure panel? Like in a scatter plot, I expect two legend items - one for each color.


How to do it?

Hi, I am not sure if there is an easier way, but I am currently handling such situations as follows

	xs = rand(1:3, 1000)
	ys = randn(1000)
	dodge = rand(1:2, 1000)
	f,a,p = boxplot(xs, ys, dodge = dodge, show_notch = true, color = dodge)

	# get current colormap
	cmap = getproperty(ColorSchemes, p.colormap[])
	# get colors used (length=2 in this case)
	ccolors = cmap[range(start=0.0, stop=1.0; length=length(unique(dodge)))]

	# build the marker elements
	elems = [[MarkerElement(color = col, marker=:circle, markersize = 15,
          strokecolor = :black)] for col in ccolors]
	axislegend(a, elems, ["First text", "Second text"]; position=:rt)

1 Like

It’s working! Thank you. :slight_smile: