1
from iexfinance import Stock
import matplotlib.pyplot as plt
tsla = Stock('TSLA')
tsla.get_close()
tsla.get_price()

from iexfinance import get_historical_data
from datetime import datetime
import pandas as pd
pd.set_option('display.max_rows', 1000)

start = datetime(2017, 2, 9)
end = datetime(2017, 5, 24)

df = get_historical_data("TSLA", start=start, end=end, output_format='json')
df.plot()
plt.show()


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call 
last)
<ipython-input-16-77b5455be846> in <module>()
      8 
      9 df = get_historical_data("TSLA", start=start, end=end, 
output_format='json')
---> 10 df.plot()
     11 plt.show()
     12 df

AttributeError: 'dict' object has no attribute 'plot'

I am trying to get the plot for the dataframe created with the different prices from the start and end date. I tried reading different blogs but none seem to solve it. I am sure I am making a very basic error. Although, I am not able to figure out what am I doing wrong here, any suggestions to fix it?

3
  • It seems that get_historical_data returns dict. Try to do: df = pd.DataFrame(get_historical_data("TSLA", start=start, end=end, output_format='json')). Commented Aug 13, 2018 at 6:15
  • that is giving me TypeError: Empty 'DataFrame': no numeric data to plot error Commented Aug 13, 2018 at 6:39
  • Please, add information about your df. What values it contains? Commented Aug 13, 2018 at 7:35

1 Answer 1

1

You cannot directly plot a dictionary in matplotlib. It needs x values and y values.

You can see that type(df) will be a <class 'dict'> which contains the value something like this:

{'TSLA': {'2017-02-09': {'open': 266.25, 'high': 271.18, 'low': 266.15, 'close': 269.2, 'volume': 7820222}}}

so, if you want to get it graphed, you need to convert it into pandas dataFrame

your code has to change like this:

from iexfinance import Stock
import matplotlib.pyplot as plt

tsla = Stock('TSLA')
tsla.get_close()
tsla.get_price()

from iexfinance import get_historical_data
from datetime import datetime
import pandas as pd

pd.set_option('display.max_rows', 1000)

start = datetime(2017, 2, 9)
end = datetime(2017, 5, 24)

df = get_historical_data("TSLA", start=start, end=end, output_format='json')
df = pd.DataFrame(df["TSLA"]).transpose() #transpose to get dates as x
df = df.drop(columns = "volume")  #deleted volume column, else it make other graphs smaller
df.index = pd.to_datetime(df.index) #change string to date format
df.plot()
plt.show();

You will get a graph like this:

Graph

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks it worked. I had to update pandas to get the columns command to work but it did it. :)

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.