Raj
Raj

Reputation: 13

matplotlib.pyplot plt.bar() warning when I try to set plt.axes()

I'm trying to getnerate a simple pyplot bargraph, Python 3.6.0. Code is below:

import random
import matplotlib.pyplot as plt
import collections.Counter

#Generate random number of friends(0-19) for 100 users
num_freinds = [random.randrange(20) for _ in range(100)]
#Define a Counter to see how Friend Counts vary
c = Counter(num_freinds)

#Plot Friend Count vs Number of users with that Friend Count
xs = [i for i in range(20)]
ys = [c[i] for i in xs]
plt.bar(xs, ys)
plt.axes([-1, 20, 0, 12])
plt.xticks([i for i in range(21)])
plt.yticks([i for i in range(13)])
plt.xlabel("# of Friends")
plt.ylabel("Count of Users")
plt.show()

I get the following error when I run the code:

\Python\lib\site-packages\matplotlib\axis.py:1035: UserWarning: Unable to find pixel distance along axis for interval padding of ticks; assuming no interval padding needed. warnings.warn("Unable to find pixel distance along axis "

The problem seems to be the following line of code:

plt.axes([-1, 20, 0, 12])

If I comment out the above line, everything works fine, no warning.

Can anybody explain why setting the axes seems to be causing the pixel warning? The axes ranges are in line with the data. I can't figure this out.

Upvotes: 1

Views: 1286

Answers (1)

Suever
Suever

Reputation: 65430

To adjust the axis limits you want to use plt.axis rather than plt.axes. The latter creates an axes object and it interprets the input to be a rect specifying the location (in the format [left, bottom, width, height]) and a pixel location of -1 is outside of your figure which is causing the warning.

The correct code would be

plt.bar(xs, ys)
plt.axis([-1, 20, 0, 12])

Upvotes: 1

Related Questions