Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put all legend entries on one line?

I can't seem to find a solution. I have a legend that's custom (using solution found here) and I'm trying to put multiple entries on one line rather than have each new entry under the previous one. How does one do?

red_patch = mpatches.Patch(color='red', label='The red data')
blue_patch = mpatches.Patch(color='blue', label='The blue data')
plt.legend(handles=[red_patch, blue_patch])

I'd like to have "(red marker) The red data, (blue marker) The blue data, etc..." all in one line on top. It'd be nice to have it start a new line too when there are more entries than can fit on the plot. So just like normal text I guess.

like image 393
Raksha Avatar asked Apr 26 '17 21:04

Raksha


People also ask

How do you add a legend to the outside of a graph?

Matplotlib set legend outside plot In Matplotlib, to set a legend outside of a plot you have to use the legend() method and pass the bbox_to_anchor attribute to it. We use the bbox_to_anchor=(x,y) attribute. Here x and y specify the coordinates of the legend.

How do you add a legend to a Boxplot?

Add an axes to the current figure as a subplot arrangement. Make a box and whisker plot using boxplot() method with different facecolors. To place the legend, use legend() method with two boxplots, bp1 and bp2, and ordered label for legend elements. To display the figure, use show() method.


1 Answers

If you want to have n columns in the legend, you can use

plt.legend(ncol=n)
like image 52
ImportanceOfBeingErnest Avatar answered Nov 15 '22 15:11

ImportanceOfBeingErnest