gpt4 book ai didi

python - plotly python中的多个绘图错误

转载 作者:行者123 更新时间:2023-12-04 10:57:19 24 4
gpt4 key购买 nike

我有这个数据框

    Id  Timestamp               Data    Group
0 1 2013-08-12 10:29:19.673 40.0 1
1 2 2013-08-13 10:29:20.687 50.0 2
2 3 2013-09-14 10:29:20.687 40.0 3
3 4 2013-10-14 10:29:20.687 30.0 4
4 5 2013-11-15 10:29:20.687 50.0 5
...

我可以使用
import plotly.express as px
df1 = df[df['Group'] ==1]
fig = px.line(df1, 'Timestamp', 'Data',width=1000, height=500)
fig.show()

然后我想按 Group 对数据进行分组列并为每个唯一的 Group 绘制图表.我用了
import plotly.express as px
df1 = df.groupby(df['Group'])
fig = px.line(df1, 'Timestamp', 'Data',width=1000, height=500)
fig.show()

并产生错误
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-10-f8ccd9a83ce9> in <module>()
2 df1 = df.groupby(df['Group'])
3
----> 4 fig = px.line(df1, 'Timestamp', 'Data',width=1000, height=500)
5 fig.show()

4 frames
/usr/local/lib/python3.6/dist-packages/pandas/core/groupby/groupby.py in _make_wrapper(self, name)
602 "using the 'apply' method".format(kind, name, type(self).__name__)
603 )
--> 604 raise AttributeError(msg)
605
606 self._set_group_selection()

AttributeError: Cannot access attribute 'columns' of 'DataFrameGroupBy' objects, try using the 'apply' method

我引用了几个类似的 posts并尝试了一些东西,但没有奏效。我该怎么做?谢谢

最佳答案

你已经用 plotly 标记了这个问题,到目前为止只得到了一个 matplotlib 答案,所以这里有一个 plotly 方法:

在您提供的数据样本中,有 no duplicate values for 'Group' ,但 timestamp似乎是连续的。虽然您的问题很好,但是您的数据集并没有为您尝试做的事情奠定合理的基础,特别是如果您不想以任何方式聚合分组值。因此,我假设您真正在这里工作的实际上更像是:

    Id  Timestamp               Data    Group
0 1 2013-08-12 10:29:19.673 40.0 1
1 2 2013-08-13 10:29:20.687 50.0 1
2 3 2013-09-14 10:29:20.687 40.0 1
3 4 2013-10-14 10:29:20.687 30.0 1
4 5 2013-11-15 10:29:20.687 50.0 1
5 6 2013-08-12 10:29:19.673 60.0 2
6 7 2013-08-13 10:29:20.687 70.0 2
7 8 2013-09-14 10:29:20.687 60.0 2
8 9 2013-10-14 10:29:20.687 40.0 2
9 10 2013-11-15 10:29:20.687 60.0 2
10 11 2013-08-12 10:29:19.673 80.0 3
11 12 2013-08-13 10:29:20.687 100.0 3
12 13 2013-09-14 10:29:20.687 80.0 3
13 14 2013-10-14 10:29:20.687 60.0 3
14 15 2013-11-15 10:29:20.687 100.0 3

如果是这种情况,您可以选择在相同的时间戳上绘制每个组以获得:

使用枢轴值绘制每组一条轨迹:

enter image description here

或者您可以选择为每个组制作一个单独的图,如下所示:

使用分组数据绘图以获得每组一个数字:

enter image description here

完整的代码片段:

带有枢轴值以获得每组一条跟踪的代码:
import pandas as pd
import plotly.graph_objects as go

df= pd.DataFrame({'Id': {(0, 1): '2013-08-12',
(1, 2): '2013-08-13',
(2, 3): '2013-09-14',
(3, 4): '2013-10-14',
(4, 5): '2013-11-15',
(5, 6): '2013-08-12',
(6, 7): '2013-08-13',
(7, 8): '2013-09-14',
(8, 9): '2013-10-14',
(9, 10): '2013-11-15',
(10, 11): '2013-08-12',
(11, 12): '2013-08-13',
(12, 13): '2013-09-14',
(13, 14): '2013-10-14',
(14, 15): '2013-11-15'},
'Timestamp': {(0, 1): '10:29:19.673',
(1, 2): '10:29:20.687',
(2, 3): '10:29:20.687',
(3, 4): '10:29:20.687',
(4, 5): '10:29:20.687',
(5, 6): '10:29:19.673',
(6, 7): '10:29:20.687',
(7, 8): '10:29:20.687',
(8, 9): '10:29:20.687',
(9, 10): '10:29:20.687',
(10, 11): '10:29:19.673',
(11, 12): '10:29:20.687',
(12, 13): '10:29:20.687',
(13, 14): '10:29:20.687',
(14, 15): '10:29:20.687'},
'Data': {(0, 1): 40.0,
(1, 2): 50.0,
(2, 3): 40.0,
(3, 4): 30.0,
(4, 5): 50.0,
(5, 6): 60.0,
(6, 7): 70.0,
(7, 8): 60.0,
(8, 9): 40.0,
(9, 10): 60.0,
(10, 11): 80.0,
(11, 12): 100.0,
(12, 13): 80.0,
(13, 14): 60.0,
(14, 15): 100.0},
'Group': {(0, 1): 1,
(1, 2): 1,
(2, 3): 1,
(3, 4): 1,
(4, 5): 1,
(5, 6): 2,
(6, 7): 2,
(7, 8): 2,
(8, 9): 2,
(9, 10): 2,
(10, 11): 3,
(11, 12): 3,
(12, 13): 3,
(13, 14): 3,
(14, 15): 3}})

# pivot values to get one trace per group
dfp = pd.pivot_table(df,
values='Data',
index=['Timestamp'],
columns=['Group'],
)
dfp.tail()

# plotly
fig = go.Figure()
for col in dfp.columns:
fig.add_trace(go.Scatter(x=dfp.index, y=dfp[col], name='Group_'+str(col)))

fig.show()

带有分组数据的代码以获得每组一个数字:
# imports
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots

# data
df= pd.DataFrame({'Id': {(0, 1): '2013-08-12',
(1, 2): '2013-08-13',
(2, 3): '2013-09-14',
(3, 4): '2013-10-14',
(4, 5): '2013-11-15',
(5, 6): '2013-08-12',
(6, 7): '2013-08-13',
(7, 8): '2013-09-14',
(8, 9): '2013-10-14',
(9, 10): '2013-11-15',
(10, 11): '2013-08-12',
(11, 12): '2013-08-13',
(12, 13): '2013-09-14',
(13, 14): '2013-10-14',
(14, 15): '2013-11-15'},
'Timestamp': {(0, 1): '10:29:19.673',
(1, 2): '10:29:20.687',
(2, 3): '10:29:20.687',
(3, 4): '10:29:20.687',
(4, 5): '10:29:20.687',
(5, 6): '10:29:19.673',
(6, 7): '10:29:20.687',
(7, 8): '10:29:20.687',
(8, 9): '10:29:20.687',
(9, 10): '10:29:20.687',
(10, 11): '10:29:19.673',
(11, 12): '10:29:20.687',
(12, 13): '10:29:20.687',
(13, 14): '10:29:20.687',
(14, 15): '10:29:20.687'},
'Data': {(0, 1): 40.0,
(1, 2): 50.0,
(2, 3): 40.0,
(3, 4): 30.0,
(4, 5): 50.0,
(5, 6): 60.0,
(6, 7): 70.0,
(7, 8): 60.0,
(8, 9): 40.0,
(9, 10): 60.0,
(10, 11): 80.0,
(11, 12): 100.0,
(12, 13): 80.0,
(13, 14): 60.0,
(14, 15): 100.0},
'Group': {(0, 1): 1,
(1, 2): 1,
(2, 3): 1,
(3, 4): 1,
(4, 5): 1,
(5, 6): 2,
(6, 7): 2,
(7, 8): 2,
(8, 9): 2,
(9, 10): 2,
(10, 11): 3,
(11, 12): 3,
(12, 13): 3,
(13, 14): 3,
(14, 15): 3}})

dfp = pd.pivot_table(df,
values='Data',
index=['Timestamp'],
columns=['Group'],
)

# data dimensions
nrows = len(dfp.columns)

fig = make_subplots(rows=nrows,
cols=1,
subplot_titles=['Group'+str(c) for c in dfp.columns])

# add traces
x = 1
for i, col in enumerate(dfp.columns):
fig.add_trace(go.Scatter(x=dfp.index, y=dfp[col].values,
name = 'Group_'+str(col),
mode = 'lines',
),
row=i+1,
col=1)

fig.update_layout(height=nrows*200)

fig.show()

关于python - plotly python中的多个绘图错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59096795/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com