I am having an issue when trying to plot scatter graphs. I am using Tiingo Api to get stock data and have plotted 4 separate histograms for my data with no issue (code below)
JavaScript
x
21
21
1
import datetime
2
import pandas_datareader as pdr
3
import pandas as pd`
4
5
api_key='thisisasecret'
6
tickers = ['TSLA', 'ADDYY', 'UPS', 'PFE']
7
start="2020-1-1"
8
end="2021-7-1"
9
10
df = pdr.tiingo.TiingoDailyReader(tickers, start=start, end=end, api_key=api_key)
11
all_data = df.read()
12
all_data
13
14
import matplotlib.pyplot as plt
15
16
daily_close_px = all_data[['adjClose']].reset_index().pivot('date', 'symbol', 'adjClose')
17
daily_pct_change = daily_close_px.pct_change()
18
daily_pct_change.hist(bins=50, sharex=True, figsize=(12,8))
19
20
plt.show()
21
However when I run this code to try to do something similar with a scatter graph I get a TypeError: no numerical data to plot, although I have defined the data to plot?
JavaScript
1
7
1
all_data.plot(kind='scatter',x='date',y='adjClose')
2
plt.title('Stock Scatter Analysis')
3
plt.xlabel('date')
4
plt.ylabel('closing price')
5
6
plt.show()
7
I was expecting four similar graphs as with the histograms but I got the TypeError.
Why is that and how can I fix it?
Advertisement
Answer
I can’t replicate your pdf.tiingo… line so I can’t be absolutely certain, but I suspect your dates are not actually datetimes.
Try something like:
JavaScript
1
3
1
datelist = [pd.to_datetime(d) for d in all_data.date]
2
all_data['date'] = datelist
3
See if that fixes it. If not, please report back.