如何根据计数制作水平堆叠的历史图?
我有一个 df,它代表 3 个时间点(1hr、2hr 和 3h
r)的三种状态(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 3h
r). 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.
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我正确理解,我认为您要使用
value
作为权重:< img src =“ https://i.sstatic.net/mbo7k.png” alt =“在此处输入图像说明”>
If I understand correctly, I think you want to use
value
as a weight:这在seaborn 中相当困难,因为它本身不支持堆叠条。您可以使用 pandas 的内置绘图,或尝试使用绘图表达。
熊猫可以做到这一点,但将标签放在那里有点痛苦。四处看看,看看该怎么做。
Plotly 让它更容易:
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.
Pandas can do it, but getting the labels in there is a bit of pain. Check around to figure out how to do it.
Plotly makes it easier: