Matplotlib - 直方图 - 第一个 bin 不从 X 轴的开头开始

发布于 2025-01-09 14:15:08 字数 829 浏览 5 评论 0原文

在这篇文章中,我不会讨论如何计算垃圾箱。

我希望第一个 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()

作为输出,我得到这个直方图:

histogram_first_bin_shifted

正如你所看到的,有空白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 :

histogram_first_bin_shifted

As you can see, there is blank space between the Y-axis and the first bin.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文