Friday, 1 July 2016

Plotting Heat Map in Python


Python is the scripting language generally used by Data Scientists for the Analytics purpose. As I was making my journey to get expertise in Machine Learning & Analytics, started trying my programming skills using Python.
I used the following resources to get me started -

1) Pycharm - https://www.jetbrains.com/pycharm/
It provides the complete IDE for python and easy to use. There are some short exercises in this which can help in making the quick start on Python programming.

2) Coursera Course on Python (Coursera.org)
Few courses on Coursera are available that helps you take deep dive in Python.


As the first challenge to test my Python skills, tried to achieve following task :

(i) Create a 2-dimensional array of size 100x100 and populate it with random floating points between 0 and 1 (inclusive (i.e., [0,1]); 
(ii) plot the 2d array using any python library, to create a visual “heat map” representation of the data; 
(iii) write a loop that refreshes the numbers in the array and replots the heatmap each time the array is repopulated.  


I tried this program in ipython notebook.




%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
for i in range(1,5):
    data = np.random.rand(100,100)
    fig, ax = plt.subplots()
    ax.set_title("Heat Map Assignment:")
    heatmap = ax.pcolor(data)







Some of the points to note :
%matplotlib inline
Helped me to plot the heatmap inline in the ipython notebook only instead of opening the new browser window each time.

matplotlib library :
It provides the various functions to plot the graphs.

numpy :
Used the numpy here as it provided the easy way to create the multi dimensional array and populate it with random numbers. Only thing that I missed here is that how to make upper limit inclusive while populating the array with random floating point numbers. Still trying to find a way ...




This is the output :







No comments:

Post a Comment