Plot and Compare the Stock Price Trend

Introduction

Nowadays, whoever is dealing with finance data, specifically with stock market needs very good understanding of data. Obviously, understanding of data completely depends on how the data is displayed or shown to someone. When we are putting money in the stock market, it is always advisable to first understand the pattern or behavior of the stock which you are going to buy or sell. 

In this article, I'm neither going to cover the internals of finance nor am I going to discuss about what all parameters one needs to analyze or understand. The idea behind this article is, how one can use certain financial parameters to get the gist of market trend.

Getting Started

Let's consider a scenario, in which you would like to know, what is the opening price of Microsoft stock in the last few weeks. So that, by looking at the price, you can derive some conclusions. To perform this entire exercise, we need a few things:

  • Stock symbol: In our case, it is MSFT.
  • Programming language and library: In our case, it is Python, Yahoo Finance and Pandas.
  • Plotting library: In our case, it is MatplotLib.
  • IDE: In this case, I'm using Jupyter Notebook. You can use other IDE of your choice.

Using the Code

First of all, we need to import all the required packages:

import pandas
import yfinance
import matplotlib.pyplot as matplt
Python

Then we need to get data for MSFT stock for the desired duration. Here, I'm interested in data for the duration 2021-10-01 to 2022-02-14.

stock = ["MSFT"]
stock_data = yfinance.download(stock,start="2021-10-01", end="2022-02-14")
Python

To have a gist of the downloaded data, you can fetch initial few rows and see. To view initial five rows, you can do the following:

stock_data.head(5)
Python

Executing the above statement will provide you with the output as shown below:








Now we have the data with us, it is completely on us that what all columns we consider for analysis. For this article, let's go with the opening price of stock, which is represented by Open column.

Let's take values from Open column in a variable as shown:

data = stock_data.loc[:,"Open"].copy()
Python

The above line will create a copy of all the data available under Open column.

Next comes plotting the data of Open column. Using matplotlib, we can plot various types of charts and for this example, I'm taking seaborn. Once chart style is decided, we need to set the required height, width and font size as per our visualization needs. Here is the code for that:

data.plot(figsize=(20,7), fontsize = 16)
matplt.style.use("seaborn")
matplt.show()
Python

The above lines will give you the visualization as shown below:








By looking at such visualization, you can easily say, which day stock opened very high and which day it was low.

Complete Code

Here is the complete code as explained above:

import pandas
import yfinance
import matplotlib.pyplot as matplt
stock = ["MSFT"] stock_data = yfinance.download(stock,start="2021-10-01", end="2022-02-14")
data = stock_data.loc[:,"Open"].copy()
data.plot(figsize=(20,7), fontsize = 16)
matplt.style.use("seaborn")
matplt.show()
Python

Hope you enjoyed reading this article. This article plots only one stock, but you can extend it to go for more.

Comments

  1. This article offers a practical introduction to analyzing stock price trends with Python, particularly by combining Yahoo Finance, Pandas, Matplotlib, and Jupyter Notebook. I appreciate how the author moves from downloading historical MSFT data to selecting the Open column and visualizing it, showing how financial data can be transformed into an understandable chart for identifying market patterns.

    The step-by-step approach makes the workflow accessible, especially the use of Pandas for preparing the downloaded dataset and Matplotlib for visualization. Readers interested in strengthening their skills with financial datasets can explore a Matplotlib Course to better understand how different plotting techniques can communicate trends effectively.

    ReplyDelete
  2. The article also demonstrates how historical stock observations can support time-based analysis and provide an initial view of price behavior. Combining visualization with appropriate analytical methods is useful when exploring financial datasets, while a Machine Learning Time Series Course can help extend such analysis toward more advanced sequential-data applications.

    ReplyDelete
  3. Overall, this is a useful hands-on example of turning raw market data into a visual representation that is easier to interpret. The workflow provides a good starting point for applying data preparation, visualization, and modeling techniques to practical datasets, making Machine Learning Projects for Final Year relevant for exploring similar predictive and analytical problems.

    ReplyDelete

Post a Comment

Popular posts from this blog

Create Chatbot Based Using GPT-Index/LlamaIndex | OpenAI | Python

Create Candlestick Charts For Stocks Using Yahoo Finance

WPF: TemplateBinding with ControlTemplate