Skip to content

Instantly share code, notes, and snippets.

@mukundraj
Last active January 27, 2017 05:12
Show Gist options
  • Save mukundraj/c557bcc40e30dca81d00de2230a43c27 to your computer and use it in GitHub Desktop.
Save mukundraj/c557bcc40e30dca81d00de2230a43c27 to your computer and use it in GitHub Desktop.
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
import matplotlib.mlab as mlab
plt.figure(facecolor="white")
F = plt.gcf() # get current frame.
DefaultSize = F.get_size_inches()
F.set_size_inches((DefaultSize[0]*1.5 ,DefaultSize[1]*1.0))
plt.suptitle('main title', fontsize=20)
plt.subplot(2,2,1)
plt.title('sin(x)')
x = np.linspace(-1, 1, 200)
y = np.sin(x)
plt.plot(x, y, 'b-', linewidth=2)
plt.axis('equal') # Making the x and y scales to be the same.
plt.xlabel('x')
plt.ylabel('f(x)=sin(x)')
x1 = np.random.uniform(-1,1,10)
y1 = np.random.uniform(-1,1,10)
x2 = np.random.uniform(-1,1,10)
y2 = np.random.uniform(-1,1,10)
plt.subplot(2,2,3)
plt.title('Random points from two classes')
y = np.cos(x)
plt.scatter(x1,y1, s=20, c='b', marker="s", label='class 1')
plt.scatter(x2,y2, s=20, c='r', marker="o", label='class 2')
ax = plt.gca() # get current axis
for i in range(len(x1)):
ax.annotate(str(i), (x1[i],y1[i]))
for i in range(len(x2)):
ax.annotate(str(i), (x2[i],y2[i]))
plt.legend(loc='best')
plt.axis('equal')
plt.xlabel('x')
plt.ylabel('y')
plt.tight_layout(pad=0.4, w_pad=2.5, h_pad=1.0, rect=(0,0,1,0.95)) # Avoiding label overlaps.
plt.subplot(2,2,2)
ax2 = plt.subplot2grid((2,2), (0, 1), rowspan=2)
plt.title('Scalar field (heatmap) and \nvectors (arrows)')
delta = 0.025
x = np.arange(-4.0, 4.0, delta)
y = np.arange(-6.0, 6.0, delta)
X, Y = np.meshgrid(x, y)
Z1 = mlab.bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0)
Z2 = mlab.bivariate_normal(X, Y, 1.5, 0.5, 1, 1)
# difference of Gaussians
Z = 10.0 * (Z2 - Z1)
plt.pcolor(X, Y, Z, cmap=cm.jet, vmin=np.amin(Z), vmax=np.amax(Z))
plt.scatter(x1, y1, 30, x1, cmap=cm.jet, vmin=np.amin(x1), vmax=np.amax(x1))
plt.colorbar()
CS = plt.contour(X,Y,Z, levels=[0.7,0.9], colors='k')
plt.clabel(CS, inline=1, fontsize=10)
Gxy = np.random.rand(len(x1),2)
ax2.quiver(x1, y1, -Gxy[:,0], -Gxy[:,1], angles='xy',scale=10,color='g')
plt.axis('equal')
plt.xlabel('x')
plt.ylabel('y')
plt.savefig('image.png', bbox_inches='tight')
plt.show(block=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment