当我尝试设置plt.axes()时,matplotlib.pyplot plt.bar()警告

时间:2017-04-05 19:52:32

标签: python matplotlib

我正在尝试使用简单的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])

如果我注释掉上述内容,一切正常,没有警告。

有人可以解释为什么设置轴似乎会导致像素警告吗?轴范围与数据一致。我无法弄清楚这一点。

1 个答案:

答案 0 :(得分:0)

要调整轴限制,请使用plt.axis而不是plt.axes。后者创建一个axes对象,它将输入解释为指定位置的矩形(格式为[left, bottom, width, height]),像素位置为-1在你的身影之外引起警告。

正确的代码是

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