【发布时间】:2016-03-15 14:30:17
【问题描述】:
我确信这已经在某个时候得到了回答,但我似乎无法找到正确的搜索词来找出解决方案。我正在尝试从分组数据框中绘制一系列线条,其中线条的颜色与数据框中的分组键对齐。我在那里 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
colors = {2013: 'r', 2014: 'b', 2015: 'g'}
fig, ax = plt.subplots()
labels = []
for key, grp in dqData[dqData['team'] == 'team1'].groupby(['year']):
ax = grp.plot(ax=ax, kind='line', x='x', y='y', figsize=(10,10), xlim=(0,30), sharex= True,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长什么样子? -
更新了组的实际快照。
-
想知道它是否有助于旋转/取消堆叠数据?我会试试看。如果可行的话,这样做似乎很痛苦……
标签: python pandas matplotlib plot