-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot graph.py
More file actions
36 lines (31 loc) · 1.18 KB
/
plot graph.py
File metadata and controls
36 lines (31 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import yfinance as yf
import matplotlib.pyplot as plt
def make_graph(data, title):
"""
Function to plot Tesla stock data.
:param data: DataFrame containing Tesla stock data
:param title: Title for the graph
"""
plt.figure(figsize=(12, 6))
plt.plot(data['Date'], data['Close'], label='Closing Price', color='blue')
plt.title(title, fontsize=16)
plt.xlabel("Date", fontsize=12)
plt.ylabel("Stock Price (USD)", fontsize=12)
plt.grid(alpha=0.5)
plt.legend()
plt.show()
def fetch_and_plot_tesla_stock():
# Fetch Tesla stock data
tesla_data = yf.Ticker("TSLA")
tesla_history = tesla_data.history(period="max")
# Reset index to ensure 'Date' is a column
tesla_history.reset_index(inplace=True)
# Ensure data includes 'Date' and 'Close' columns
if 'Date' in tesla_history.columns and 'Close' in tesla_history.columns:
# Call make_graph to plot the data
make_graph(tesla_history, "Tesla Stock Price Over Time")
else:
print("Data does not contain the required columns to plot.")
# Run the function
if __name__ == "__main__":
fetch_and_plot_tesla_stock()