Plotting a grouped pandas data in plotly

荒凉一梦 提交于 2020-01-02 05:13:24

问题


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   

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')

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?


回答1:


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)

you can also change other properties like the colors by setting trace.marker.color attribute.



来源:https://stackoverflow.com/questions/53283570/plotting-a-grouped-pandas-data-in-plotly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!