-1

Does anyone have a simple way to change the size of the legend to make it larger or make the border bold?

  • a-o are just random ints
group_a = (a,b,c,d,e)
group_b = (f,g,h,i,j)
group_c = (k,l,m,n,o)

width = 0.2

x = np.arange(5)

plt.bar(x-0.2, group_a, width, color = 'cyan')
plt.bar(x, group_b, width, color = 'orange')
plt.bar(x+0.2, group_c, width, color = 'green')

plt.xticks(x, ['1','2','3','4','5'])
plt.xlabel("quarter")

plt.ylabel('%')

plt.legend(['Group A','Group B','Group C'])


print(f'Group A: {a},{b},{c},{d},{e}')
print(f'Group B: {f},{g},{h},{i},{j}')
print(f'Group C: {k},{l},{m},{n},{o}')

plt.show()
nji1997
  • 11
  • 5

1 Answers1

0

You can use the loc keyword for plt.legend():

plt.legend(['Group A','Group B','Group C'], loc=(1.04, 1))

Output:enter image description here

Nin17
  • 2,821
  • 2
  • 4
  • 14
  • @nji1997 if this was useful, consider accepting it as the answer – Nin17 Apr 29 '22 at 16:38
  • Apart from having many duplicates on stackoverflow, this answer is missing the importance of combining `bbox_to_anchor=` and `loc=`. – JohanC Apr 29 '22 at 18:34
  • @JohanC care to elaborate? – Nin17 Apr 29 '22 at 18:41
  • See https://stackoverflow.com/questions/4700614/how-to-put-the-legend-outside-the-plot-in-matplotlib If you just give `loc=(1.04, 1)` instead of one of the strings, you can only position the legend via the lower left corner. This "works", but is very inflexible. Moreover, the same question has been asked many many times before, and should be marked as a duplicate. If you have a better answer, you can still add that to the original question. (But in this case it would be extremely hard to improve an answer with that many up-votes) – JohanC Apr 29 '22 at 18:56
  • @JohanC The answer you flagged was not helpful in my case. – nji1997 Apr 29 '22 at 21:41
  • @Nin17 your answer was exactly what i was looking for – nji1997 Apr 29 '22 at 21:42