示例#1
0
    def vecPlot2(self, row, col, r_m, mWx, mWy, data1, data2, dmr, frmSize, winSize):
        pl.clf()
        pl.ion()
        pWx = []
        l = dmr - row - col
        for f in range(l):
            pWx.append(mWx[row+f, col+f,:,0])
        pWx = np.array(pWx)

        sqWx = np.sqrt(pWx * pWx)
        print sqWx
        r,c = sqWx.shape
        x = pl.arange(c+1)
        y = pl.arange(r+1)
        X, Y = pl.meshgrid(x, y)

        pl.subplot2grid((1,2),(0,0))
        pl.pcolor(X, Y, sqWx, vmin=0, vmax=1)
        pl.xlim(0,c)
        pl.ylim(0,r)
        pl.colorbar()
        pl.title("user_1 (t:"+str(row)+")")
        pl.gray()

        pWy = []
        l = dmr - row - col
        for f in range(l):
            pWy.append(mWy[row+f, col+f,:,0])
        pWy = np.array(pWy)

        sqWy = np.sqrt(pWy * pWy)
        print sqWy
        r,c = sqWy.shape
        x = pl.arange(c+1)
        y = pl.arange(r+1)
        X, Y = pl.meshgrid(x, y)

        pl.subplot2grid((1,2),(0,1))
        pl.pcolor(X, Y, sqWy, vmin=0, vmax=1)
        pl.xlim(0,c)
        pl.ylim(0,r)
        pl.colorbar()
        pl.title("user_2 (t:"+str(col)+")")
        pl.gray()

        pl.tight_layout()
        pl.draw()
示例#2
0
def neh_contourplot(dictionary, X_key, Y_key, Z_key, color_lims, color_map, xlabel='xlabel', ylabel='ylabel', title='title', size=[10, 7], sub=0):
    # Don't generate a new figure if it's being called from neh_subplot
    if sub != 1: fig, ax = plt.subplots(figsize=size)
    #     fig = plt.figure()
    #     ax = fig.add_subplot(111)

    X, Y = plt.meshgrid(dictionary[X_key], dictionary[Y_key])
    # Check that the vector lengths conform to the plot matrix, and if not try to reverse the transpose
    Z = dictionary[Z_key]
    if Z.shape != X.shape:
        Z = plt.transpose(Z)
    # pdb.set_trace()
    plt.pcolor(X, Y, Z, vmin=color_lims[0], vmax=color_lims[1], cmap=color_map, norm=MidpointNormalize(midpoint=0))
    cbar = plt.colorbar()
    cbar.ax.tick_params(labelsize=14)

    # Axis tick font (rotate the x ticks if they are dates - too long to be horizontal)
    plt.setp(ax.get_xticklabels(), rotation='horizontal', fontsize=10)
    plt.setp(ax.get_yticklabels(), rotation='horizontal', fontsize=10)

    # Axis labels
    if xlabel != '': plt.xlabel(xlabel, fontsize=12, fontweight='bold')
    if ylabel != '': plt.ylabel(ylabel, fontsize=12, fontweight='bold')

    # Plot title (smaller if part of a subplot)
    plt.title(title, fontsize=14 if sub != 1 else 12, fontweight='bold')

    # Don't show the figure if it's being called from neh_subplot
    if sub != 1: plt.show()


# class MidpointNormalize(colors.Normalize):
#     def __init__(self, vmin=None, vmax=None, midpoint=None, clip=False):
#         self.midpoint = midpoint
#         colors.Normalize.__init__(self, vmin, vmax, clip)
#
#     def __call__(self, value, clip=None):
#         # I'm ignoring masked values and all kinds of edge cases to make a
#         # simple example...
#         x, y = [self.vmin, self.midpoint, self.vmax], [0, 0.5, 1]
#         return nplt.ma.masked_array(nplt.interp(value, x, y))
示例#3
0
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
alpha = 0.7
phi_ext = 2 * np.pi * 0.5

def flux_qubit_potential(phi_m, phi_p):
    return 2 + alpha - 2 * cos(phi_p)*cos(phi_m) - alpha * cos(phi_ext - 2*phi_p)
phi_m = np.linspace(0, 2*np.pi, 100)
phi_p = np.linspace(0, 2*np.pi, 100)
X,Y = plt.meshgrid(phi_p, phi_m)
Z = flux_qubit_potential(X, Y).T
fig, ax = subplots()

p = ax.pcolor(X/(2*np.pi), Y/(2*np.pi), Z, cmap=cm.RdBu, vmin=abs(Z).min(), vmax=abs(Z).max())
cb = fig.colorbar(p)
示例#4
0
    def drawPlot(self, row, col, mWx, mWy, ccaMat, data1, data2, dataMaxRange, dataDimen, winSize):
        #def drawPlot(self, row, col):
        print "draw:"+str(row)+", "+str(col)

        #test
        pWx = mWx[row,col:,0,:]
        print "pWx:",pWx
        print "shape:",pWx.shape

        #データの取得
        U1 = []
        U2 = []

        for w in range(winSize):
            U1.append(data1[row+w])
            U2.append(data2[col+w])

        nU1 = CCA().stdn(U1)
        nU2 = CCA().stdn(U2)

        #pl.clf()
        pl.ion()

        Wx = mWx[row][col]
        Wy = mWy[row][col]

        X, Y = pl.meshgrid(pl.arange(dataDimen+1), pl.arange(dataDimen+1))
       
        strCorU1s = []
        strCorU2s = []
        for i in range(dataDimen):
            fU1 = np.dot(nU1.T, Wx[:,i:i+1]).T
            fU2 = np.dot(nU2.T, Wy[:,i:i+1]).T
            strU1 = np.corrcoef(fU1, nU1)
            strU2 = np.corrcoef(fU2, nU2)  
            strCorU1 = np.squeeze(np.asarray(strU1[0:1,1:]))
            strCorU2 = np.squeeze(np.asarray(strU2[0:1,1:]))
            strCorU1s.append(strCorU1)
            strCorU2s.append(strCorU2)
            
            #print np.dot(Wx[:,i:i+1].T,Wx[:,i:i+1]).mean()

        sWx = np.array(np.matrix(strCorU1s))
        sWy = np.array(np.matrix(strCorU2s))
        
        #print Wx.shape
        #print Wx
        #print sWx.shape
        #print sWx

        pl.subplot2grid((3,2),(0,0),colspan=2)
        pl.xlim(0,dataDimen)
        pl.ylim(0,1)
        pl.xticks(fontsize=10)
        pl.yticks(fontsize=10)
        pl.plot(ccaMat[row][col])
        pl.title("eigen value (user 1:"+str(row)+", user 2:"+str(col)+")",fontsize=11)

        pl.subplot2grid((3,2),(1,0))
        pl.pcolor(X, Y, sWx)
        pl.gca().set_aspect('equal')
        pl.colorbar()
        pl.gray()
        pl.xticks(fontsize=10)
        pl.yticks(fontsize=10)
        pl.title("user 1:"+str(row),fontsize=11)

        pl.subplot2grid((3,2),(1,1))
        pl.pcolor(X, Y, sWy)
        pl.gca().set_aspect('equal')
        pl.colorbar()
        pl.gray()
        pl.xticks(fontsize=10)
        pl.yticks(fontsize=10)
        pl.title("user 2:"+str(col),fontsize=11)

        x = np.linspace(0, winSize-1, winSize)

        #forで回して第三位の正準相関までとる?
        pl.subplot2grid((3,2),(2,0),colspan=2)
        ls = ["-","--","-."]
        rhos = ""
        order = 3
        for i in range(order):
            fU1 = np.dot(nU1.T, Wx[:,i:i+1]).T
            fU2 = np.dot(nU2.T, Wy[:,i:i+1]).T

            fU1 = np.squeeze(np.asarray(fU1))
            fU2 = np.squeeze(np.asarray(fU2))

            rho = round(ccaMat[row][col][i],5)

            pl.plot(x, fU1, label="user1:"+str(rho), linestyle=ls[i])
            pl.plot(x, fU2, label="user2:"+str(rho), linestyle=ls[i])
            rhos += str(rho)+", "

        leg = pl.legend(prop={'size':9})
        leg.get_frame().set_alpha(0.7)
        pl.xticks(fontsize=10)
        pl.yticks(fontsize=10)
        pl.title("canonical variate (eig val:"+rhos.rstrip(", ")+")",fontsize=11)

        pl.tight_layout()
        pl.draw()
示例#5
0
    def vecPlot(self, row, col, r_m, mWx, mWy, data1, data2, frmSize, winSize):
        pl.clf()
        pl.ion()

        #pWy = mWy[row][col]
        #まずはx方向のWx
        pWx = mWx[row,col:(frmSize-winSize+1)+row,:,0]

        #print np.mean(pWx*pWx, axis=0)
        sqWx = np.sqrt(pWx * pWx)/1.1
        print sqWx
        r,c = sqWx.shape
        x = pl.arange(c+1)
        y = pl.arange(r+1)
        X, Y = pl.meshgrid(x, y)

        pl.subplot2grid((2,2),(0,0))
        pl.pcolor(X, Y, sqWx, vmin=0, vmax=1)
        pl.xlim(0,c)
        pl.ylim(0,r)
        pl.colorbar()
        pl.title("user_1 (t:"+str(row)+")")
        pl.gray()

        
        pWy = mWy[row,col:(frmSize-winSize+1)+row,:,0]
        #print "pWy sum:",np.sum(pWy[0,:],axis=1)
        #print np.sum(pWy*pWy,axis=1)
        #print np.mean(pWy*pWy, axis=0)
        sqWy = np.sqrt(pWy * pWy)
        r,c = sqWx.shape
        x = pl.arange(c+1)
        y = pl.arange(r+1)
        X, Y = pl.meshgrid(x, y)

        pl.subplot2grid((2,2),(0,1))
        pl.pcolor(X, Y, sqWy,vmin=0, vmax=1)    
        pl.xlim(0,c)
        pl.ylim(0,r)
        pl.colorbar()
        pl.title("user_2 (t:"+str(col)+")")
        pl.gray()

        """
        pl.subplot2grid((2,2),(1,0),colspan=2)
        pr, pc = pWx.shape
        for i in range(pc):
            pl.plot(pWx[:,i], color="r")
        """
        
        
        xl = np.arange(c)        
        pl.subplot2grid((2,2),(1,0))
        #subx1 = np.delete(sqWx,0,0)
        #subx2 = np.delete(sqWx,r-1,0)
        #print "sum sub:",sum(subx2-subx1)
        #pl.bar(xl, np.fabs(np.mean(subx2-subx1,axis=0)))
        pl.bar(xl, np.mean(sqWx, axis=0))
        pl.xlim(0,c)
        pl.ylim(0,1)

        pl.subplot2grid((2,2),(1,1))
        #subx1 = np.delete(sqWy,0,0)
        #subx2 = np.delete(sqWy,r-1,0)
        #pl.bar(xl, np.fabs(np.mean(subx2-subx1,axis=0)))
        pl.bar(xl, np.mean(sqWy, axis=0))
        pl.xlim(0,c)
        pl.ylim(0,1)

        """
        pl.subplot2grid((2,2),(1,0),colspan=2)
        U1 = []
        U2 = []
        od = 0
        for w in range(winSize):
            U1.append(data1[row+w][od])
            U2.append(data2[col+w][od])
        pl.plot(U1, color="r")
        pl.plot(U2, color="b")        
        """
        """
        #正準相関変量の表示
        U1 = []
        U2 = []
        for w in range(winSize):
            U1.append(data1[row+w])
            U2.append(data2[col+w])
        U1 = np.array(U1)
        U2 = np.array(U2)
        U1 = U1 - U1.mean(axis=0)
        U2 = U2 - U2.mean(axis=0)

        print "u1 s:",U1.shape
        print "wx s:",mWx[row,col,0,:].shape
    
        ls = ["-","--","-."]
        rhos = ""
        order = 3
        xl = np.linspace(0, winSize-1, winSize)
        for i in range(order):

            fU1 = np.dot(U1, mWx[row,col,i,:]).T
            fU2 = np.dot(U2, mWy[row,col,i,:]).T

            fU1 = np.squeeze(np.asarray(fU1))
            fU2 = np.squeeze(np.asarray(fU2))

            rho = round(r_m[row][col][i],5)

            pl.plot(xl, fU1, label="user1:"+str(rho), linestyle=ls[i])
            pl.plot(xl, fU2, label="user2:"+str(rho), linestyle=ls[i])
            rhos += str(rho)+", "

        leg = pl.legend(prop={'size':9})
        leg.get_frame().set_alpha(0.7)
        pl.title("canonical variate (eig val:"+rhos.rstrip(", ")+")",fontsize=11)
        """
        pl.xticks(fontsize=10)
        pl.yticks(fontsize=10)

        pl.tight_layout()

        pl.draw()
def Lineas_Campo():

    os.system("cls")

    def Phi(r, rp, q):
        phi = 1 / (4 * np.pi * eps0) * q / np.linalg.norm(r - rp)
        return phi

    def Campo_Electrico(r, rp, q):
        E = 1 / (4 * np.pi * eps0) * q * (r - rp) / np.linalg.norm(r - rp)**3
        return E

    eps0 = 8.85418781762e-12
    Nres = 25
    Xarray = np.linspace(0, 10, Nres)
    Yarray = np.linspace(0, 10, Nres)
    X, Y = plt.meshgrid(Xarray, Yarray)

    q1 = float(input("Ingrese el el Valor de la Carga 1 : "))
    rp1 = np.array([4, 4])
    phi1 = np.zeros((Nres, Nres))
    E1x = np.ones((Nres, Nres))
    E1y = np.ones((Nres, Nres))

    for i in range(Nres):

        for j in range(Nres):

            r = np.array([Xarray[i], Yarray[j]])
            phi1[i, j] = Phi(r, rp1, q1)
            E = Campo_Electrico(r, rp1, q1)
            E1x[i, j], E1y[i, j] = E / np.linalg.norm(E)

    q2 = float(input("\nIngrese el el Valor de la Carga 2 : "))
    os.system("cls")
    rp2 = np.array([6, 6])
    phi2 = np.zeros((Nres, Nres))
    E2x = np.ones((Nres, Nres))
    E2y = np.ones((Nres, Nres))

    for i in range(Nres):

        for j in range(Nres):
            r = np.array([Xarray[i], Yarray[j]])
            phi2[i, j] = Phi(r, rp2, q2)
            E = Campo_Electrico(r, rp2, q2)
            E2x[i, j], E2y[i, j] = E / np.linalg.norm(E)

    os.system("cls")

    phi_tot = phi1 + phi2
    Ex_tot = np.ones((Nres, Nres))
    Ey_tot = np.ones((Nres, Nres))
    for i in range(Nres):

        for j in range(Nres):

            E = np.array( [E1x[i,j] + E2x[i,j], \
            E1y[i,j] + E2y[i,j]] )
            E = E / np.linalg.norm(E)
            Ex_tot[i, j], Ey_tot[i, j] = E

    os.system("cls")

    plt.contour(X, Y, phi_tot, 100)
    plt.quiver(X, Y, Ey_tot, Ex_tot)

    plt.xlim((0, 10))
    plt.ylim((0, 10))
    plt.legend()
    plt.title('Lineas de campo y Equipotenciales de Cargas Puntuales\n')
    os.system("cls")
    plt.show()