使用每月数据控制 matplotlib 中的条形宽度

发布于 2024-07-21 06:20:18 字数 871 浏览 5 评论 0原文

当我用条形图绘制每月采样的数据时,它们的宽度非常细。 如果我将 X 轴次要定位器设置为 DayLocator(),我可以看到条形宽度调整为 1 天,但我希望它们填满整个月。

我尝试将次要刻度定位器设置为 MonthLocator() ,但没有效果。

[编辑]

也许一个例子会更明确,这里有一个ipython -pylab示例来说明我的意思:

x = [datetime.datetime(2008, 1, 1, 0, 0),
    datetime.datetime(2008, 2, 1, 0, 0),
    datetime.datetime(2008, 3, 1, 0, 0),
    datetime.datetime(2008, 4, 1, 0, 0),
    datetime.datetime(2008, 5, 1, 0, 0),
    datetime.datetime(2008, 6, 1, 0, 0),
    datetime.datetime(2008, 7, 1, 0, 0),
    datetime.datetime(2008, 8, 1, 0, 0),
    datetime.datetime(2008, 9, 1, 0, 0),
    datetime.datetime(2008, 10, 1, 0, 0),
    datetime.datetime(2008, 11, 1, 0, 0),
    datetime.datetime(2008, 12, 1, 0, 0)]

y = cos(numpy.arange(12) * 2)

bar(x, y)

这给出了12个2像素宽的条,我希望它们范围更广,逐月延伸。

When I plot data sampled per month with bars, their width is very thin. If I set X axis minor locator to DayLocator(), I can see the bars width is adjusted to 1 day, but I would like them to fill a whole month.

I tried to set the minor ticks locator to MonthLocator() without effect.

[edit]

Maybe an example will be more explicit, here is an ipython -pylab example of what I mean :

x = [datetime.datetime(2008, 1, 1, 0, 0),
    datetime.datetime(2008, 2, 1, 0, 0),
    datetime.datetime(2008, 3, 1, 0, 0),
    datetime.datetime(2008, 4, 1, 0, 0),
    datetime.datetime(2008, 5, 1, 0, 0),
    datetime.datetime(2008, 6, 1, 0, 0),
    datetime.datetime(2008, 7, 1, 0, 0),
    datetime.datetime(2008, 8, 1, 0, 0),
    datetime.datetime(2008, 9, 1, 0, 0),
    datetime.datetime(2008, 10, 1, 0, 0),
    datetime.datetime(2008, 11, 1, 0, 0),
    datetime.datetime(2008, 12, 1, 0, 0)]

y = cos(numpy.arange(12) * 2)

bar(x, y)

This gives 12 2 pixels wide bars, I would like them to be wider and extend from month to month.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情未る 2024-07-28 06:20:19

只需使用 width 关键字参数:

bar(x, y, width=30)

或者,由于不同的月份有不同的天数,为了使其看起来不错,您可以使用一个序列:

bar(x, y, width=[(x[j+1]-x[j]).days for j in range(len(x)-1)] + [30])

Just use the width keyword argument:

bar(x, y, width=30)

Or, since different months have different numbers of days, to make it look good you can use a sequence:

bar(x, y, width=[(x[j+1]-x[j]).days for j in range(len(x)-1)] + [30])
溺深海 2024-07-28 06:20:19

较新的 matplotlib 版本(例如 3.7.0)可以直接处理 datetime 和 timedelta,因此只需计算差异(可能使用 np.diff())就足以计算宽度列表。

import datetime
import numpy as np
import matplotlib.pyplot as plt

x = [datetime.datetime(2008, i, 1) for i in range(1, 13)]
y = np.cos(np.arange(12) * 2)

# plot a bar chart with timedelta x-axis
plt.bar(x, y, width=[*np.diff(x), datetime.timedelta(31)]);

另一种选择是只绘制条形图,而不用担心 x 轴,但稍后添加日期时间数据作为刻度标签。

plt.bar(range(len(x)), y)
plt.xticks(range(0, len(x), 2), [d.strftime('%Y-%m') for d in x[::2]]);

结果

Newer matplotlib versions (e.g. 3.7.0) can handle datetime and timedelta directly, so just computing the difference (perhaps with np.diff()) would suffice to compute a list of widths.

import datetime
import numpy as np
import matplotlib.pyplot as plt

x = [datetime.datetime(2008, i, 1) for i in range(1, 13)]
y = np.cos(np.arange(12) * 2)

# plot a bar chart with timedelta x-axis
plt.bar(x, y, width=[*np.diff(x), datetime.timedelta(31)]);

Another option is to just plot a bar chart without worrying about x-axis but add datetime data as ticklabels later.

plt.bar(range(len(x)), y)
plt.xticks(range(0, len(x), 2), [d.strftime('%Y-%m') for d in x[::2]]);

result

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文