带有基于多个数据框列的刻度的条形图
如何从数据框中的 bin 中在 matplotlib (或 pandas)中制作条形图?
我想要像下面这样的东西,其中 x 轴标签来自数据帧中的 low
、high
(所以第一个刻度将读取 [-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.
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用低、高列创建一个新列。
将 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
usingdf.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