在绘图中绘制分组的 pandas 数据 [英] Plotting a grouped pandas data in plotly
问题描述
我有一个熊猫数据框,看起来像这样:
I have a pandas dataframe which looks like this:
A B
1 USA Y
3 USA Y
4 USA N
5 India Y
8 India N
12 USA N
14 USA Y
19 USA Y
我想为此数据帧创建一个countplot
.也就是说,该图在X-axis
上具有国家名称,在Y-axis
上具有每个类别的计数.我知道我可以像这样在seaborn
中做到这一点:
I want to make a countplot
for this dataframe. That is, the plot will have country names on X-axis
and the counts for each category on Y-axis
. I know I can do this in seaborn
like this:
sns.countplot(x='A', data=df, hue='B')
但这不是交互式情节.我想在plotly
中实现相同的目的,但是我很难弄清楚.有人可以帮我吗?
But this will not be an interactive plot. I want to achieve the same thing in plotly
but I am having a hard time figuring it out. Can anyone please help me out?
推荐答案
使用plotly 3,您可以执行以下操作:
Using plotly 3 you can do something like this:
from plotly import graph_objs as go
fig = go.Figure()
for name, group in df.groupby('B'):
trace = go.Histogram()
trace.name = name
trace.x = group['A']
fig.add_trace(trace)
您还可以通过设置trace.marker.color
属性来更改其他属性,例如颜色.
you can also change other properties like the colors by setting trace.marker.color
attribute.
这篇关于在绘图中绘制分组的 pandas 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!