cheeseexpert
cheeseexpert

Reputation: 21

Stock Price Start and End range from specified time

I have this code that works when manually entering a start date and an end date:

import datetime as dt
from datetime import date
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import pandas_datareader as web

today = date.today()
style.use("ggplot")
start = dt.datetime(2020,1,1)
end = today
df = web.get_data_yahoo("TSLA", start, end)
df["Adj Close"].plot()
plt.xlabel("Date")
plt.ylabel("Price $")
plt.title("TSLA Stock Price ", )
plt.show()

It works when plotting the graph, but I want the start and end date to show up on the title as well. Is there anyway that I can import the "start" and "end" variables after "TSLA Stock Price "?

Upvotes: 0

Views: 375

Answers (1)

J.T. Baker
J.T. Baker

Reputation: 73

I think you can just use an f string in the plt.title().

plt.title(f"TSLA Stock Price {start.strftime('%x')} to {end.strftime('%x')}")

This code results in the title being formated like mm/dd/yy to mm/dd/yy

Upvotes: 1

Related Questions