如何制作一个不会从y = 0开始

发布于 2025-02-09 12:24:54 字数 70 浏览 2 评论 0原文

我想在Python中制作一个具有自己的“基线”的Barchart,因此Y-AS不在0开始(通常像通常一样),而是我选择的价值。

I want to make an barchart in python that has its own 'baseline', thus that the y-as doesn't start at 0 (as it usually does) but my chosen value.

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

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

发布评论

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

评论(1

只是在用心讲痛 2025-02-16 12:24:54

您可以将底部参数用于 matplotlib.pyplot.bar 。可以在plt.bar呼叫中使用,也可以在pandas.dataframe.plot.bar中使用,该将将关键字参数传递到matplotlib。底部的参数将添加到数据框中的所有值中,因此您需要在绘制之前从数据框架的值中减去它。

例如:

import pandas as pd, numpy as np
years = np.arange(1880, 2022)
df = pd.DataFrame({
    'co2_concentration': (
        np.sin((years / 50 - 1)* np.pi) * 0.2
        + np.arange(len(years)) / len(years)
        + 56.7
    )},
    index=years,
)
base = 57.1
(df.co2_concentration - base).plot.bar(bottom=base, width=1)
plt.xticks(range(0, len(years), 20))

“在此处输入图像描述”

You can use the bottom argument to matplotlib.pyplot.bar. This can be used in a plt.bar call or in pandas.DataFrame.plot.bar, which will pass keyword arguments through to matplotlib. The bottom argument will be added to all values in the dataframe, so you need to subtract it from the dataframe's values prior to plotting.

For example:

import pandas as pd, numpy as np
years = np.arange(1880, 2022)
df = pd.DataFrame({
    'co2_concentration': (
        np.sin((years / 50 - 1)* np.pi) * 0.2
        + np.arange(len(years)) / len(years)
        + 56.7
    )},
    index=years,
)
base = 57.1
(df.co2_concentration - base).plot.bar(bottom=base, width=1)
plt.xticks(range(0, len(years), 20))

enter image description here

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