0

I'm trying to plot the Treasury Yield curves. I've got the data in the following pandas data frame: enter image description here

So, basically I want to plot the 3 columns of the data frame against index and want to have 3 lines. But when I try to do so, instead of the lines I get 2D shapes which overlap with each other, like this:

enter image description here

I tried using both df.plot() from Pandas and usual plt.plot() commands, but they give the same result. How can I make 1D lines from these 2D shapes?

Zephyr
  • 11,891
  • 53
  • 45
  • 80
John P.
  • 139
  • 5

1 Answers1

2

In the plot you show, those are actually lines, but they are so close to each other that it seems there is a colored area.

Fake dataframe creation:

df = pd.DataFrame({'date': pd.date_range(start = '1990-01-02', end = '2020-12-31', freq = 'D')})
df['DGS1'] = 10*np.random.random(len(df))
df['DGS10'] = 10*np.random.random(len(df))
df['DGS5'] = 10*np.random.random(len(df))
df = df.set_index('date')

Plotting:

df.plot()

plt.show()

enter image description here

Zoom in a casual region:

plt.xlim('2003-03-07', '2003-08-04')

enter image description here

Zoom again:

plt.xlim('2003-04-07', '2003-04-22')

enter image description here


You could keep your dataframe as is, if you are ok with zooming in some regions of interest, otherwise you can apply a re-sampling of your data with pandas.DataFrame.resample. Pay attention that this operation will change your original data, so you have to be carefull about interpreting the plot you will get.
A working example could be to resample the dataframe with a frequency of 1 month and calculate the mean over each month:

df = df.resample('1M').mean()

enter image description here

Zephyr
  • 11,891
  • 53
  • 45
  • 80