我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用matplotlib.mlab()。
def dZ_at_t(self, t): """ Interpolate dZ to specified time t and return deformation. """ from matplotlib.mlab import find if t <= self.times[0]: return self.dZ[0,:,:] elif t >= self.times[-1]: return self.dZ[-1,:,:] else: n = max(find(self.times <= t)) t1 = self.times[n] t2 = self.times[n+1] dz = (t2-t)/(t2-t1) * self.dZ[n,:,:] + \ (t-t1)/(t2-t1) * self.dZ[n+1,:,:] return dz
def plot_model(model, data): """ :param model: the GMM model :param data: the data set 2D :return: """ delta = 0.025 x = np.arange(0.0, 4, delta) y = np.arange(0.0, 4, delta) X, Y = np.meshgrid(x, y) z = np.zeros((np.size(x), np.size(y))) # sum of Gaussian plt.figure() for i in range(np.size(model)): ztemp = mlab.bivariate_normal(X, Y, np.sqrt(model['cov'][i][0, 0]), np.sqrt(model['cov'][i][1, 1]), model['mu'][i][0], model['mu'][i][1], model['cov'][i][0,1]) plt.contour(X, Y, model['w'][i]*ztemp) z = np.add(z, ztemp) plt.scatter(data[0, :], data[1, :], s=5) plt.figure() plt.contour(X, Y, z*np.size(model)) plt.scatter(data[0, :], data[1, :], s=5)
def save_contour(netD, filename, cuda=False): #import warnings #warnings.filterwarnings("ignore", category=FutureWarning) #import numpy as np #import matplotlib #matplotlib.use('Agg') #import matplotlib.cm as cm #import matplotlib.mlab as mlab #import matplotlib.pyplot as plt matplotlib.rcParams['xtick.direction'] = 'out' matplotlib.rcParams['ytick.direction'] = 'out' matplotlib.rcParams['contour.negative_linestyle'] = 'solid' # gen grid delta = 0.1 x = np.arange(-25.0, 25.0, delta) y = np.arange(-25.0, 25.0, delta) X, Y = np.meshgrid(x, y) # convert numpy array to to torch variable (h, w) = X.shape XY = np.concatenate((X.reshape((h*w, 1, 1, 1)), Y.reshape((h*w, 1, 1, 1))), axis=1) input = torch.Tensor(XY) input = Variable(input) if cuda: input = input.cuda() # forward output = netD(input) # convert torch variable to numpy array Z = output.data.cpu().view(-1).numpy().reshape(h, w) # plot and save plt.figure() CS1 = plt.contourf(X, Y, Z) CS2 = plt.contour(X, Y, Z, alpha=.7, colors='k') plt.clabel(CS2, inline=1, fontsize=10, colors='k') plt.title('Simplest default with labels') plt.savefig(filename) plt.close()
def plot_model(model, data): """ :param model: the GMM model :param data: the data set 2D :return: """ delta = 0.025 x = np.arange(0.0, 4, delta) y = np.arange(0.0, 4, delta) X, Y = np.meshgrid(x, y) z = np.zeros((np.size(x), np.size(y))) # sum of Gaussian plt.figure() for i in range(np.size(model)): ztemp = mlab.bivariate_normal(X, Y, np.sqrt(model['cov'][i][0, 0]), np.sqrt(model['cov'][i][1, 1]), model['mu'][i][0], model['mu'][i][1], model['cov'][i][0,1]) plt.contour(X, Y, model['w'][i]*ztemp) z = np.add(z, ztemp) plt.scatter(data[0, :], data[1, :], s=5) plt.figure() plt.contour(X, Y, z) plt.scatter(data[0, :], data[1, :], s=5)