Stock Return Heatmap using Seaborn

Hi all, this post is going to be a relatively short and to the point run through of creating an annotated heatmap for the Dow 30 stock returns using the Python Seaborn package.

Let’s start with what is a heatmap actually is; it’s defined as “a representation of data in the form of a map or diagram in which data values are represented as colours.”

This makes it a great tool to quickly visualise the magnitude of stock returns over time in a matrix/grid format, using a colour map/scale to represent the size and direction of each stock’s percentage change over that period of time.

Creating a heatmap without stock ticker labels annotated, i.e. a heatmap annotated with just the numerical value of the relevant cell is a very easy process, thanks to the power and ease of use of Seaborn.

It can be achieved as follows:

The above code gets the data we actually need to populate the heatmap, with the actual heatmap creating being as easy as folows:

This creates the following:

The above is all well and good, but we can’t actually see which cell relates to which individual stock! Not ideal.

The (slightly) tricky part is going to be creating the label arrays to actually annotate the heatmap with the relevant cell’s stock ticker information.

This can be added as follows:

Create the new heatmap, this time using the “annot” call to use our newly created “labels” list to annotate it.

We now have the following:

A nicely annotated heatmap showing both the returns and stock ticker relevant to each of the Dow 30 stocks.

I’ll leave this post here, and as always – any questions or comments just leave them below.


It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInEmail this to someonePin on PinterestShare on Reddit
Written by s666