Python matplotlib |分组数据框打印问题

Python matplotlib |分组数据框打印问题,python,pandas,matplotlib,plot,Python,Pandas,Matplotlib,Plot,我相信这个问题在某些时候已经得到了回答,但我似乎无法找到正确的搜索词来找到解决方案。我试图从分组数据框中绘制一系列线,其中线的颜色与数据框上的分组键对齐。我是99%,但现在我得到了一个奇数输出,在这个输出中,图中线条的最终数据点被重新连接到原点。我假设这与x轴具有不同的“长度”有关,这取决于正在绘制的序列  编辑:以下是分组数据框中x和y的外观示例: (2013, team year month x y z Index

我相信这个问题在某些时候已经得到了回答,但我似乎无法找到正确的搜索词来找到解决方案。我试图从分组数据框中绘制一系列线,其中线的颜色与数据框上的分组键对齐。我是99%,但现在我得到了一个奇数输出,在这个输出中,图中线条的最终数据点被重新连接到原点。我假设这与x轴具有不同的“长度”有关,这取决于正在绘制的序列



编辑:以下是分组数据框中x和y的外观示例:

(2013,         team  year  month   x         y   z
Index                                      
665    team2  2013      1   6  0.003268   1
666    team2  2013      1   7  0.003390   1
667    team2  2013      1   8  0.006969   2
668    team2  2013      1   9  0.003571   1
669    team2  2013      1  10  0.011152   3
670    team2  2013      1  11  0.007634   2
671    team2  2013      1  12  0.028226   7
672    team2  2013      1  13  0.016949   4
673    team2  2013      1  14  0.022026   5
674    team2  2013      1  15  0.013761   3
675    team2  2013      1  16  0.023810   5
676    team2  2013      1  18  0.010204   2
677    team2  2013      1  19  0.021858   4
678    team2  2013      1  20  0.034091   6
679    team2  2013      1  21  0.046784   8
680    team2  2013      1  22  0.037975   6
681    team2  2013      1  23  0.020548   3
682    team2  2013      1  24  0.021277   3
683    team2  2013      1  25  0.021277   3
684    team2  2013      1  26  0.007407   1
685    team2  2013      1  27  0.015267   2
686    team2  2013      1  29  0.008130   1
687    team2  2013      1  30  0.016807   2
688    team2  2013      1  31  0.034783   4
689    team2  2013      1  33  0.028302   3
690    team2  2013      1  34  0.019048   2
691    team2  2013      1  35  0.038095   4
692    team2  2013      2   4  0.005405   1
693    team2  2013      2   6  0.016667   3
694    team2  2013      2   7  0.005848   1
     ...   ...    ...  ..       ...  ..
953    team2  2013     11  18  0.045767  20
954    team2  2013     11  19  0.057279  24
955    team2  2013     11  20  0.042079  17
956    team2  2013     11  21  0.027919  11
957    team2  2013     11  22  0.025907  10
958    team2  2013     11  23  0.029650  11
959    team2  2013     11  24  0.032787  12
960    team2  2013     11  25  0.030220  11
961    team2  2013     12   3  0.002621   2
962    team2  2013     12   4  0.006640   5
963    team2  2013     12   5  0.010782   8
964    team2  2013     12   6  0.009602   7
965    team2  2013     12   7  0.008368   6
966    team2  2013     12   8  0.018466  13
967    team2  2013     12   9  0.013043   9
968    team2  2013     12  10  0.019345  13
969    team2  2013     12  11  0.015291  10
970    team2  2013     12  12  0.023364  15


解决方案如下。必须以数据为中心并创建NAN

colors = {2013: 'r', 2014: 'b', 2015: 'g'}

fig, ax = plt.subplots(figsize=(10,10))
labels = []

for key, grp in dqData[dqData['team'] == 'team1'].groupby(['year']):
    g2 = grp.pivot(index='x', columns='month', values='y')
    ax = g2.plot(ax=ax, kind='line', c = colors[key])
    labels.append(key)

lines, _ = ax.get_legend_handles_labels()
ax.legend(lines, labels, loc='best')
plt.show()

您的数据帧是什么样子的?使用
kind='line'
将连续的点与一条线连接起来,因此我的第一个猜测是,每个点的末尾都有尾随的(0,0)点。添加了一个框架外观示例。没有尾随的零,但我假设它们是如何在绘图中创建的,因为x的长度不同?
grp
看起来像什么?为组的实际快照更新。不知道这是否有助于数据透视/取消堆叠?我试试看。如果它有效的话,这么做似乎很痛苦。。。
colors = {2013: 'r', 2014: 'b', 2015: 'g'}

fig, ax = plt.subplots(figsize=(10,10))
labels = []

for key, grp in dqData[dqData['team'] == 'team1'].groupby(['year']):
    g2 = grp.pivot(index='x', columns='month', values='y')
    ax = g2.plot(ax=ax, kind='line', c = colors[key])
    labels.append(key)

lines, _ = ax.get_legend_handles_labels()
ax.legend(lines, labels, loc='best')
plt.show()