def function(position): xg = 3.0 * position[0] yg = 3.0 * position[1] h1 = bivariate_normal(xg, yg, 1.0, 1.0, 0.0, 0.0) h2 = bivariate_normal(xg, yg, 1.5, 0.5, 1, 1) h = 10.0 * (h1 - h2) return h
def heatmap_with_hexagon_cell(x,y,timestamp): from matplotlib import cm from matplotlib import mlab as ml n = 1e5 #x = y = NP.linspace(-5, 5, 100) X, Y = np.meshgrid(x, y) Z1 = ml.bivariate_normal(X, Y, 2, 2, 0, 0) Z2 = ml.bivariate_normal(X, Y, 4, 1, 1, 1) ZD = Z2 - Z1 x = X.ravel() y = Y.ravel() z = ZD.ravel() gridsize=300 plt.subplot(111) # if 'bins=None', then color of each hexagon corresponds directly to its count # 'C' is optional--it maps values to x-y coordinates; if 'C' is None (default) then # the result is a pure 2D histogram plt.hexbin(x, y, C=z, gridsize=gridsize, cmap=cm.jet, bins=None) plt.axis([x.min(), x.max(), y.min(), y.max()]) plt.text(18.9300,72.8200,"ChurchGate",bbox=dict(facecolor='green', alpha=0.5)) plt.text(19.1833,72.8333,"Malad",bbox=dict(facecolor='green', alpha=0.5)) plt.text(19.0587,72.8997,"Chembur",bbox=dict(facecolor='green', alpha=0.5)) plt.text(19.2045,72.8376,"Kandivili",bbox=dict(facecolor='green', alpha=0.5)) cb = plt.colorbar() cb.set_label('mean value') plt.show()
def calcAtomGaussians(mol,a=0.03,step=0.02,weights=None): """ useful things to do with these: fig.axes[0].imshow(z,cmap=cm.gray,interpolation='bilinear',origin='lower',extent=(0,1,0,1)) fig.axes[0].contour(x,y,z,20,colors='k') fig=Draw.MolToMPL(m); contribs=Crippen.rdMolDescriptors._CalcCrippenContribs(m) logps,mrs=zip(*contribs) x,y,z=Draw.calcAtomGaussians(m,0.03,step=0.01,weights=logps) fig.axes[0].imshow(z,cmap=cm.jet,interpolation='bilinear',origin='lower',extent=(0,1,0,1)) fig.axes[0].contour(x,y,z,20,colors='k',alpha=0.5) fig.savefig('coumlogps.colored.png',bbox_inches='tight') """ import numpy from matplotlib import mlab x = numpy.arange(0,1,step) y = numpy.arange(0,1,step) X,Y = numpy.meshgrid(x,y) if weights is None: weights=[1.]*mol.GetNumAtoms() Z = mlab.bivariate_normal(X,Y,a,a,mol._atomPs[0][0], mol._atomPs[0][1])*weights[0] for i in range(1,mol.GetNumAtoms()): Zp = mlab.bivariate_normal(X,Y,a,a,mol._atomPs[i][0], mol._atomPs[i][1]) Z += Zp*weights[i] return X,Y,Z
def filtered_text(ax): delta = 0.025 x = np.arange(-3.0, 3.0, delta) y = np.arange(-2.0, 2.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) Z = 10.0 * (Z2 - Z1) im = ax.imshow(Z, interpolation='bilinear', origin='lower', cmap=cm.gray, extent=(-3,3,-2,2)) levels = np.arange(-1.2, 1.6, 0.2) CS = ax.contour(Z, levels, origin='lower', linewidths=2, extent=(-3,3,-2,2)) ax.set_aspect("auto") cl = ax.clabel(CS, levels[1::2], # label every second level inline=1, fmt='%1.1f', fontsize=11) for t in cl: t.set_color("k") white_glows = FilteredArtistList(cl, GrowFilter(3)) ax.add_artist(white_glows) white_glows.set_zorder(cl[0].get_zorder()-0.1) ax.xaxis.set_visible(False) ax.yaxis.set_visible(False)
def main(args): x = np.arange(-3.0, 3.0, 0.025) y = np.arange(-3.0, 3.0, 0.025) X, Y = np.meshgrid(x, y) Z0 = mlab.bivariate_normal(X, Y, 1.0, 4.0, 0.0, 0.0) Z1 = mlab.bivariate_normal(X, Y, 4.0, 1.0, 0.0, 0.0) Z = Z1 - Z0 cdict = { "red": ((0.0, 0.0, 0.98), (1.0, 1.0, 1.0)), "green": ((0.0, 0.0, 0.74), (1.0, 1.0, 1.0)), "blue": ((0.0, 0.0, 0.0), (1.0, 1.0, 1.0)), } _cmap = LinearSegmentedColormap("tmp", cdict) plt.imshow(Z >= 0, interpolation="nearest", cmap=_cmap, extent=(-3, 3, -3, 3)) CS = plt.contour(Z, [0], colors="g", extent=(-3, 3, -3, 3)) plt.clabel(CS, inline=1, manual=[(-2, 2), (2, 2)], fmt="boundaries") CS0 = plt.contour(X, Y, Z0, 4, colors="r") plt.clabel(CS0, inline=1, manual=[(0, 0)], fmt="p(x | y = 0)") plt.text(0, -2, "f(x) = 0") plt.text(0, 2, "f(x) = 0") CS1 = plt.contour(X, Y, Z1, 4, colors="b") plt.clabel(CS1, inline=1, manual=[(0, 0)], fmt="p(x | y = 1)") plt.text(-2, 0, "f(x) = 1") plt.text(2, 0, "f(x) = 1") plt.show()
def test_mask_image_over_under(): delta = 0.025 x = y = np.arange(-3.0, 3.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) Z = 10*(Z2 - Z1) # difference of Gaussians palette = copy(plt.cm.gray) palette.set_over('r', 1.0) palette.set_under('g', 1.0) palette.set_bad('b', 1.0) Zm = ma.masked_where(Z > 1.2, Z) fig, (ax1, ax2) = plt.subplots(1, 2) im = ax1.imshow(Zm, interpolation='bilinear', cmap=palette, norm=colors.Normalize(vmin=-1.0, vmax=1.0, clip=False), origin='lower', extent=[-3, 3, -3, 3]) ax1.set_title('Green=low, Red=high, Blue=bad') fig.colorbar(im, extend='both', orientation='horizontal', ax=ax1, aspect=10) im = ax2.imshow(Zm, interpolation='nearest', cmap=palette, norm=colors.BoundaryNorm([-1, -0.5, -0.2, 0, 0.2, 0.5, 1], ncolors=256, clip=False), origin='lower', extent=[-3, 3, -3, 3]) ax2.set_title('With BoundaryNorm') fig.colorbar(im, extend='both', spacing='proportional', orientation='horizontal', ax=ax2, aspect=10)
def get_image(): delta = 0.25 x = y = np.arange(-3.0, 3.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) Z = Z2 - Z1 # difference of Gaussians return Z
def get_gauss_kernel(sigma, size, res): """ return a two dimesional gausian kernel of shape (size*(1/resolution),size*(1/resolution)) with a std deviation of std """ x,y = ny.mgrid[-size/2:size/2:res,-size/2:size/2:res] b=bivariate_normal(x,y,sigma,sigma) A=(1/ny.max(b)) #A=1 return x,y,A*bivariate_normal(x, y, sigma, sigma)
def __call__(self, inputs): from matplotlib.mlab import bivariate_normal X = self.get_input('X') Y = self.get_input('Y') Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1) # difference of Gaussians Z = 10.0 * (Z2 - Z1) return Z
def get_demo_image(): delta = 0.5 extent = (-3,4,-4,3) x = np.arange(-3.0, 4.001, delta) y = np.arange(-4.0, 3.001, delta) X, Y = np.meshgrid(x, y) import matplotlib.mlab as mlab 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) Z = (Z1 - Z2) * 10 return Z, extent
def create_plot(): x = np.linspace(-3.0, 3.0, 30) y = np.linspace(-2.0, 2.0, 30) 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) Z = 10.0 * (Z2 - Z1) fig, ax = plt.subplots() CS = ax.contourf(X, Y, Z, 30) return fig
def testcontour(self): #contour plot test data: delta = 0.025 x = np.arange(-3.0, 3.0, delta) y = np.arange(-2.0, 2.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.contour(X, Y, Z, 20)
def test3(): N = 1000 x = (np.linspace(-2.0, 3.0, N)) y = (np.linspace(-2.0, 2.0, N)) X, Y = np.meshgrid(x, y) X1 = 0.5*(X[:-1,:-1] + X[1:,1:]) Y1 = 0.5*(Y[:-1,:-1] + Y[1:,1:]) from matplotlib.mlab import bivariate_normal Z1 = bivariate_normal(X1, Y1, 0.1, 0.2, 1.27, 1.11) + 100.*bivariate_normal(X1, Y1, 1.0, 1.0, 0.23, 0.72) Z1[Z1>0.9*np.max(Z1)] = +np.inf cdict = {'red': [(0.0, 1.0, 1.0), (1.0, 1.0, 1.0)], 'green': [(0.0, 0.0, 0.0), (1.0, 0.0, 0.0)], 'blue': [(0.0, 0.0, 0.0), (1.0, 0.0, 0.0)], 'alpha': [(0.0, 0.0, 0.0), (1.0, 1.0, 1.0)], } # this is a color maps showing how the resukt should look like cdictx = {'red': [(0.0, 1.0, 1.0), (1.0, 1.0, 1.0)], 'green': [(0.0, 1.0, 1.0), (1.0, 0.0, 0.0)], 'blue': [(0.0, 1.0, 1.0), (1.0, 0.0, 0.0)], } fig = plt.figure() ax = fig.add_subplot(1,1,1) col = mpl_colors.LinearSegmentedColormap('test', cdict) i = ax.pcolorfast(x, y, Z1, cmap = col) plt.colorbar(i) fig = plt.figure() ax = fig.add_subplot(1,1,1) col = mpl_colors.LinearSegmentedColormap('testx', cdictx) i = ax.pcolorfast(x, y, Z1, cmap = col) plt.colorbar(i) plt.show()
def main(): x = np.linspace(-3.0, 3.0, 30) y = np.linspace(-2.0, 2.0, 30) 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) Z = 10.0 * (Z2 - Z1) fig, ax = plt.subplots() CS = ax.contour(X, Y, Z) ax.clabel(CS, inline=True, fontsize=10) return fig
def test_image_plot(self): delta = 0.025 x = y = np.arange(-3.0, 3.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) Z = Z2 - Z1 # difference of Gaussians im = plt.imshow(Z, interpolation='bilinear', cmap=cm.RdYlGn, origin='lower', extent=[-3, 3, -3, 3], vmax=abs(Z).max(), vmin=-abs(Z).max()) plt.show()
def calcAtomGaussians(mol,a=0.03,step=0.02,weights=None): import numpy from matplotlib import mlab x = numpy.arange(0,1,step) y = numpy.arange(0,1,step) X,Y = numpy.meshgrid(x,y) if weights is None: weights=[1.]*mol.GetNumAtoms() Z = mlab.bivariate_normal(X,Y,a,a,mol._atomPs[0][0], mol._atomPs[0][1])*weights[0] # this is not bivariate case ... only univariate no mixtures #matplotlib.mlab.bivariate_normal(X, Y, sigmax=1.0, sigmay=1.0, mux=0.0, muy=0.0, sigmaxy=0.0) for i in range(1,mol.GetNumAtoms()): Zp = mlab.bivariate_normal(X,Y,a,a,mol._atomPs[i][0], mol._atomPs[i][1]) Z += Zp*weights[i] return X,Y,Z
def main(): # Part of the example at # http://matplotlib.sourceforge.net/plot_directive/mpl_examples/pylab_examples/contour_demo.py delta = 0.025 x = numpy.arange(-3.0, 3.0, delta) y = numpy.arange(-2.0, 2.0, delta) X, Y = numpy.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) Z = 10.0 * (Z2 - Z1) pyplot.figure() CS = pyplot.contour(X, Y, Z) pyplot.show()
def get_test_data(delta=0.05): from matplotlib.mlab import meshgrid, bivariate_normal x = y = npy.arange(-3.0, 3.0, delta) X, Y = meshgrid(x,y) Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1) Z = Z2-Z1 X = X * 10 Y = Y * 10 Z = Z * 500 return X,Y,Z
def calcAtomGaussians(mol,a=0.03,step=0.02,weights=None): import numpy from matplotlib import mlab x = numpy.arange(0,1,step) y = numpy.arange(0,1,step) X,Y = numpy.meshgrid(x,y) if weights is None: weights=[1.]*mol.GetNumAtoms() Z = mlab.bivariate_normal(X,Y,a,a,mol._atomPs[0][0], mol._atomPs[0][1])*weights[0] for i in range(1,mol.GetNumAtoms()): Zp = mlab.bivariate_normal(X,Y,a,a,mol._atomPs[i][0], mol._atomPs[i][1]) Z += Zp*weights[i] return X,Y,Z
def image_demo(fig, ax): delta = 0.025 x = y = np.arange(-3.0, 3.0, delta) xx, yy = np.meshgrid(x, y) z1 = mlab.bivariate_normal(xx, yy, 1.0, 1.0, 0.0, 0.0) z2 = mlab.bivariate_normal(xx, yy, 1.5, 0.5, 1, 1) image = z2-z1 # Difference of Gaussians img_plot = ax.imshow(image) ax.set_title('image') fig.tight_layout() # `colorbar` should be called after `tight_layout`. fig.colorbar(img_plot, ax=ax)
def slit_image(self, y_strength): x = numpy.arange(0, self.length*self.length_mult+1, 1.0) y = numpy.arange(0, self.width*self.width_mult+1, 1.0) X, Y = numpy.meshgrid(x, y) ptsource = mlab.bivariate_normal(X, Y, self.FWHM, self.FWHM, len(x)/2.0+(self.object_location-0.5)*self.length, len(y)/2.0) sky = numpy.zeros([len(y), len(x)]) for i in numpy.arange(len(x)/2.0-self.length/2.0, len(x)/2.0+self.length/2.0, 0.1): for j in numpy.arange(len(y)/2.0-self.width/2.0, len(y)/2.0+self.width/2.0, 0.1): sky += ((numpy.random.randn(1))**2.0)*mlab.bivariate_normal(X, Y, self.FWHM, self.FWHM, i, j) #ptsource = mlab.bivariate_normal composite = numpy.round(sky) + numpy.round(ptsource*500.0*y_strength) return composite
def get_test_data(delta=0.05): ''' Return a tuple X, Y, Z with a test data set. ''' from matplotlib.mlab import bivariate_normal x = y = np.arange(-3.0, 3.0, delta) X, Y = np.meshgrid(x, y) Z1 = bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) Z2 = bivariate_normal(X, Y, 1.5, 0.5, 1, 1) Z = Z2 - Z1 X = X * 10 Y = Y * 10 Z = Z * 500 return X, Y, Z
def check_abnormal_with_density(meanx, meany, stdx, stdy, target_sz): normal_sz = 264 target_sz = target_sz X = np.arange(plot_lim_min, plot_lim_max, 0.1) Y = np.arange(plot_lim_min, plot_lim_max, 0.1) mX, mY = np.meshgrid(X, Y) n1 = mlab.bivariate_normal(mX, mY, 2.15, 0.89, 15.31, -6.5) n2 = mlab.bivariate_normal(mX, mY, 3.16, 3.21, 18, -17.5) n3 = mlab.bivariate_normal(mX, mY, 1.79, 1, 17.5, -11.3) n4 = mlab.bivariate_normal(mX, mY, 3.6, 2.5, 16, -11.4) n5 = mlab.bivariate_normal(mX, mY, 3.4, 2.6, 14, -18) n6 = mlab.bivariate_normal(mX, mY, 3.65, 4.85, 11, 4.7) n7 = mlab.bivariate_normal(mX, mY, 1.85, 1.45, 15.8, -13) normal_dist = n1 + n2 + n3 + n4 + n5 + n6 + n7 target_dist = mlab.bivariate_normal(mX, mY, stdx, stdy, meanx, meany) normal_dist = normal_dist*(normal_sz/float(normal_sz+target_sz)) target_dist = target_dist*(target_sz/float(target_sz+target_sz)) s = 0 for x in range(len(X)) : for y in range(len(Y)): det = target_dist[x,y] - normal_dist[x,y] if det > 0: s = s + det return s
def test(): """Test the plotdata routine.""" # Generate and plot test data delta = 0.025 x = np.arange(-3.0, 3.0, delta) y = np.arange(-2.0, 2.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) plotdata(Z, title="test data", fill=False, mono=False) plotdata(Z, title="Fill in mono", fill=True, mono=True)
def init(): """ This is where our contour is created """ sigma = np.random.uniform(0, 1, 4) Z1 = mlab.bivariate_normal(X, Y, sigma[0], sigma[1], 0.0, 0.0) Z2 = mlab.bivariate_normal(X, Y, sigma[2], sigma[3], 1, 1) Z = (Z1 - Z2) * 10 norm = cm.colors.Normalize(vmax=abs(Z).max(), vmin=-abs(Z).max()) cmap = cm.PRGn contf = plt.contourf(X, Y, Z, levels, cmap=cm.get_cmap(cmap, len(levels) - 1), norm=norm) return contf
def countStatParams(self): N = len(self.sample) M = self.parameters.dimension m = [0 for i in range(M)] for i in range(M): sum = 0 for j in range(N): sum += self.sample[j][i] m[i] = (sum + 0.0) / N r = [[0 for i in range(M)] for j in range(M)] for j in range(M): for l in range(j, M): sum = 0 for i in range(N): sum += (self.sample[i][j] - m[j]) * (self.sample[i][l] - m[l]) r[j][l] = r[l][j] = (sum + 0.0) / N self.results = DistributionParameters(1, M, m, r) x_ = self.frstVar.currentIndex() y_ = self.scndVar.currentIndex() N = 100 x1 = np.linspace(-(self.results.covariation[x_][x_] + 7) + self.results.expectations[x_], (self.results.covariation[x_][x_] + 7) + self.results.expectations[x_], N) y1 = np.linspace(-(self.results.covariation[y_][y_] + 7) + self.results.expectations[y_], (self.results.covariation[y_][y_] + 7) + self.results.expectations[y_], N) X, Y = np.meshgrid(x1, y1) Z = mlab.bivariate_normal(X, Y, math.sqrt(self.results.covariation[x_][x_]), math.sqrt(self.results.covariation[y_][y_]), self.results.expectations[x_], self.results.expectations[y_], self.results.covariation[x_][y_]) self.sc1.contour(X, Y, Z, 10, 'yellow') self.analyzeCnt += 1 self.analyzedSignal.emit()
def startAnalyze(self): x_ = self.frstVar.currentIndex() y_ = self.scndVar.currentIndex() x = [] y = [] if self.expNum.value() > 1000000: #10^6 -- maxsize for i in range(self.expNum.value()): if random.randint(0, self.expNum.value() - 1) < 1000000: x.append(self.sample[i][x_]) y.append(self.sample[i][y_]) else: x = [self.sample[i][x_] for i in range(self.expNum.value())] y = [self.sample[i][y_] for i in range(self.expNum.value())] self.sc1.clear() self.sc1.plot(x, y, '.', 'black') N = 100 x1 = np.linspace(-(self.parameters.covariation[x_][x_] + 7) + self.parameters.expectations[x_], (self.parameters.covariation[x_][x_] + 7) + self.parameters.expectations[x_], N) y1 = np.linspace(-(self.parameters.covariation[y_][y_] + 7)+ self.parameters.expectations[y_], (self.parameters.covariation[y_][y_] + 7 ) + self.parameters.expectations[y_], N) X, Y = np.meshgrid(x1, y1) Z = mlab.bivariate_normal(X, Y, math.sqrt(self.parameters.covariation[x_][x_]), math.sqrt(self.parameters.covariation[y_][y_]), self.parameters.expectations[x_], self.parameters.expectations[y_], self.parameters.covariation[x_][y_]) self.sc1.contour(X, Y, Z, 10, 'red') self.analyzeCnt += 1 self.analyzedSignal.emit()
def plot_mog(dataset, means, sigma, classes, ax1=None): ''' Draws a scatterplot of the clusterized data Args: dataset: Dataset to be plotted clusters: Clusters centers classes: Clases of each point in the dataset ''' k = len(means) cs = range(k) classes = np.argmax(classes,1) print classes x = np.arange(-3, 4, 0.025) y = np.arange(-4, 2.5, 0.025) X, Y = np.meshgrid(x, y) cm = plt.get_cmap('Set1', k) if ax1 is None: ax1 = plt.gca() ax1.scatter(dataset[:, 0], dataset[:, 1], c=classes, s=25, alpha=0.6, cmap=cm, label="Data") ax1.scatter(means[:, 0], means[:, 1], marker='*', c=cs, s=250, linewidths=3, cmap=cm, label="Clusters") for i in range(len(sigma)): Z = mlab.bivariate_normal(X, Y, sigma[i], sigma[i], means[i,0], means[i,1]) ax1.contour(X,Y,Z, colors=[cm(i)]) plt.title('Mixture of Gaussians') plt.xlabel('X1') plt.ylabel('X2') plt.grid()
def plotPost2D(mdn, y, rangex = [0, 1], rangey = [0, 1], deltax = 0.01, deltay = 0.01, true_model = None): M = mdn.M alpha, sigma, mu = mdn.getMixtureParams(y) print 'mu: ' + str(mu) print 'sigma: ' + str(sigma) print 'true value: ' + str(true_model) xlin = np.arange(rangex[0], rangex[1], deltax) ylin = np.arange(rangey[0], rangey[1], deltay) [XLIN, YLIN] = np.meshgrid(xlin, ylin) phi = np.zeros([M,ylin.shape[0], xlin.shape[0]]) P = np.zeros([ylin.shape[0], xlin.shape[0]]) for k in range(M): phi[k,:,:] = mlab.bivariate_normal(XLIN, YLIN, np.sqrt(sigma[k]), np.sqrt(sigma[k]), mu[k,0], mu[k,1]) P = P + phi[k,:,:] * alpha[k] plt.imshow(P, #interpolation='bilinear', ##cmap=cm.gray, origin='lower', extent=[rangex[0],rangex[1], rangey[0],rangey[1]] ) #plt.contour(XLIN, YLIN, P, #levels = [0, 1.0/np.exp(1)] # ) #plt.scatter(true_model[0],true_model[1],marker='^', c="r") if not true_model == None: plt.axvline(true_model[0], c = 'r') plt.axhline(true_model[1], c = 'r')
def create_2d_sample(f, b): # stolen fom matplolib examples http://matplotlib.org/examples/pylab_examples/image_demo.html delta = 0.025 x = y = np.arange(-3.0, 3.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) Z = Z2 - Z1 # difference of Gaussians da = b.create_data_array("difference of Gaussians", "nix.2d.heatmap", data=Z) d1 = da.append_sampled_dimension(delta) d1.label = "x" d1.offset = -3. d2 = da.append_sampled_dimension(delta) d2.label = "y" d2.offset = -3.
def f5(X, Y, random_seed=2017, sigma=0.5): ''' superposition multiple Gaussian distributions ''' random.seed(random_seed) for i in range(300): mu1, mu2 = np.array([random.uniform(-10, 10) for i in range(2)]) sigma1 = random.uniform(sigma, 3) sigma2 = random.uniform(sigma * 0.8, 4) sigma12 = random.uniform( min(sigma1, sigma2) / 20, min(sigma1, sigma2) / 2) if i == 0: Z = mlab.bivariate_normal(X, Y, sigma1, sigma2, mu1, mu2, sigma12) else: Z = Z + mlab.bivariate_normal(X, Y, sigma1, sigma2, mu1, mu2, sigma12) return Z
def part_b(): change = 0.025 x = np.arange(-10.0, 10.0, change) y = np.arange(-10.0, 10.0, change) X, Y = np.meshgrid(x, y) Gauss = mlab.bivariate_normal(X, Y, 2.0, 3.0, -1.0, 2.0, 1) plt.figure() CS = plt.contour(X, Y, Gauss) plt.show()
def main(): data = loadmat('../data/ex8data1.mat') X = data['X'] (mu, sigma2) = estimate_gaussian_params(X) print('mu: ' + str(mu)) print('variance: ' + str(sigma2)) # Plot dataset plt.scatter(X[:, 0], X[:, 1], marker='x') plt.axis('equal') plt.show() # Plot dataset and contour lines plt.scatter(X[:, 0], X[:, 1], marker='x') x = np.arange(0, 25, .025) y = np.arange(0, 25, .025) first_axis, second_axis = np.meshgrid(x, y) Z = mlab.bivariate_normal(first_axis, second_axis, np.sqrt(sigma2[0]), np.sqrt(sigma2[1]), mu[0], mu[1]) plt.contour(first_axis, second_axis, Z, 10, cmap=plt.cm.jet) plt.axis('equal') plt.show() # Load validation dataset Xval = data['Xval'] yval = data['yval'].flatten() stddev = np.sqrt(sigma2) pval = np.zeros((Xval.shape[0], Xval.shape[1])) pval[:, 0] = stats.norm.pdf(Xval[:, 0], mu[0], stddev[0]) pval[:, 1] = stats.norm.pdf(Xval[:, 1], mu[1], stddev[1]) print(np.prod(pval, axis=1).shape) epsilon, _ = select_epsilon(np.prod(pval, axis=1), yval) print('Best value found for epsilon: ' + str(epsilon)) # Computando a densidade de probabilidade # de cada um dos valores do dataset em # relacao a distribuicao gaussiana p = np.zeros((X.shape[0], X.shape[1])) p[:, 0] = stats.norm.pdf(X[:, 0], mu[0], stddev[0]) p[:, 1] = stats.norm.pdf(X[:, 1], mu[1], stddev[1]) # Apply model to detect abnormal examples in X anomalies = np.where(np.prod(p, axis=1) < epsilon) # Plot the dataset X again, this time highlighting the abnormal examples. plt.clf() plt.scatter(X[:, 0], X[:, 1], marker='x') plt.scatter(X[anomalies[0], 0], X[anomalies[0], 1], s=50, color='r', marker='x') plt.axis('equal') plt.show()
def test_image_plot(self): delta = 0.025 x = y = np.arange(-3.0, 3.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) Z = Z2 - Z1 # difference of Gaussians im = plt.imshow(Z, interpolation='bilinear', cmap=cm.RdYlGn, origin='lower', extent=[-3, 3, -3, 3], vmax=abs(Z).max(), vmin=-abs(Z).max()) plt.show() time.sleep(0.2) plt.close()
def filtered_text(ax): # mostly copied from contour_demo.py # prepare image delta = 0.025 x = np.arange(-3.0, 3.0, delta) y = np.arange(-2.0, 2.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) # draw im = ax.imshow(Z, interpolation='bilinear', origin='lower', cmap=cm.gray, extent=(-3,3,-2,2)) levels = np.arange(-1.2, 1.6, 0.2) CS = ax.contour(Z, levels, origin='lower', linewidths=2, extent=(-3,3,-2,2)) ax.set_aspect("auto") # contour label cl = ax.clabel(CS, levels[1::2], # label every second level inline=1, fmt='%1.1f', fontsize=11) # change clable color to black from matplotlib.patheffects import Normal for t in cl: t.set_color("k") # to force TextPath (i.e., same font in all backends) t.set_path_effects([Normal()]) # Add white glows to improve visibility of labels. white_glows = FilteredArtistList(cl, GrowFilter(3)) ax.add_artist(white_glows) white_glows.set_zorder(cl[0].get_zorder()-0.1) ax.xaxis.set_visible(False) ax.yaxis.set_visible(False)
def contourf_with_logscale(): import matplotlib.pyplot as plt import matplotlib.ticker as tkr #from matplotlib import colors, ticker from matplotlib.mlab import bivariate_normal N = 100 x = np.linspace(-3.0, 3.0, N) y = np.linspace(-2.0, 2.0, N) X, Y = np.meshgrid(x, y) # A low hump with a spike coming out of the top right. # Needs to have z/colour axis on a log scale so we see both hump and spike. # linear scale only shows the spike. z = bivariate_normal(X, Y, 0.1, 0.2, 1.0, 1.0) \ + 0.1 * bivariate_normal(X, Y, 1.0, 1.0, 0.0, 0.0) # Put in some negative values (lower left corner) to cause trouble with logs: z[:5, :5] = -1 # The following is not strictly essential, but it will eliminate # a warning. Comment it out to see the warning. z = np.ma.masked_where( z<=0, z ) # Automatic selection of levels works; setting the # log locator tells contourf to use a log scale: cs = plt.contourf( X, Y, z, locator = tkr.LogLocator() ) # Alternatively, you can manually set the levels # and the norm: #lev_exp = np.arange(np.floor(np.log10(z.min())-1), # np.ceil(np.log10(z.max())+1)) #levs = np.power(10, lev_exp) #cs = plt.contourf(X, Y, z, levs, norm=colors.LogNorm()) #The 'extend' kwarg does not work yet with a log scale. cbar = plt.colorbar() return 'contourf with logscale'
def demo(): delta = 0.025 x = y = np.arange(-3.0, 3.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) Z = Z2 - Z1 # difference of Gaussians my_extent = (-5, 5, -5, 5) im = plt.imshow(Z, interpolation='bilinear', cmap=cm.RdYlGn, origin='lower', extent=my_extent, vmax=abs(Z).max(), vmin=-abs(Z).max()) plt.show()
def visualize_contour(mean, cov, alpha=1): delta = 1.0 dots = 0.5 x = np.arange(mean[0] - cov[0][0] - delta, mean[0] + cov[0][0] + delta, dots) y = np.arange(mean[1] - cov[1][1] - delta, mean[1] + cov[1][1] + delta, dots) X, Y = np.meshgrid(x, y) Z = bivariate_normal(X, Y, cov[0][0] / 2, cov[1][1] / 2, mean[0], mean[1]) plt.contour(X, Y, Z, alpha=alpha)
def plotheatmap2(s, valmin, valmax, points=100): import numpy as np import matplotlib.cm as cm import matplotlib.mlab as mlab import matplotlib.pylab as plt x = range(1, len(s)) y = np.linspace(valmin, valmax, points) X, Y = np.meshgrid(x, y) Z = mlab.bivariate_normal(X, Y) * 0.0 for i in range(len(s)): Z1 = mlab.bivariate_normal(X, Y, 3.0, 3.0, i, s[i]) Z = Z1 + Z im = plt.imshow(Z, interpolation='bilinear', cmap=cm.gray, origin='lower') #extent=[-3,3,-3,3]) return Z
def get_heatmap_from_kp(cx, cy, w, h): x = np.linspace(1, w, w) y = np.linspace(1, h, h) X, Y = np.meshgrid(x, y) # 1.5 pixels from human keypoint annotation Z = ml.bivariate_normal(X, Y, 1.5, 1.5, cx, cy) if np.max(Z) != 0: Z = Z / np.max(Z) #im = Image.fromarray(np.uint8(Z)) return Z
def get_test_data(delta=0.05): from matplotlib.mlab import bivariate_normal x = y = np.arrange(4.0, -6.0, delta) X, Y = np.meshgrid(x,y) Z1 = bivariate_normal(X, Y ,6.0, 7.0, 8.0, 9.0) Z2 = bivariate_normal(X, Y ,2.0, 0.5 , 1 ,1) Z = Z2-Z1 X = X*100 Y = Y*100 Z = Z*600 return X, Y, Z fig = plt.figure() ax = fig.add_subplot(11, projection='3d') x,y,z = axes3d.get_test_data(0.05) ax.plot_wireframe(x,y,z, rstride=2 , cstride=2) plt.show()
def after_converge(mean, covar, cir_buffer, threshold): cnt = 0 mean_list1, illegal = [], [] plt.figure(1) plt.ion() plt.show() for i in range(count, len(vals_data)): #Calculate the gaussian value and if the value is greater than some threshold declare it as legal use it to update the gaussian #and if it is less than threshold declare it as illegal plt.clf() #start prediction val_func = vals_data[i] const = 1 / (linalg.det(covar)) const1 = matrix((val_func - mean)) const2 = const1 * linalg.inv(covar) * const1.T exp_term = ((-1 / 2) * const2) probab = exp(exp_term) #print probab if probab > threshold: if count >= 824: pass cir_buffer.append(val_func) vals_func = array(cir_buffer) mean = vals_func.mean(axis=0) mean_list.append(mean) mean_array = array(mean_list) #plt.subplot(211) X, Y = meshgrid(vals_func[:, 0], vals_func[:, 1]) z = bivariate_normal(X, Y, covar[0, 0], covar[1, 1], mean[0], mean[1], covar[0, 1]) plt.contour(X, Y, z) plt.scatter(vals_func[:, 0], vals_func[:, 1], 10, 'b', 'o') plt.scatter(mean[0], mean[1], 100, 'b', '^') if illegal == []: pass else: plt.scatter(illegal_array[:, 0], illegal_array[:, 1], 10, 'r', 'o') time.sleep(2) plt.draw() else: if count >= 824: pass cnt += 1 illegal.append(val_func) illegal_array = array(illegal) count += 1 print cnt
def test_mask_image_over_under(): delta = 0.025 x = y = np.arange(-3.0, 3.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) Z = 10 * (Z2 - Z1) # difference of Gaussians palette = copy(plt.cm.gray) palette.set_over('r', 1.0) palette.set_under('g', 1.0) palette.set_bad('b', 1.0) Zm = ma.masked_where(Z > 1.2, Z) fig, (ax1, ax2) = plt.subplots(1, 2) im = ax1.imshow(Zm, interpolation='bilinear', cmap=palette, norm=colors.Normalize(vmin=-1.0, vmax=1.0, clip=False), origin='lower', extent=[-3, 3, -3, 3]) ax1.set_title('Green=low, Red=high, Blue=bad') fig.colorbar(im, extend='both', orientation='horizontal', ax=ax1, aspect=10) im = ax2.imshow(Zm, interpolation='nearest', cmap=palette, norm=colors.BoundaryNorm([-1, -0.5, -0.2, 0, 0.2, 0.5, 1], ncolors=256, clip=False), origin='lower', extent=[-3, 3, -3, 3]) ax2.set_title('With BoundaryNorm') fig.colorbar(im, extend='both', spacing='proportional', orientation='horizontal', ax=ax2, aspect=10)
def position_estimation_bayes(data, nr_anchors, p_anchor, prior_mean, prior_cov, lambdas, p_true): """ estimate the position by accounting for prior knowledge that is specified by a bivariate Gaussian Input: data...distance measurements to unkown agent, nr_measurements x nr_anchors nr_anchors... scalar p_anchor... position of anchors, nr_anchors x 2 prior_mean... mean of the prior-distribution, 2x1 prior_cov... covariance of the prior-dist, 2x2 lambdas... estimated parameters (scenario 3), nr_anchors x 1 p_true... true position (needed to calculate error), 2x2 """ x = np.arange(-5, 5, .05) y = np.arange(-5, 5, .05) xx, yy = np.meshgrid(x, y) # construct a mesh for each anchor of the distance from the anchor -- to be used in determining likelihoods distances = [ np.sqrt((anchor[0] - xx)**2 + (anchor[1] - yy)**2) for anchor in p_anchor ] # construct a mesh of the gaussian pdf for the point p prior_pdf = mlab.bivariate_normal(xx, yy, np.sqrt(prior_cov[0][0]), np.sqrt(prior_cov[1][1]), prior_mean[0][0], prior_mean[0][1], prior_cov[0][1]) position_estimations = [] for d in data: likelihoods = [ np.where( d[i] > distances[i], lambdas[0][i] * np.exp(-lambdas[0][i] * (d[i] - distances[i])), 0) for i in range(nr_anchors) ] # multiply the likelihood mesh from each anchor together to obtain the joint likelihood joint_likelihood = functools.reduce(np.multiply, likelihoods) # bayesian_likelihood is p(r|p) * p(p) bayesian_likelihood = joint_likelihood * prior_pdf ml_index = unravel_index(bayesian_likelihood.argmax(), bayesian_likelihood.shape) p = (x[ml_index[1]], y[ml_index[0]]) position_estimations.append(p) p_est_x = [p[0] for p in position_estimations] p_est_y = [p[1] for p in position_estimations] plt.plot(p_est_x, p_est_y, 'bo') plt.xlabel('x') plt.ylabel('y') plt.title('Bayesian Estimation of the Target Point (2, -4)') pylab.xlim([-5, 5]) pylab.ylim([-5, 5]) plt.show()
def BivariateNormalPDFPlot(): # Number of points in each direction n = 40 # Parameters mu_1 = 0 mu_2 = 0 sigma_1 = 1 sigma_2 = 0.5 rho1 = 0.0 rho2 = -0.8 rho3 = 0.8 # Create a grid and a multivariate normal x = np.linspace(-3.0, 3.0, n) y = np.linspace(-3.0, 3.0, n) X, Y = np.meshgrid(x, y) Z = lambda rho: bivariate_normal(X, Y, sigma_1, sigma_2, mu_1, mu_2, rho * sigma_1 * sigma_2) # Make a 3D plot- rho = 0.0 fig = plt.figure(1) ax = fig.gca(projection='3d') ax.plot_surface(X, Y, Z(rho1), cmap='viridis', linewidth=0) ax.set_xlabel('X axis') ax.set_ylabel('Y axis') ax.set_zlabel('Z axis') plt.show() # Make a 3D plot- rho = -0.8 fig = plt.figure(2) ax = fig.gca(projection='3d') ax.plot_surface(X, Y, Z(rho2), cmap='viridis', linewidth=0) ax.set_xlabel('X axis') ax.set_ylabel('Y axis') ax.set_zlabel('Z axis') plt.show() # Make a 3D plot- rho = 0.8 fig = plt.figure(3) ax = fig.gca(projection='3d') ax.plot_surface(X, Y, Z(rho3), cmap='viridis', linewidth=0) ax.set_xlabel('X axis') ax.set_ylabel('Y axis') ax.set_zlabel('Z axis') plt.show()
def gaussian2D(imshape, x_mean, y_mean, sigma): # imshape = (width, height) # Set up the 2D Gaussian: delta = 1 x = np.arange(0, imshape[0], delta) y = np.arange(0, imshape[1], delta) X, Y = np.meshgrid(x, y) if not (x_mean <= 0 or x_mean >= imshape[0] or y_mean <= 0 or y_mean >= imshape[1]): Z = mlab.bivariate_normal(X, Y, sigma, sigma, x_mean, y_mean) else: Z = np.zeros((imshape[1], imshape[0])) return Z
def test_contour_xyz(self): _skip_if_no_matplotlib() import numpy as np import matplotlib.mlab as mlab delta = 0.025 x = np.arange(-3.0, 3.0, delta) y = np.arange(-2.0, 2.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) viewer = cesiumpy.Viewer() viewer.plot.contour(X, Y, Z) self.assertEqual(len(viewer.entities), 7) self.assertTrue( all(isinstance(x, cesiumpy.Polyline) for x in viewer.entities)) self.assertEqual(viewer.entities[0].material, cesiumpy.color.Color(0.0, 0.0, 0.5, 1.0))
def load_2DGM_slope(n_samples=100000, verbose=False): x1 = np.random.uniform(-1, 1, size=n_samples) x2 = np.random.uniform(-1, 1, size=n_samples) pi1 = ((mlab.bivariate_normal(x1, x2, 0.25, 0.25, -0.5, -0.2) + mlab.bivariate_normal(x1, x2, 0.25, 0.25, 0.7, 0.5)) / 2).clip(max=1) pi1 = pi1 * 0.5 + 0.5 * (0.5 * (x1 + 1) / 2 + 0.3 * (1 - x2) / 2) p = np.zeros(n_samples) h = np.zeros(n_samples) for i in range(n_samples): rnd = np.random.uniform() if rnd > pi1[i]: p[i] = np.random.uniform() h[i] = 0 else: p[i] = np.random.beta(a=0.3, b=4) h[i] = 1 X = np.concatenate([[x1], [x2]]).T X = (X + 1) / 2 if verbose: fig = plt.figure() ax1 = fig.add_subplot(121) x_grid = np.arange(-1, 1, 1 / 100.0) y_grid = np.arange(-1, 1, 1 / 100.0) X_grid, Y_grid = np.meshgrid(x_grid, y_grid) pi1_grid = ( (mlab.bivariate_normal(X_grid, Y_grid, 0.25, 0.25, -0.5, -0.2) + mlab.bivariate_normal(X_grid, Y_grid, 0.25, 0.25, 0.7, 0.5)) / 2).clip(max=1) * 0.5 + (0.5 * (0.5 * (X_grid + 1) / 2 + 0.3 * (1 - Y_grid) / 2)) ax1.pcolor(X_grid, Y_grid, pi1_grid) ax2 = fig.add_subplot(122) alt = ax2.scatter(x1[h == 1][1:50], x2[h == 1][1:50], color='r') nul = ax2.scatter(x1[h == 0][1:50], x2[h == 0][1:50], color='b') ax2.legend((alt, nul), ('50 alternatives', '50 nulls')) return p, h, X
def draw_EM(points, mean_0, cov_0, arr_log_likelihood, labels): #label setting for ploting newLabel = reassign_class_labels(labels) for label in np.nditer(labels, op_flags=['readwrite']): label[...] = newLabel[label] #print log likelihood function x = arr_log_likelihood.size plt.title("log likelihood function") plt.xlabel("iterations") plt.ylabel("log-likelihood") plt.scatter(np.arange(x), arr_log_likelihood) plt.show() xmin = 4 xmax = 8 ymin = 0 ymax = 8 nr_points = 50 # #pca # xmin = -4 # xmax = 4 # ymin = -2 # ymax = 2 #print gauss function for i in range(points.shape[0]): for j in range(mean_0.shape[1]): if labels[i] == j: plt.scatter(points[i, 0], points[i, 1], c='C{}'.format(j)) break c = 0 for k in range(0, mean_0.shape[1]): mu = mean_0[:, k] cov = cov_0[:, :, k] delta_x = float(xmax - xmin) / float(nr_points) delta_y = float(ymax - ymin) / float(nr_points) x = np.arange(xmin, xmax, delta_x) y = np.arange(ymin, ymax, delta_y) X, Y = np.meshgrid(x, y) Z = mlab.bivariate_normal(X, Y, np.sqrt(cov[0][0]), np.sqrt(cov[1][1]), mu[0], mu[1], cov[0][1]) plt.plot([mu[0]], [mu[1]], 'r+') # plot the mean as a single point CS = plt.contour(X, Y, Z) plt.clabel(CS, inline=1, fontsize=10) plt.show() plot_iris_data(points, labels)
def compute_grid_geo_probabilities(model_parameters: ModelParameters, scaler: StandardScaler, prob_granularity: float = 0.01, geo_prob_threshold: float = 0.2): """ Creates a mesh grid and computes the geographical probabilities for each point per each topic. Probabilities are weighted by topic probabilities (theta). :param geo_prob_threshold: points with mixture geo probability lower than this amount will be removed :param model_parameters: :param scaler: :param prob_granularity: :return: """ X, Y, X_lon, Y_lat = create_probability_grid(-5.0, 5.0, -5.0, 5.0, scaler, prob_granularity) theta = model_parameters.theta.flatten() # 3D matrix of k x N1 X N2 probs = np.zeros((model_parameters.num_topics, X.shape[0], X.shape[1])) for z in range(model_parameters.num_topics): center = model_parameters.topic_centers[z] cov = model_parameters.topic_covar[z] probs[z] = mlab.bivariate_normal(X, Y, np.sqrt(cov[0, 0]), np.sqrt(cov[1, 1]), center[0], center[1], cov[0, 1]) probs *= theta[:, np.newaxis, np.newaxis] # Keep only locations that are geographically likely disabled_locations = probs.sum(axis=0) < geo_prob_threshold badcols = np.all(disabled_locations, axis=0) badrows = np.all(disabled_locations, axis=1) firstcol = max(0, badcols.argmin() * 0.9) firstrow = max(0, badrows.argmin() * 0.9) lastcol = min(len(badcols), (len(badcols) - badcols[::-1].argmin()) * 1.1) lastrow = min(len(badrows), (len(badrows) - badrows[::-1].argmin()) * 1.1) probs = probs[:, firstrow:lastrow, firstcol:lastcol] X_lon = X_lon[firstrow:lastrow, firstcol:lastcol] Y_lat = Y_lat[firstrow:lastrow, firstcol:lastcol] # print("Shrinked number grid to {}x{} by removing points with p<{}.".format(X_lon.shape[1], X_lon.shape[0], # geo_prob_threshold)) return probs, X_lon, Y_lat, probs / theta[:, np.newaxis, np.newaxis]
def setUpClass(cls): traveltimes_dir = os.path.join(cls.data_dir, 'traveltimes') os.mkdir(cls.data_dir) os.mkdir(traveltimes_dir) if os.path.exists(cls.filename_out): os.remove(cls.filename_out) # remove file from any previous tests # taken from http://matplotlib.org/examples/pylab_examples/contour_demo.html figure = plt.figure() ax = figure.add_subplot(111) delta = 0.025 x = numpy.arange(-3.0, 3.0, delta) y = numpy.arange(-2.0, 2.0, delta) X, Y = numpy.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) Z = 10.0 * (Z2 - Z1) cls.levels = numpy.linspace(0, 100, num=10) cls.contour_plot = ax.contour(X, Y, Z, levels=cls.levels, cmap=plt.cm.jet)
def init_map(self,map_type,**kwargs): "Set the map type according to specified string" if map_type == 'identity': self._map = np.identity(min(self.dim.x,self.dim.y)) elif map_type == 'zero': pass elif map_type == 'random': self._map = np.random.rand(self.dim.x,self.dim.y) elif map_type == 'gaussian': self._map = self.pitch*self.pitch*mlab.bivariate_normal(self.X,self.Y, kwargs['covariance'], kwargs['covariance'], (self.dim.x-self.base.x)/2, (self.dim.y-self.base.y)/2)
def plot_2d_contours(ax, xfit, vfit): delta = 0.025 [_,d,T] = vfit.shape xlim = ax.get_xlim() ylim = ax.get_ylim() x = np.arange(xlim[0],xlim[1], delta) y = np.arange(ylim[0],ylim[1], delta) X, Y = np.meshgrid(x, y) for t in range(T): s2d = np.sqrt(vfit[0:2,0:2,t]) x0 = xfit[0,t] x1 = xfit[1,t] Z = mlab.bivariate_normal(X, Y,sigmax=s2d[0,0], sigmay=s2d[1,1], mux=x0, muy=x1, sigmaxy=s2d[0,1]) ax.contour(X, Y, Z, 3)
def _gaussian2d(stddev, truncate=4): """ Creates a 2-d gaussian kernel truncated at 4 standard deviations (8 in total). :param (float, float) stddev: Standard deviations in y and x. :param float truncate: Number of stddevs at each side of the kernel. ..note:: Kernel sizes will always be odd. """ from matplotlib import mlab half_kernel = np.round(stddev * truncate) # kernel_size = 2 * half_kernel + 1 y, x = np.meshgrid(np.arange(-half_kernel[0], half_kernel[0] + 1), np.arange(-half_kernel[1], half_kernel[1] + 1)) kernel = mlab.bivariate_normal(x, y, sigmay=stddev[0], sigmax=stddev[1]) return kernel
def bivariate_normal(): fig = plt.figure() #add a 3d subplot ax = fig.add_subplot(111, projection='3d') #set X,Y,Z x = np.linspace(-10, 10, 200) y = x X, Y = np.meshgrid(x, y) #create bivariate gaussian distribution for equal shape X,Y Z = mlab.bivariate_normal(X, Y, 1, 5, 0, -5, 0) #create surface ax.plot_surface(X, Y, Z, cmap='OrRd') plt.show()
def plotTest003004(): delta = 0.025 x = y = np.arange(-3.0, 3.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) Z = Z2 - Z1 # difference of Gaussians path = Path([[0, 1], [1, 0], [0, -1], [-1, 0], [0, 1]]) patch = PathPatch(path, facecolor='none') fig, ax = plt.subplots() ax.add_patch(patch) im = ax.imshow(Z, interpolation='bilinear', cmap=cm.gray, origin='lower', extent=[-3, 3, -3, 3], clip_path=patch, clip_on=True) im.set_clip_path(patch) plt.show()
def draw_data(ax, mu, cov): """ 将正太分布的数据可视化 """ data = generate_data(150, mu, cov) ax.scatter(data[:, 0], data[:, 1]) x = np.arange(-10, 10, 0.1) X, Y = np.meshgrid(x, x) Z = bivariate_normal(X, Y, cov[0, 0], cov[1, 1], mu[0], mu[1], cov[0, 1]) ax.contour(X, Y, Z) ax.set_xlim([-10, 10]) ax.set_ylim([-10, 10]) ax.get_yaxis().set_visible(False) ax.get_xaxis().set_visible(False)
def plot_gmm_contour(self, stat1, stat2): fig = plt.figure() legendlocations = {x: [] for x in self.scenarios} for scenario in self.scenarios: G = self.gmm_fit(stat1, stat2, scenario) mu = G.means_ sigma = G.covars_ legendlocations[scenario] = [ mu[0][0] + .2 * sigma[0][0][0], mu[0][1] + .2 * sigma[0][1][1] ] w = G.weights_ minscore1 = min(self.minscores[self.stat2num[stat1]]) maxscore1 = max(self.maxscores[self.stat2num[stat1]]) minscore2 = min(self.minscores[self.stat2num[stat2]]) maxscore2 = max(self.maxscores[self.stat2num[stat2]]) x = np.linspace(minscore1, maxscore1, 100) y = np.linspace(minscore2, maxscore2, 100) X, Y = np.meshgrid(x, y) Z = 0 for i in range(len(w)): Z = Z + w[i] * bivariate_normal( X, Y, mux=mu[i][0], muy=mu[i][1], sigmax=math.sqrt(sigma[i][0][0]), sigmay=math.sqrt(sigma[i][1][1]), sigmaxy=sigma[i][0][1]) C = plt.contour( X, Y, Z, 10, cmap=self.colorspectra[self.scenarios.index(scenario) % len(self.colorspectra)]) plt.xlabel(stat1) plt.ylabel(stat2) for scenario in self.scenarios: plt.text(legendlocations[scenario][0], legendlocations[scenario][1], scenario, color=self.colors[self.scenarios.index(scenario) % len(self.colors)]) plt.savefig(self.path2files + 'component_statistic_distributions/joints/' + stat1 + '_' + stat2 + '_joint.pdf') plt.clf()