Showing posts with label matplotlib. Show all posts
Showing posts with label matplotlib. Show all posts

Thursday 7 December 2017

Python Programming for Data Analytics: Matplotlib library Basics

Matplotlib Tool
Matplotlib is a plotting library for the Python programming language and its numerical mathematics extension NumPy. It provides an object-oriented API for embedding plots into applications using general-purpose GUI toolkits like TKinter, wxPython, Qt, or GTK+.
Source: Wikipedia

Importing matplotlib and numpy to plot line graph
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
x = np.linspace(0, 5, 11)               #generate 1D array starting from 0—5, total 11 elements
y = x ** 2                                         # x2
plt.plot(x, y, 'r’)                              # 'r' is the color red
plt.xlabel('X Axis Title Here')                      #label x asis
plt.ylabel('Y Axis Title Here')                      # label y axis
plt.title('String Title Here')                         # give title to plot
plt.show()                                                     # show plot

Create subplot in single plot (like MATLAB)
plt.subplot(1,2,1)                          # 1 row, 2 columns, 1st figure in row
plt.plot(x, y, 'r--')                            # r—red color, -- for dashed graph lines
plt.subplot(1,2,2)                          # 1 row, 2 columns, 2nd figure in row
plt.plot(y, x, 'g*-');                         # g—green color, *- for graph lines

Use subplot functions to create MATLAB style line color
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
x = np.linspace(0, 5, 11)
fig, ax = plt.subplots()                              #generate 2 objects, fig and ax
ax.plot(x, x**2, 'b.-')                               # blue line with dots
ax.plot(x, x**3, 'g--')                              # green dashed line
ax.set_xlabel('x axis’)
ax.set_ylabel('y axis')
ax.set_title(' plot title')

Plot various types of plots (scatter, step, bar and fill) in same figure
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

n = np.array([0,1,2,3,4,5])
xx = np.linspace(-0.75, 1., 100)
fig, axes = plt.subplots(1, 4, figsize=(12,3))        #create figure of dimension 12x3 inch, with 4 subplots
axes[0].scatter(xx, xx + 0.25*np.random.randn(len(xx)))
axes[0].set_title("scatter")
axes[1].step(n, n**2, lw=2)                       #lw=line width of 2
axes[1].set_title("step")
axes[2].bar(n, n**2, align="center", width=0.5, alpha=0.5)     #alpha – transparency level
axes[2].set_title("bar")
axes[3].fill_between(xx, xx**2, xx**3, color="green", alpha=0.5);
axes[3].set_title("fill_between");

Saving plot into file format
fig.savefig("filename.png")

fig.savefig("filename.png", dpi=200)

Hope you have enjoyed working with Matplotlib library and plotted beautiful graphs of your data :-)
In next post, I would come up with another popular and important library used for Data Analytics purpose. 
Keep learning.Keep Growing :-) Please post your comments below !