Kiarash
Kiarash

Reputation: 8028

matplotlib.pyplot overlaying axis labels on bars

I have this code that generates this graph but the text is so awfully off. I tried horizontal and vertical adjustments and rotations to make it readable with no success.

Could someone please tell me how I could relocate the text and choose a better font/size so they are readable?

Any suggestions on a better graph to use to show this data is very welcome.

enter image description here

import matplotlib.pyplot as plt

N = 13
ind = np.arange(N) 
width = 0.8

good=[0, 1, 28, 144, 0, 41, 23, 5, 7, 2, 3, 9, 48]
bad=[0, 1, 28, 144, 0, 41, 23, 5, 7, 2, 3, 9, 48]
keys=[u'gaming', u'recreation', u'business', u'computer_internet', u'unknown', u'culture_politics', u'science_technology', u'law_crime', u'sports', u'religion', u'weather', u'health', u'arts_entertainment']
c2='r'
c1='b'
p1 = plt.bar(ind, good, width, color=c1)
p2 = plt.bar(ind, bad, width, color=c2, bottom=good)

plt.ylabel('Number of URLs scanned')
plt.title('Category')
plt.xticks(ind+width/2., keys, rotation='vertical')
#plt.yticks(np.arange(0,81,10), rotation='vertical')
plt.legend( (p1[0], p2[0]), ('good', 'bad') )

plt.show()

Upvotes: 0

Views: 1634

Answers (2)

mwaskom
mwaskom

Reputation: 49032

You could try a horizontal barplot. It often works better for this kind of visualization. I've also made a few tweaks to sort the categories and use better colors:

import numpy
import matplotlib.pyplot as plt

good = np.array([0, 1, 28, 144, 0, 41, 23, 5, 7, 2, 3, 9, 48])
bad = np.array([0, 1, 28, 144, 0, 41, 23, 5, 7, 2, 3, 9, 48])
keys = np.array([u'gaming', u'recreation', u'business', u'computer_internet',
                 u'unknown', u'culture_politics', u'science_technology',
                 u'law_crime', u'sports', u'religion', u'weather',
                 u'health', u'arts_entertainment'])
ind = np.arange(1, len(keys) + 1) 

order = np.argsort(good + bad)
good, bad, keys = good[order], bad[order], keys[order]

c2 = '#FF7777'
c1 = '#7777FF'
p1 = plt.barh(ind, good, color=c1, align="center", label="good")
p2 = plt.barh(ind, bad, color=c2, left=good, align="center", label="bad")

plt.xlabel('Number of URLs scanned')
plt.yticks(ind, keys)
plt.legend(loc="center right")
plt.tight_layout()

enter image description here

Upvotes: 1

wflynny
wflynny

Reputation: 18551

Try some of the following to adjust the bottom margins:

plt.tight_layout() or fig.savefig(bbox_inches='tight') to try and let matplotlib fix it on its own.

You can manually adjust the margins with

#adjust these as needed
plt.subplots_adjust(bottom=0.1, top=0.9, left=0.05, right=0.95)

Edit:

If you'd rather put the labels on the plot next to the bars, you can do something like

N = 13
width = 0.4
ind = np.arange(N) + 0.5 #this puts the bars on the right of each "bin"
p1 = plt.bar(ind, good, width, color=c1)
p2 = plt.bar(ind, good, width, color=c2, bottom=good)
for rect, key in zip(p1, keys):
    x, y = rect.get_xy()
    plt.text(x-width, y, text, va='bottom', ha='left', rotation=90)

You may want to add a bit of a buffer to the y coordinate (like y+0.01 or something).

Upvotes: 1

Related Questions