7

I could not get all the legends to appear in matplotlib.

My Labels array is:

lab = ['Google', 'MSFT', 'APPL', 'EXXON', 'WMRT']

I use the below code to add the legend:

ax.legend(lab,loc="best")

Am seeing only 'Google' in top right corner. How to show all labels? enter image description here

Full code:

import numpy as np
import matplotlib.pyplot as plt
from itertools import cycle, islice

menMeans = (8092, 812, 2221, 1000, 562)
N = len(menMeans)

lab = ['Google', 'MSFT', 'APPL', 'EXXON', 'WMRT']

ind = np.arange(N)  # the x locations for the groups
width = 0.35       # the width of the bars

fig, ax = plt.subplots()
my_colors = list(islice(cycle(['b', 'r', 'g', 'y', 'k']), None, N))
rects1 = ax.bar(ind, menMeans, width, color=my_colors,label=lab)

# add some
ax.set_ylabel('Count')
ax.set_title('Trending words and their counts')
ax.set_xticks(ind+width)
ax.legend(lab,loc="best")
plt.show()
NEO
  • 1,961
  • 8
  • 34
  • 53
  • Can you give us your full code? – Ffisegydd Apr 26 '14 at 22:24
  • 1
    Your problem is that you're only plotting once, using a single `ax.bar`. As such your legend will only have one item. The fact that you have modified the colours of your other bars is, unfortunately, immaterial. – Ffisegydd Apr 26 '14 at 22:27

2 Answers2

4

The answer by @Ffisegydd may be more useful*, but it doesn't answer the question. Simply create separate bar charts for the legend, and you'll get the desired result:

for x,y,c,lb in zip(ind,menMeans,my_colors,lab):
    ax.bar(x, y, width, color=c,label=lb)

ax.legend()

enter image description here

* To see why this presentation may be harmful, consider what would happen if the viewer was colorblind (or this was printed in black and white).

Hooked
  • 84,485
  • 43
  • 192
  • 261
3

Your problem is that you're only plotting once with a single ax.bar and as such your legend can only have one item (for that one plot). One way to get around this, for your plotting script, is to change the xticks and xticklabels as shown below.

When you create a legend you create an entry for each matplotlib.artist object that you have created through plotting. These objects could be a set of data points, or a line, or a set of bars in a bar chart. It doesn't matter if you have 5 or 10 bars in your bar chart as you've still only plotted one bar chart. This means you will end up only having one entry in your legend.

I have used ax.set_xticks(ind+width/2) to place the tick positions directly underneath your bars, I have then set these labels using your lab list with ax.set_xticklabels(lab).

import numpy as np
import matplotlib.pyplot as plt
from itertools import cycle, islice
import matplotlib.ticker as mtick

menMeans = (8092, 812, 2221, 1000, 562)
N = len(menMeans)

lab = ['Google', 'MSFT', 'APPL', 'EXXON', 'WMRT']

ind = np.arange(N)  # the x locations for the groups
width = 0.35       # the width of the bars

fig, ax = plt.subplots()

my_colors = list(islice(cycle(['b', 'r', 'g', 'y', 'k']), None, N))

rects1 = ax.bar(ind, menMeans, width, color=my_colors,label=lab)

ax.set_ylabel('Count')
ax.set_title('Trending words and their counts')

plt.xticks(rotation=90)

ax.set_xticks(ind+width/2)
ax.set_xticklabels(lab)

plt.show()

Plot

Ffisegydd
  • 51,807
  • 15
  • 147
  • 125
  • I need to show as legend at top right. @Ffisegydd Can you show the same for legends in top right instead of labels in xaxis? – NEO Apr 26 '14 at 22:35
  • https://www.dropbox.com/s/tq03e6nofatzx1u/Screenshot%202014-04-26%2018.39.47.png I used ax.legend(lab). Only 'Google' is appearing. How to get other labels? – NEO Apr 26 '14 at 22:40
  • +1 Even though you do not directly solve the problem of the legend, this solution is superior in the graphical presentation of information. The bars represent different information when going from left to right. Why not label them right there, instead of repeating the colors top-down in a legend. – Bas Swinckels Apr 26 '14 at 22:45
  • Because what I showed is just sample data. I have 10+ items in xaxis and each of them have string length of 10+. They overlap each other. Setting small font size makes them unreadable, that's y opting for legend at top right – NEO Apr 26 '14 at 22:46
  • I have edited the question slightly to take into account this fact, the edit adds a rotation to the x-ticks. This should help you fit in more data bars if needs be. – Ffisegydd Apr 26 '14 at 22:48
  • @Ffisegydd I did try that and it does seems good. But can you tell why I could not get more than one label in the legend? – NEO Apr 26 '14 at 22:54
  • I've added more text to the answer, hopefully this will help explain it to you. If not please let me know and I can add more detail. – Ffisegydd Apr 26 '14 at 22:59