如何绘制特定索引范围的两列?

时间:2019-04-04 00:47:24

标签: pandas dataframe plot indexing

我从具有2列的.txt文件中制作了一个数据框。我有一个特定的索引范围(3751:6252),我想为此绘制第1列(频率)与第2列(相位)的关系。

我该怎么做?

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns 

#open text file and save it as a df (dataframe)
textfile = pd.read_csv("RawPhaseData.txt", sep='\s+', skiprows= 2, header=None, names = ['freq', 'phase'])
#make the index equal to frequency
#textfile.set_index("freq", inplace=True)

p = textfile[3751:6252]
x = p['freq']
y = p['phase']
plt.plot(x,y)
plt.show()

期望图形,但是没有输出。

我说出print(p)的部分结果

                  freq             phase
3751  0.55000000000000  -51.101839657065
3752  0.55004000000000  -51.251119837268
3753  0.55008000000000  -51.400516517531
3754  0.55012000000000  -51.550029980720
3755  0.55016000000000  -51.699660509792
3756  0.55020000000000  -51.849408387787

....

我说出print(x)的部分结果

3751    0.55000000000000
3752    0.55004000000000
3753    0.55008000000000
3754    0.55012000000000
3755    0.55016000000000
3756    0.55020000000000
3757    0.55024000000000
3758    0.55028000000000
3759    0.55032000000000

....

我说print(y)时的部分结果

3751    -51.101839657065
3752    -51.251119837268
3753    -51.400516517531
3754    -51.550029980720
3755    -51.699660509792
3756    -51.849408387787
3757    -51.999273897829

....

1 个答案:

答案 0 :(得分:1)

也许您需要在导入后内联添加%matplotlib。

import matplotlib.pyplot as plt
%matplotlib inline