1

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 :)

9
  • Can you post the error and the traceback? Commented Mar 24, 2020 at 14:52
  • I am getting a KeyError for the X Co-ordinate in prices.plot Commented Mar 24, 2020 at 14:55
  • In that case, what does your DataFrame look like? What are the column names? Commented Mar 24, 2020 at 14:59
  • There are a total of 4 columns, the 2nd 3rd and 4th are ["CVX,"XOM","BP"] respectively. The 1st column is a datetime column, the name of which I am not sure about, so I tried to convert the DataFrame into a dictionary and then see the name...but I am not sure how to explain what I saw there, because I have never seen that kind of a dictionary before. Commented Mar 24, 2020 at 15:04
  • Try using print(df.columns) to see. Commented Mar 24, 2020 at 15:06

1 Answer 1

1

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')
Sign up to request clarification or add additional context in comments.

1 Comment

Got it! I am really grateful to you! Thank you :) :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.