情节:如何在多条线上绘制分组结果?

时间:2019-08-14 21:46:27

标签: python pandas d3.js 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轴上应具有Date,Y轴上应具有ID计数,并应基于类别进行多行显示

1 个答案:

答案 0 :(得分:1)

据我所知,您将不得不使用pandas.DataFrame.pivot之类的方法来获取您在此处寻找的数据结构。这是一个建议,应该使用以下示例数据框来适合您的数据集描述:

数据:

         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

情节:

enter image description here

代码:

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