我正在尝试使用简单的pyplot条形图,Python 3.6.0。代码如下:
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()
运行代码时出现以下错误:
\ Python \ lib \ site-packages \ matplotlib \ axis.py:1035:UserWarning:无法找到沿轴的像素距离,用于间隔填充刻度;假设不需要间隔填充。 warnings.warn(“无法找到沿轴的像素距离”
问题似乎是以下代码行:
plt.axes([-1, 20, 0, 12])
如果我注释掉上述内容,一切正常,没有警告。
有人可以解释为什么设置轴似乎会导致像素警告吗?轴范围与数据一致。我无法弄清楚这一点。