带有基于多个数据框列的刻度的条形图

发布于 2025-01-11 13:59:37 字数 742 浏览 2 评论 0原文

如何从数据框中的 bin 中在 matplotlib (或 pandas)中制作条形图?

我想要像下面这样的东西,其中 x 轴标签来自数据帧中的 lowhigh (所以第一个刻度将读取 [-1.089, 0) y 值是我的数据框中的 percent 列。

输入图片此处描述

这是一个示例数据集。数据集已经采用这种格式(我没有未剪切的版本)。

df = pd.DataFrame(
    {
        "low": [-1.089, 0, 0.3, 0.5, 0.6, 0.8],
        "high": [0, 0.3, 0.5, 0.6, 0.8, 10.089],
        "percent": [0.509, 0.11, 0.074, 0.038, 0.069, 0.202],
    }
)
display(df)

输入图片此处描述

How can I make a bar chart in matplotlib (or pandas) from the bins in my dataframe?

I want something like this, below, where the x-axis labels come from the low, high in my dataframe (so first tick would read [-1.089, 0) and the y value is the percent column in my dataframe.

enter image description here

Here is an example dataset. The dataset is already in this format (I don't have an uncut version).

df = pd.DataFrame(
    {
        "low": [-1.089, 0, 0.3, 0.5, 0.6, 0.8],
        "high": [0, 0.3, 0.5, 0.6, 0.8, 10.089],
        "percent": [0.509, 0.11, 0.074, 0.038, 0.069, 0.202],
    }
)
display(df)

enter image description here

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

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

发布评论

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

评论(1

居里长安 2025-01-18 13:59:37

使用低、高列创建一个新列。

将 low 和 high 列中的 int 值转换为 str 类型,并以所需的 [,) 表示法设置新的 str。

从那里,您可以使用 df.plot.bar() 直接从 df 创建条形图,将新创建的列指定为 x,将百分比指定为 y。

https://pandas.pydata.org/docs/参考/api/pandas.DataFrame.plot.bar.html

Create a new column using the the low, high cols.

Covert the int values in the low and high columns to str type and set the new str in the [<low>, <high>) notation that you want.

From there, you can create a bar plot dirrectly from df using df.plot.bar(), assigning the newly created column as x and percent as y.

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.plot.bar.html

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