【问题标题】:Plotly: How to plot grouped results on multiple lines?Plotly:如何在多行上绘制分组结果?
【发布时间】:2019-08-14 21:46:27
【问题描述】:

我正在尝试根据“Plotly”中的类别和日期的 ID 计数创建多个折线图 我的日期包含三列“日期”、“类别”、“ID”

我现在使用此代码绘制了一条线

b=mdata.groupby(['Date']).count()['ID ']
b=b.sort_index(ascending=True)


xScale = b.index
yScale = b.values
trace =go.Scatter(
    x = xScale,
    y = yScale,
    marker=dict(
        color='Red')

)
data2 = [trace]
graphJSON2 = json.dumps(data2, cls=plotly.utils.PlotlyJSONEncoder)

输出图表应在 X 轴上具有日期,在 Y 轴上具有 ID 计数,并且基于类别的多条线

【问题讨论】:

    标签: python pandas d3.js plotly


    【解决方案1】:

    据我所知,您将不得不使用像 pandas.DataFrame.pivot 这样的方法来获得您在此处寻找的数据结构:

    pd.pivot_table(df, values='ID', index=['Date'],columns='Category', aggfunc=np.sum)
    

    下面是一个完整的方法,它应该使用以下示例数据框适合您的数据集的描述:

    数据:

             Date  ID Category
    0  2013-01-02   1        A
    1  2013-01-02   3        B
    2  2013-01-03   1        C
    3  2013-01-03   2        B
    4  2013-01-03   1        B
    5  2013-01-03   3        A
    6  2013-01-03   3        A
    7  2013-01-03   4        A
    8  2013-01-04   4        B
    9  2013-01-04   4        C
    10 2013-01-05   1        B
    11 2013-01-06   2        A
    

    剧情:

    代码:

    import plotly.graph_objs as go
    import pandas as pd
    import numpy as np
    
    # sample dataframe to match OPs structure
    df = pd.DataFrame({'Date' : [pd.Timestamp('20130102'), pd.Timestamp('20130102'), 
                                 pd.Timestamp('20130103'), pd.Timestamp('20130103'),
                                 pd.Timestamp('20130103'), pd.Timestamp('20130103'),
                                 pd.Timestamp('20130103'), pd.Timestamp('20130103'),
                                 pd.Timestamp('20130104'), pd.Timestamp('20130104'),
                               pd.Timestamp('20130105'),pd.Timestamp('20130106')],
                        'ID' : [1, 3, 1, 2, 1 , 3,3,4,4,4,1,2],
                        'Category' : pd.Categorical(["A","B","C","B","B","A",
                                                     "A","A","B","C","B","A"  ])})
    # data munging to get OPs desired plot
    df = pd.pivot_table(df, values='ID', index=['Date'],columns='Category', aggfunc=np.sum)
    
    # ploty
    fig = go.Figure()
    for col in df.columns:
        fig.add_trace(go.Scatter(x=df.index, y=df[col].values,
                                 name = col,
                                 mode = 'markers+lines',
                                 line=dict(shape='linear'),
                                 connectgaps=True
                                 )
                     )
    fig.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-12
      • 2017-09-24
      • 2020-10-31
      • 1970-01-01
      • 1970-01-01
      • 2019-10-05
      • 1970-01-01
      • 2019-04-16
      相关资源
      最近更新 更多