Search This Blog

6 Matplotlib, seaborn, Cufflinks

1) Matplotlib

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+. 

There is also a procedural "pylab" interface based on a state machine (like OpenGL), designed to closely resemble that of MATLAB, though its use is discouraged.[3] SciPy makes use of Matplotlib.

https://en.wikipedia.org/wiki/Matplotlib


Reference: https://matplotlib.org/stable/users/index.html

All of plotting functions expect numpy.array or numpy.ma.masked_array as input.

Example1:

import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline

plt.plot([1, 2, 3, 4], [1, 4, 2, 3])
O/p:

Example2:

import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(0,20,11)
y=x**2

plt.plot(x,y, 'r*-') # r for red color
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.title('Matplotlib Exmaple')
o/p:



Example3:

#Multiple graphs
plt.subplot(2,2,1) # 2,2,1 implies 2rows, 2columns, 1st index
plt.plot(x,y, 'r-')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.title('Matplotlib Exmaple')

plt.subplot(2,2,2) # 2,2,1 implies 2rows, 2columns, 2nd index
plt.plot(x,y, 'r*')
plt.xlabel('X axis')
plt.ylabel('Y axis')
plt.title('Matplotlib Exmaple')

plt.subplot(2,2,3) # 2,2,1 implies 2rows, 2columns, 3rd index
plt.plot(x,y, 'r*-')
plt.xlabel('X axis')
plt.ylabel('Y axis')
#plt.title('Matplotlib Exmaple')

plt.subplot(2,2,4) # 2,2,1 implies 2rows, 2columns, 4th index
plt.plot(x,y, 'b*')
plt.xlabel('X axis')
plt.ylabel('Y axis')
#plt.title('Matplotlib Exmaple')

O/p:

Example 4: Pyplot style

# pyplot style
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2, 100)

plt.plot(x, x, label='linear') # Plot some data on the (implicit) axes.
plt.plot(x, x**2, label='quadratic') # etc.
plt.plot(x, x**3, label='cubic')
plt.xlabel('x label')
plt.ylabel('y label')
plt.title("Simple Plot")
plt.legend()
O/p:


Example 5: Object Oriented style

# Note that even in the OO-style, we use `.pyplot.figure` to create the figure.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2, 100)


fig, ax = plt.subplots() # Create a figure and an axes.
ax.plot(x, x, label='linear') # Plot linear data on the axes.
ax.plot(x, x**2, label='quadratic') # Plot quadratic data on the axes.
ax.plot(x, x**3, label='cubic') # Plot cubic data.
ax.set_xlabel('x label') # Add an x-label to the axes.
ax.set_ylabel('y label') # Add a y-label to the axes.
ax.set_title("Simple Plot") # Add a title to the axes.
ax.legend() # Add a legend.
O/p:











No comments:

Post a Comment