如何根据计数制作水平堆叠的历史图?

发布于 2025-01-20 09:06:44 字数 784 浏览 1 评论 0原文

我有一个 df,它代表 3 个时间点(1hr、2hr 和 3hr)的三种状态(S1、S2、S3)。我想显示状态的堆积条形图,但堆栈是不连续的或至少不是累积的。如何在 Seaborn 中解决此问题?重要的是,时间位于 y 轴,状态计数位于 x 轴。

下面是一些代码。

data = [[3, 2, 18],[4, 13, 6], [1, 2, 20]]
df = pd.DataFrame(data, columns = ['S1',  'S2', 'S3'])
df = df.reset_index().rename(columns = {'index':'Time'})
melt = pd.melt(df, id_vars = 'Time')

plt.figure()
sns.histplot(data = melt,x = 'value', y = 'Time', bins = 3, hue = 'variable', multiple="stack")

编辑: 这就是我正在寻找的东西,我希望这能给你一个想法。请忽略框之间比例的差异...

在此处输入图像描述

I have a df which represents three states (S1, S2, S3) at 3 timepoints (1hr, 2hr and 3hr). I would like to show a stacked bar plot of the states but the stacks are discontinous or at least not cumulative. How can I fix this in Seaborn? It is important that time is on the y-axis and the state counts on the x-axis.

enter image description here

Below is some code.

data = [[3, 2, 18],[4, 13, 6], [1, 2, 20]]
df = pd.DataFrame(data, columns = ['S1',  'S2', 'S3'])
df = df.reset_index().rename(columns = {'index':'Time'})
melt = pd.melt(df, id_vars = 'Time')

plt.figure()
sns.histplot(data = melt,x = 'value', y = 'Time', bins = 3, hue = 'variable', multiple="stack")

EDIT:
This is somewhat what I am looking for, I hope this gives you an idea. Please ignore the difference in the scales between boxes...

enter image description here

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

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

发布评论

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

评论(2

九公里浅绿 2025-01-27 09:06:44

如果我正确理解,我认为您要使用value作为权重:

sns.histplot(
    data=melt, y='Time', hue='variable', weights='value',
    multiple='stack', shrink=0.8, discrete=True,
)

< img src =“ https://i.sstatic.net/mbo7k.png” alt =“在此处输入图像说明”>

If I understand correctly, I think you want to use value as a weight:

sns.histplot(
    data=melt, y='Time', hue='variable', weights='value',
    multiple='stack', shrink=0.8, discrete=True,
)

enter image description here

慕烟庭风 2025-01-27 09:06:44

这在seaborn 中相当困难,因为它本身不支持堆叠条。您可以使用 pandas 的内置绘图,或尝试使用绘图表达。

data = [[3, 2, 18],[4, 13, 6], [1, 2, 20]]
df = pd.DataFrame(data, columns = ['S1',  'S2', 'S3'])
df = df.reset_index().rename(columns = {'index':'Time'})
# so your y starts at 1
df.Time+=1

melt = pd.melt(df, id_vars = 'Time')
# so y isn't treated as continuous
melt.Time = melt.Time.astype('str')

熊猫可以做到这一点,但将标签放在那里有点痛苦。四处看看,看看该怎么做。

df.set_index('Time').plot(kind='barh', stacked=True)

输入图片这里的描述

Plotly 让它更容易:

import plotly.express as px
px.bar(melt, x='value', y='Time', color='variable', orientation='h', text='value')

在此处输入图像描述

This is pretty tough in seaborn as it doesn't natively support stacked bars. You can use either the builtin plot from pandas, or try plotly express.

data = [[3, 2, 18],[4, 13, 6], [1, 2, 20]]
df = pd.DataFrame(data, columns = ['S1',  'S2', 'S3'])
df = df.reset_index().rename(columns = {'index':'Time'})
# so your y starts at 1
df.Time+=1

melt = pd.melt(df, id_vars = 'Time')
# so y isn't treated as continuous
melt.Time = melt.Time.astype('str')

Pandas can do it, but getting the labels in there is a bit of pain. Check around to figure out how to do it.

df.set_index('Time').plot(kind='barh', stacked=True)

enter image description here

Plotly makes it easier:

import plotly.express as px
px.bar(melt, x='value', y='Time', color='variable', orientation='h', text='value')

enter image description here

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