我可以为Pandas DataFrame.plot指定子图的顺序吗?

0 投票
1 回答
903 浏览
提问于 2025-04-26 12:35

Pandas的DataFrame.plot功能很好用。但是我没有找到一种方法来给我的子图设置顺序,这样我就可以按照我想要的顺序来绘制列。比如说,我想从上到下绘制第4列、第2列、第1列和第3列。我该怎么做呢?

暂无标签

1 个回答

1

如果你想改变数据表中列的顺序,可以直接调整列的位置。在最后一个例子中,你可以看到列的位置是被移动过的:

In [89]:

df = pd.DataFrame({'a':randn(5), 'c':randn(5), 'b':randn(5)})
print(df)
df.plot()
          a         b         c
0 -0.902707  0.652272 -1.033054
1  0.482112  0.300324  1.440294
2 -1.091794  0.007883 -0.068403
3 -1.444140  0.137132  0.315419
4  0.634232  1.259193 -0.011486
Out[89]:

enter image description here

In [90]:

df = df.ix[:,['c','b','a']]
print(df)
df.plot()
          c         b         a
0 -1.033054  0.652272 -0.902707
1  1.440294  0.300324  0.482112
2 -0.068403  0.007883 -1.091794
3  0.315419  0.137132 -1.444140
4 -0.011486  1.259193  0.634232
Out[90]:

enter image description here

撰写回答