Search code examples
pythonpandastypeerrorscatter-plot

TypeError when plotting multiple scatter graphs?


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)

import datetime
import pandas_datareader as pdr 
import pandas as pd`

api_key='thisisasecret'
tickers = ['TSLA', 'ADDYY', 'UPS', 'PFE']
start="2020-1-1"
end="2021-7-1"

df = pdr.tiingo.TiingoDailyReader(tickers, start=start, end=end, api_key=api_key)
all_data = df.read()
all_data

import matplotlib.pyplot as plt

daily_close_px = all_data[['adjClose']].reset_index().pivot('date', 'symbol', 'adjClose')
daily_pct_change = daily_close_px.pct_change()
daily_pct_change.hist(bins=50, sharex=True, figsize=(12,8))

plt.show()

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?

all_data.plot(kind='scatter',x='date',y='adjClose')
plt.title('Stock Scatter Analysis')
plt.xlabel('date')
plt.ylabel('closing price')

plt.show()

I was expecting four similar graphs as with the histograms but I got the TypeError.

Why is that and how can I fix it?


Solution

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

    datelist = [pd.to_datetime(d) for d in all_data.date]
    all_data['date'] = datelist
    

    See if that fixes it. If not, please report back.