In this article we want to learn about How to Integrate Yahoo Finance API with Matplotlib, so If you are interested in financial data analysis, you might find yourself needing to use Yahoo Finance data. Yahoo Finance is free and widely used platform that provides financial information on different stocks, currencies, commodities and indices. in this article we want to learn how to integrate Yahoo Finance with Matplotlib to create graphs and charts based on financial data.
First of all we need to install required libraries.
1 |
pip install yfinance matplotlib |
Now we can access Yahoo Finance data using yfinance library. yfinance library is popular and easy Python library that allows you to download historical market data from Yahoo Finance.
1 2 3 4 5 6 7 |
import yfinance as yf # Get data for the stock Apple apple_data = yf.download("AAPL", start="2020-01-01", end="2022-04-01") # Print first five rows of the data print(apple_data.head()) |
In the above example we are downloading Apple’s stock data from January 1, 2020, to April 1, 2022. data will be stored in pandas dataframe.
This will be the result
Now that we have the data, we can use Matplotlib to create a chart of the stock closing price. we can do this by plotting the closing price against the date.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import yfinance as yf import matplotlib.pyplot as plt # Get data for stock Apple apple_data = yf.download("AAPL", start="2020-01-01", end="2022-04-01") import matplotlib.pyplot as plt # Create new figure and set the size plt.figure(figsize=(12, 6)) # Plot closing price against the date plt.plot(apple_data.index, apple_data['Close']) # Set title and axis labels plt.title('Apple Stock Price') plt.xlabel('Date') plt.ylabel('Closing Price ($)') # Show the chart plt.show() |
In this example we are have created line plot of Apple’s stock price against the date. we set the title and axis labels and then show the chart.
This will be the result
Learn More on Python
- Qt Designer Tool in PyQt6
- How to Use Stylesheets in Python PyQt6
- Event Handling in Python and PyQt6
- Responsive Applications with PyQt6 Multithreading
- How to Deploy PyQt6 Applications
- How to Build Web Applications with PyQt6
- How to Create ComboBox in PySide6
- How to Create CheckBox in Python PySide6
- Responsive Applications with PyQt6 Multithreading
- Event Handling in Python and PyQt6
- How to Use Stylesheets in Python PyQt6