跨多个组/列绘图

时间:2019-01-16 15:39:20

标签: python python-3.x pandas plotly

我正在提取下面形式的数据集。我有两个变量(sampleidNum),想在两个组中进行绘制。我已附加了我希望输出看起来像在JMP中生成的内容。我已经尝试过以透视图的方式进行透视图和子图,但是看来您无法将groupby传递到子图中。

任何建议都会很棒。

# -*- coding: utf-8 -*-

from plotly import tools
from plotly.offline import  plot
import plotly.graph_objs as go
import pandas as pd
import numpy as np

d  = {'day':['Mon','Mon','Mon','Mon','Tues','Tues','Tues','Tues'],
      'sample':['A','A','B','B','A','A','B','B'],
      'idNum':[1,2,1,2,1,2,1,2],
      'values':[1.3,1.4,1.25,1,1.87,1.11,1.9,0.8]}

df = pd.DataFrame(data=d)

pdata = [dict(type = 'scatter',
              x = df.day,
              y = df.values,
              mode = 'markers',
              transforms = [dict(type = 'groupby',
                                 groups =df.idNum)])]

plot({'data': pdata}, validate=False)

Desired Output

1 个答案:

答案 0 :(得分:0)

这就像没有groupby的解决方案。但是您可能会从中找到有用的东西。 代码:

# import all the necessaries libraries
from plotly import tools
import plotly.offline as py
import plotly.graph_objs as go
import pandas as pd
# your data
d  = {'day':['Mon','Mon','Mon','Mon','Tues','Tues','Tues','Tues'],
      'sample':['A','A','B','B','A','A','B','B'],
      'idNum':[1,2,1,2,1,2,1,2],
      'values':[1.3,1.4,1.25,1.0,1.87,1.11,1.9,0.8]}
# create a df
df = pd.DataFrame(data=d)
# create traces
trace1 = go.Scatter(
    x=df.loc[(df['sample']=='A') & (df['idNum']==1)]['day'],
    y=df.loc[(df['sample']=='A') & (df['idNum']==1)]['values'],
    line = dict(color=('blue')),
    legendgroup='first',
    name='1'
)
trace2 = go.Scatter(
    x=df.loc[(df['sample']=='A') & (df['idNum']==2)]['day'],
    y=df.loc[(df['sample']=='A') & (df['idNum']==2)]['values'],
    line = dict(color=('red')),
    legendgroup='second',
    name='B1',
    showlegend=False
)
trace3 = go.Scatter(
    x=df.loc[(df['sample']=='B') & (df['idNum']==1)]['day'],
    y=df.loc[(df['sample']=='B') & (df['idNum']==1)]['values'],
    line = dict(color=('blue')),
    legendgroup='first',
    name='A2',
    showlegend=False
)
trace4 = go.Scatter(
    x=df.loc[(df['sample']=='B') & (df['idNum']==2)]['day'],
    y=df.loc[(df['sample']=='B') & (df['idNum']==2)]['values'],
    line = dict(color=('red')),
    legendgroup='second',
    name='2'
)
# told plotly, how much subplots we want
fig = tools.make_subplots(rows=2, cols=1)
# add to each subplot trace
fig.append_trace(trace1, 1, 1)
fig.append_trace(trace2, 1, 1)
fig.append_trace(trace3, 2, 1)
fig.append_trace(trace4, 2, 1)
# customize layout
fig['layout']['yaxis1'].update(title='values',
   tickvals=[0.6,0.8,1.0,1.2,1.4,1.6,1.8,2.0], range=[0.6,2.0],
   showgrid=False,showline=True,zeroline=True)
fig['layout']['xaxis1'].update(title='days',
   showgrid=False,showline=True,zeroline=True)
fig['layout']['yaxis2'].update(title='values',
   tickvals=[0.6,0.8,1.0,1.2,1.4,1.6,1.8,2.0], range=[0.6,2.0],
   showgrid=False,showline=True,zeroline=True)
fig['layout']['xaxis2'].update(title='days',
   showgrid=False,showline=True,zeroline=True)
# set height and width for a plot
fig['layout'].update(height=600, width=400, title='<b>values vs. day</b>')
# plot a plot
py.plot(fig, filename='stacked-subplots')

输出:

Something like what you want

我不能仅创建两条迹线,因为在每个图中都有相似的数据。有关如何在情节中创建子图的更多信息,请查看here。希望对您有帮助