Matplotlib - 直方图 - 第一个 bin 不从 X 轴的开头开始
在这篇文章中,我不会讨论如何计算垃圾箱。
我希望第一个 bin 位于 X 轴的开头。
换句话说:
我希望第一个 bin 位于 Y 轴的位置
这是我的源代码:
import random
import pandas as pd
from matplotlib import pyplot as plt
# Preparing the variable
data = {}
data["values"] = [random.randint(0,50) for x in range(200)]
df = pd.DataFrame(data)
# Creation of the figure
plt.figure(figsize = (10, 10))
plt.subplot(1, 1, 1)
# Creation of my bins
mybins = [-0.5+x for x in range(0,52)]
# Creation of the chart
plt.hist(df["values"], bins=mybins,
rwidth=0.7, align="mid")
plt.xticks(list(range(0, 51, 5)))
plt.show()
作为输出,我得到这个直方图:
正如你所看到的,有空白Y 轴和第一个 bin 之间的空间。
In this post i'm not talking about how bins are calculated.
I want that the first bin gets located at the beginning of the X-axis.
In other words :
I want that the first bin gets located where is the Y-axis
Here is my source code :
import random
import pandas as pd
from matplotlib import pyplot as plt
# Preparing the variable
data = {}
data["values"] = [random.randint(0,50) for x in range(200)]
df = pd.DataFrame(data)
# Creation of the figure
plt.figure(figsize = (10, 10))
plt.subplot(1, 1, 1)
# Creation of my bins
mybins = [-0.5+x for x in range(0,52)]
# Creation of the chart
plt.hist(df["values"], bins=mybins,
rwidth=0.7, align="mid")
plt.xticks(list(range(0, 51, 5)))
plt.show()
As an output i get this histogram :
As you can see, there is blank space between the Y-axis and the first bin.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论