【问题标题】:Pandas plot without specifying index没有指定索引的熊猫图
【发布时间】:2017-02-17 15:47:03
【问题描述】:

给定数据:

Column1; Column2; Column3
1; 4; 6
2; 2; 6
3; 3; 8
4; 1; 1
5; 4; 2

我可以通过以下方式绘制它:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('test0.csv',delimiter='; ', engine='python')
titles = list(df)
for title in titles:
    if title == titles[0]:
        continue
    df.plot(titles[0],title, linestyle='--', marker='o')
    plt.savefig(title+'.png')

但如果相反,数据丢失了Column1,例如:

Column2; Column3
4; 6
2; 6
3; 8
1; 1
4; 2

如何绘制它?

可能是df.plot(title, linestyle='--', marker='o')

【问题讨论】:

  • 有很多方法来绘制它!我需要更好地了解您要绘制的内容。
  • 我想绘制Column2 X index,但缺少索引。
  • @KcFnMi,IIUC,您可以执行reset_indexDF 的索引设置为默认整数索引,然后将Column2 作为绘图的y arg 传递为:@987654330 @

标签: python python-2.7 pandas matplotlib dataframe


【解决方案1】:

我不确定您要达到什么目的,但您可以重置索引并根据需要进行设置:

In[11]: df
Out[11]: 
   Column1   Column2   Column3
0        1         4         6
1        2         2         6
2        3         3         8
3        4         1         1
4        5         4         2

因此,如果您想将 col 2 绘制为 X 轴,将 3 绘制为 Y 轴,您可以执行以下操作:

df.set_index('Column2')['Column3'].plot()

【讨论】:

  • 我会接受一个显示如何添加和索引列的答案。
猜你喜欢
  • 2015-04-19
  • 2021-02-26
  • 2023-01-26
  • 2019-04-27
  • 2014-04-01
  • 2020-06-15
相关资源
最近更新 更多