How to Visualise Multiple Stocks with Matplotlib
Tags: python, matplotlib, finance, pandas
Published: 2020-01-31 19:07:41


In this quick tutorial, we are going to use python to get data about a collection of stocks, and then plot then on a single graph. 


The very first thing we need to is find the tickers for the stocks that we want to plot. 


So go to Yahoo Finance and search for the companies you are interested in. 


When you get to the company's listing you will see the ticker in parentheses next to the company's name. 


![An example of United Utilities from the Yahoo Finance page.](https://thepracticaldev.s3.amazonaws.com/i/0n2wpd168ppa2dvgfhmp.png)

An example of United Utilities from the Yahoo Finance page.

Once you have the list of companies you want to plot. It's time to start coding. 


First we need a few imports:

```
import numpy as np
import pandas as pd
from pandas_datareader import data as wb
import matplotlib.pyplot as plt
```

If you don't have any of these installed. Go to your terminal and run:

```
pip install pandas
pip install numpy
pip install pandas-datareader
pip install matplotlib
```

Now let's create an array of stock objects:


```
stocks = [
    {
        'ticker': 'UU.L',
        'name': 'United Utilities'
    },
    {
        'ticker': 'VOD.L',
        'name': 'Vodafone Group'
    },
    {
        'ticker': 'BP.L',
        'name': 'BP Group'
    }
]
```

Next let's create a method to take this data and plot the data. 


```
def create_plot(stocks):
  	# Create an empty dataframe
    data = pd.DataFrame()
    for stock in stocks: 
      	# Create a column for the adjusted close of each stock
        # Here we use the DataReader library to get the data.
        data[stock['ticker']] = wb.DataReader(stock['ticker'], data_source='yahoo', start='2007-1-1')['Adj Close']
```

Here's what the contents of the data frame currently looks like:


![](https://thepracticaldev.s3.amazonaws.com/i/8vw558nhnvi94fyj4wt3.png)

You can see that adjusted close prices for each stock at present their own columns. 


The problem with the current state of the data is that we can't easily plot this data as if we did, all the of the stocks would displayed on different scales. 


So what we need to do is change this data to be the percentage return between each date and the first date in the series. 


To do this we can use a lovely bit of pythonic code:


```
# Calculate the returns for all the days
returns = data.apply(lambda x: (x / x[0] * 100))
```

Here we are using a lambda expression to turn create a new data frame which contains the percent returns of each of the days for each of the stocks.


That's all we have to do to get and adjust the data. 


Now all we have do is plot the data. 


```
plt.figure(figsize=(10,6))
  
# Plot the returns
for stock in stocks: 
plt.plot(returns[stock['ticker']], label=stock['name'])

# We need to call .legend() to show the legend.
plt.legend()
# Give the axes labels
plt.ylabel('Cumulative Returns %')
plt.xlabel('Time')
plt.show()
```


And we're done. 


You can find the full code here: [Full Code](https://codeyogi.co.uk/2019/12/30/visualising-multiple-stocks-with-matplotlib/)

I've also made a video tutorial to go alongside this tutorial.

[![Python For Finance | Visualising Multiple Stocks with Matplotlib](http://img.youtube.com/vi/vKWf5skmGAI/0.jpg)](http://www.youtube.com/watch?v=vKWf5skmGAI)

If you have any questions or have any feedback (definitely welcome!), then please leave a comment below, or you can find me at:

* [YouTube](http://youtube.com/shaneLeeCoding)
* [GitHub](https://github.com/ShaneLee)
* [CodeYogi](https://codeyogi.co.uk/)
​

Article originally posted [here](https://codeyogi.co.uk/2019/12/30/visualising-multiple-stocks-with-matplotlib/)