Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

Creating Heatmap Using Python Seaborn

$
0
0

Creating Heatmap Using Python Seaborn

python Data Visualization Creating Heatmap using Seaborn by Milind Paradkar

In our previous blog article we talked about Data Visualization in Python using Bokeh . Now, let’s take our series on Python data visualization forward, and cover another cool data visualization Python package. In this post we will use the Python Seaborn package to create Heatmaps which can be used for various purposes, including by traders for tracking markets.

Seaborn for Python Data Visualization Seaborn is a Python data visualization library based on Matplotlib. It provides a high-level interface for drawing attractive statistical graphics. Because seaborn is built on top of Matplotlib, the graphics can be further tweaked using Matplotlib tools and rendered with any of the Matplotlib backends to generate publication-quality figures. [ 1 ]

Types of plots that can be created using seaborn includes:

Distribution plots Regression plots Categorical plots Matrix plots Timeseries plots The plotting functions operate on Python dataframes and arrays containing a whole dataset, and internally perform the necessary aggregation and statistical model-fitting to produce informative plots. [2]
Creating Heatmap Using Python Seaborn

Source: seaborn.pydata.org

What is a heatmap?

A heatmap is a two-dimensional graphical representation of data where the individual values that are contained in a matrix are represented as colors. The seaborn package allows for creation of annotated heatmaps which can be tweaked using Matplotlib tools as per the creator’s requirement.


Creating Heatmap Using Python Seaborn

Annotated Heatmap

Python Heatmap Code

We will create a seaborn heatmap for a group of 30 Pharmaceutical Company stocks listed on the National Stock Exchange of India Ltd (NSE). The heatmap will display the stock symbols and its respective single-day percentage price change.

We collate the required market data on Pharma stocks and construct a comma-separated values (CSV) file comprising of the stock symbols and their respective percentage price change in the first two columns of the CSV file.

Since we have 30 Pharma companies in our list, we will create a heatmap matrix of 6 rows and 5 columns. Further, we want our heatmap to display the percentage price change for the stocks in a descending order. To that effect we arrange the stocks in a descending order in the CSV file and add two more columns which indicate the position of each stock on X & Y axis of our heatmap.

Import the required Python packages

We import the following Python packages:


Creating Heatmap Using Python Seaborn
Load the dataset

We read the dataset using the read_csv function from pandas, and visualize the first ten rows using the print statement.


Creating Heatmap Using Python Seaborn
Creating Heatmap Using Python Seaborn
Create a Python Numpy array

Since we want to construct a 6 x 5 matrix, we create an n-dimensional array of the same shape for “Symbol” and the “Change” columns.


Creating Heatmap Using Python Seaborn
Creating Heatmap Using Python Seaborn
Creating Heatmap Using Python Seaborn
Create a Pivot in Python

The pivot function is used to create a new derived table from the given dataframe object “df”. The function takes three arguments;index,columns, andvalues. The cell values of the new table are taken from column given as thevaluesparameter, which in our case is the “Change” column.


Creating Heatmap Using Python Seaborn
Creating Heatmap Using Python Seaborn
Create an Array to Annotate the Heatmap

In this step we create an array which will be used to annotate the heatmap. We call the flatten method on the “symbol” and “percentage” arrays to flatten a Python list of lists in one line. The zip function which returns an iterator zips a list in Python . We run a Python For loop and by using the format function; we format the stock symbol and the percentage price change value as per our requirement.


Creating Heatmap Using Python Seaborn
Create the Matplotlib figure and define the plot

We create an empty Matplotlib plot and define the figure size. We also add the title to the plot and set the title’s font size, and its distance from the plot using set_position method.

We wish to display only the stock symbols and their respective single-day percentage price change. Hence, we hide the ticks for the X & Y axis, and also remove both the axes from the heatmap plot.


Creating Heatmap Using Python Seaborn
Create the Heatmap

In the final step, we create the heatmap using the heatmap function from the Python seaborn package. The heatmap function takes the following arguments:

data 2D dataset that can be coerced into an ndarray. If a Pandas DataFrame is provided, the index/column information will be used to label the columns and rows.

annot an array of same shape as data which is used to annotate the heatmap.

cmap a matplotlib colormap name or object. This maps the data values to the color space.

fmt string formatting code to use when adding annotations.

linewidths sets the width of the lines that will divide each cell.


Creating Heatmap Using Python Seaborn
Creating Heatmap Using Python Seaborn

Here’s our final output of the seaborn heatmap for the chosen group of pharmaceutical companies. Looks pretty neat and clean, doesn’t it? A quick glance at this heatmap and one can easily make out how the market is faring for the period.

Download the Python Heatmap Code

Readers can download the entire Python code plus the excel file using the download button provided below and create their own custom heatmaps. A little tweak in the Python code and you can create Python heatmaps of any size, for

Viewing all articles
Browse latest Browse all 9596

Trending Articles