10

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.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Raksha
  • 1,572
  • 2
  • 28
  • 53

1 Answers1

19

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

plt.legend(ncol=n)
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • There isn't an actual legend though, so if I do that, I get an error: `"UserWarning: No labelled objects found. Use label='...' kwarg on individual plots. warnings.warn("No labelled objects found. ""` – Raksha Apr 26 '17 at 21:36
  • 1
    nevermind, it totally worked ... \*facepalm\* ... I was confusing rows with columns and kept wondering why `ncol=1` didn't make one line ... I guess this'll do. Thanks! – Raksha Apr 26 '17 at 21:49
  • +1. It would be good to indicate there is no general option to have one line (without adjusting manually the number of columns when a plot is added), if this is the case. – mins Jul 07 '23 at 10:10