Search code examples
python-3.xplotyfinance

Plotting with yfinance package


In the following code, I want to plot the number of shares of company based on dates, e.g. YY-MM-DD. Using yfinance, the code looks like:

import yfinance as yf
import matplotlib.pyplot as plt

ticker_symbol = 'MSFT'
stock = yf.Ticker(ticker_symbol)
s = stock.get_shares_full()
s = stock.get_shares_full(start="2023-01-01")
print(s)
     2023-01-04 00:00:00-05:00    7461940224
     2023-01-06 00:00:00-05:00    7454470144
     ...
     dtype: int64
dates=s.index
shares=s.values
plt.plot(dates, shares)

But the error is

ValueError: Multi-dimensional indexing (e.g. `obj[:, None]`) is no longer supported. Convert to a numpy array before indexing instead.

How can I fix that with numpy?


Solution

  • Your code will run using matplotlib versions greater that or equal to 3.6.

    If however, this will fix the error: dates=s.index.to_numpy()

    Ref: pandas.Index.to_numpy