简体   繁体   English

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

[英]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. 我试图获得一个简单的pyplot条形图,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; \\ Python \\ lib \\ site-packages \\ matplotlib \\ axis.py:1035:UserWarning:无法找到刻度线间隔填充沿轴的像素距离; assuming no interval padding needed. 假设不需要间隔填充。 warnings.warn("Unable to find pixel distance along axis " warnings.warn(“无法找到沿轴的像素距离”

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. 我不知道这个。

To adjust the axis limits you want to use plt.axis rather than plt.axes . 要调整轴限制,您想使用plt.axis而不是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. 后者创建一个axes对象,并将输入解释为指定位置的rect(格式为[left, bottom, width, height] ),并且像素位置-1在图形外部,从而引起警告。

The correct code would be 正确的代码是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM