#3 Make graphs of stock price in Python (Python Financial Analysis)

wsh
Python Financial Analysis
5 min readJul 31, 2021

#3 Make Graphs

you can watch it in a video

Python Financial Analysis | Home

Last Story

https://sparkle-mdm.medium.com/python-financial-analysis-2-38e35e31f6e6

Introduction

When doing financial analysis, one of the most important skills we can’t go without is making graphs. We usually want to see the performance of a company with graphs, or want to see if our analysis is successful visually.

Because this series is about financial analysis, I’m not going to write about everything of Python and Matplotlib. This is for beginners. Therefore we focus on making graphs of time series data, because the concept of this series is “take the shortest path”!

Thus, today’s topic is Matplotlib. I’ll make it easy as long as possible!

You can download the data set from this link
https://drive.google.com/drive/folders/1Ux2u1s5mctYiywS08sv7_3_PbnWd8v0G?usp=sharing

The full source code is available from here
https://drive.google.com/drive/folders/1F5n6a6hY9g_GLhPV80DCrAypa2onj0c1?usp=sharing

List of articles

1. Python Financial Analysis

1 Read fundamental data from a CSV in Python
2 Handling table like data in Python with DataFrame
3 Make graphs of stock price in Python
4.1 Make custom market index — prerequisites
4.2 Make custom market index — make your own index
4.3 Make custom market index — market cap based index
5.1 Analyze COVID-19 Impacts by Sector in Python — compare weighted average prices
5.2 Analyze COVID-19 Impacts by Market Caps in Python — compare weighted average prices
5.3 Find companies that lost or gained from the COVID19 pandemic

2. Python Data Analysis Basics (easiest ways)

Python “datetime” in the easiest way (how to handle dates in data science with Python)
Python DataFrame slicing in the easiest way (How to find a company from 5000 companies)

Make graphs

0. Import packages

As like the previous stories, we use the three packages NumPy, Pandas, and Matplotlib. But we have a new one “datetime”. This package handles dates and times in Python.

1. Read price data

If you haven’t downloaded the CSV file “pricedata_reshaped.csv”, you can download it from the link above. We need the file in this story.

As we saw in the first and second story, we read the CSV file with the “read_csv()” function of Pandas. The returned object is a one called “DataFrame”. You can thing of it like a table. It has columns (tickers of US listed companies) and rows (price data and dates). It would take few seconds to read the file because the CSV file has thousands of columns and thousands of rows (36.3MB).

Then we get Apple’s price data and Nvidia’s price for displaying it in the example later. The returned object is also DataFrame. If you don’t know so much about Panda’s DataFrame, this story may help you:
#2 Handling table like data in Python with DataFrame (Python Financial Analysis)

2. Read date (convert them to datetime objects)

The price data is for the y-axis. Then we need the data for the x-axis. The dates is stored as a column “date” in the DataFrame “price”. But we can’t use it directly as the x-axis, because the dates are stored as strings. For example, the first entry of “date” is a string “2015–10–01”.

The standard format to handle date and time, in Python, is “datetime”. Because Matplotlib accepts just “datetime” objects, we have to convert the “string” objects to “datetime” objects.

I wrote a code to convert the dates one by one with a “for” loop. We firs convert DataFrame “price[“date”]” to a Python list (a Python list looks like [“2015–10–01”, “2015–10–02”, …]). Then we convert each string with the “strptime()” function of the “datetime” package. You can append a new element to a list with the “+= […]” operator.

And I have to note here that you don’t need to remember it nor need to understand it completely! When you want to write this kind of code in the future, you can come back to this story. I never delete this.

Details about datetime is explained here:
Python “datetime” in the easiest way (how to handle dates in data science with Python)

3. Make line chart

We’ve already passed the hardest point. Making figure is quite simple. We first make a canvas of graph. This canvas is called a “figure” object, which has a name “fig” in the code below. We can make a figure with the “fig()” function of Matplotlib (= plt). If you want to change the size of figure, you can give preferences to “fig()” as arguments. But it’s out of our scope.

Then we make a line chart with the “plot()” function. The fist argument is the values for the x-axis, which is “date”. And the second is the y-axis, which is prices “price_apple”. If you want to change the styles of the graph, you can specify them as arguments.

And finally, we save the figure as an image “price_apple.png” with the function “savefig()”. You can choose any extension.

Full Python code

Conclusion

There are several types of graph, like, bar chart. In the future, we are going to encounter them. But I’ll show you how to use them at that time. So, don’t warry! This story is just an introduction.

This series is for anyone who don’t have background of programming and are interested in financial analysis. Learning programing is hard for beginner and takes them a lot time, so it’s good idea to take “the shortest path”! This means we don’t need to learn programming thar are not related to financial analysis. This series are made in this concept.

Other Links

Python Financial Analysis | Home
Python Data Analysis | Home

--

--