Python Plotting With Matplotlib

Drawing attractive figures and plots is an important part of Data Science. In Python, the most frequently used plotting package is matplotlib , and is written in pure Python. It is probably the single most used Python package for creating static, animated, and interactive visualizations. Matplotlib package provides both a very quick way to visualize data from Python and professional-quality figures in different formats. It is highly customizable , but it can be hard to know what settings to tweak to achieve an attractive plot. Also, matplotlib makes heavy use of NumPy arrays and other extension code to provide best performance even for large arrays.

Installing matplotlib

There are several ways to install matplotlib to your operating system. The esaiest way is to using pip command. The pip can also use to install the matplotlib library . Open the command prompt, type the following command.
pip install matplotlib
Once you can successfully install matplotlib library , then you are ready to continue. Example 1
from matplotlib import pyplot as plt plt.plot([10,20,30],[40,50,10]) plt.show()
In the above code, import pyplot from matplotlib - use pyplot to "plot" some data to the canvas in memory and then use plt.show(), which is pyplot, to show what you've got.
Python matplotlib
Example 2
from numpy import * import matplotlib.pyplot as plt x = arange(1001) y = mod(x,2.87) l1 = plt.hist(y,color='y',rwidth = 0.8) lx = plt.xlabel("y") ly = plt.ylabel("num(y)") tl = plt.title("y = mod(arange(1001),2.87)") plt.show()

pyplot from matplotlib
Example-3
from numpy import * import matplotlib.pyplot as plt x = linspace(0,10,51) y = linspace(0,8,41) (X,Y) = meshgrid(x,y) a = exp(-((X-2.5)**2 + (Y-4)**2)/4) - exp(-((X-7.5)**2 + (Y-4)**2)/4) c = plt.contour(x,y,a) l = plt.clabel(c) lx = plt.xlabel("x") ly = plt.ylabel("y") plt.show()

matplot python data science
Example-4
from matplotlib import pyplot as plt from matplotlib import style style.use('ggplot') x = [5,8,10] y = [12,16,6] x2 = [6,9,11] y2 = [6,15,7] plt.bar(x, y, align='center') plt.bar(x2, y2, color='g', align='center') plt.title('Example-4') plt.ylabel('Y-axis') plt.xlabel('X-axis') plt.show()

python data science libraries
Example-5
from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import numpy as np fig = plt.figure() ax = fig.add_subplot(111,projection='3d') for c, z in zip(['r', 'g', 'b', 'y'], [30, 20, 10, 0]): xs = np.arange(20) ys = np.random.rand(20) cs = [c] * len(xs) cs[0] = 'c' ax.bar(xs, ys, zs=z, zdir='y', color=cs, alpha=0.8) ax.set_xlabel('X-Axis') ax.set_ylabel('Y-Axis') ax.set_zlabel('Z-Axis') plt.show()

Python data science tutorial