Issue
I made two curves. Both depend on dates and the second (extend) should occur in the date range after the first one, but it simply gets the following result:
Here is my code:
import pandas_datareader as web
import pandas as pd
import matplotlib.pyplot as plt
stock = web.DataReader('MMM', data_source="yahoo", start='2021-01-01', end='2021-11-12')
stock = stock.filter(["Adj Close"])
extend = pd.DataFrame({'Date': ["2021-11-15", "2021-11-16"], 'Adj Close': [170, 171]})
extend.set_index('Date')
plt.figure(figsize=(16, 8))
plt.plot(stock["Adj Close"])
plt.plot(extend[["Adj Close"]])
plt.show()
I think the problem has something to do with the index column, but I don't found a way to delete it.
Solution
I believe it would help to pass the values for the plots because the main issue is that web.DataReader returns the index as a DateTime
range whereas the data you add if of type object
. I believe this incompatibility is causing the issue for you. Kindly try:
plt.plot(x=stock.index,y=stock['Adj Close'])
plt.plot(x=pd.to_datetime(extend['Date'],infer_datetime_format=True),y=extend['Adj Close'])
plt.show()
Alternatively (and I think this might work better) I suggest you append the extend
dataframe to the original one. I am not being to use web.DataReader
but it should work the same as manually creating the dataset:
df = pd.DataFrame(np.random.randint(100,200,10).tolist(),index =pd.date_range(start='2021-10-01',periods=10,freq='d')).rename(columns={0:'Adj Close'}) ##This should be the result of the DataReader
df = df.append(pd.DataFrame([180,174],index=pd.date_range(start='2021-11-15',periods=2)).rename(columns={0:'Adj Close'})) ##Data being added manually
Plotting this:
plt.plot(df['Adj Close'])
plt.show()
Answered By - Celius Stingher
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.