Jaideep Dadi
Jaideep Dadi

Reputation: 13

Plotting dataframes using matplotlib in Python IDE

I am trying to plot a dataframe which has been taken from get_data_yahoo attribute in pandas_datareader.data on python IDE using matplotlib.pyplot and I am getting an KeyError for the X-Co-ordinate in prices.plot no matter what I try. Please help!

I have tried this out :-

import matplotlib.pyplot as plt

from pandas import Series,DataFrame

import pandas_datareader.data as pdweb

import datetime

prices=pdweb.get_data_yahoo(['CVX','XOM','BP'],start=datetime.datetime(2020,2,24),
                            end=datetime.datetime(2020,3,20))['Adj Close']

prices.plot(x="Date",y=["CVX","XOM","BP"])
plt.imshow()
plt.show()

And I have tried this as well:-

prices=DataFrame(prices.to_dict())
prices.plot(x="Timestamp",y=["CVX","XOM","BP"])
plt.imshow()
plt.show()

Please Help...!!

P.S: I am also getting some kind of warning, please explain about it if you could :)

Upvotes: 1

Views: 118

Answers (1)

Alan
Alan

Reputation: 2498

The issue is that the Date column isn't an actual column when you import the data. It's an index. So just use:

prices = prices.reset_index()

Before plotting. This will convert the index into a column, and generate a new, integer-labelled index.

Also, in regards to the warnings, Pandas is full of them and they are super annoying! You can turn them off with the standard python library warnings.

import warnings
warnings.filterwarnings('ignore')

Upvotes: 1

Related Questions