1

I have 2 dataframes:

'stock' is a dataframe with columns Date and Price. 'events' is a dataframe with columns Date and Text.

My goal is to produce a graph of the stock prices and on the line place dots where the events occur. However, I do not know how to do 'y' value for the events dataframe as I want it to be where it is on the stock dataframe.

I am able to plot the first dataframe fine with:

plt.plot('Date', 'Price', data=stock)

And I try to plot the event dots with:

plt.scatter('created_at', ???, data=events)

However, it is the ??? that I don't know how to set

1 Answer 1

1

Assuming Date and created_at are datetime:

stock = pd.DataFrame({'Date':['2021-01-01','2021-02-01','2021-03-01','2021-04-01','2021-05-01'],'Price':[1,5,3,4,10]})
events = pd.DataFrame({'created_at':['2021-02-01','2021-03-01'],'description':['a','b']})

stock.Date = pd.to_datetime(stock.Date)
events.created_at = pd.to_datetime(events.created_at)

Filter stock by events.created_at (or merge) and plot them onto the same ax :

stock_events = stock[stock.Date.isin(events.created_at)]

# or merge on the date columns
# stock_events = stock.merge(events, left_on='Date', right_on='created_at')

ax = stock.plot(x='Date', y='Price')
stock_events.plot.scatter(ax=ax, x='Date', y='Price', label='Event', c='r', s=50)

stock events

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

Comments

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.