以下のコードの結果を3次元プロットしようと思いまして、ここでx軸がx座標,y座標を時間、z軸を確率とします。
python
1import numpy as np 2import matplotlib.pyplot as plt 3import math 4from mpl_toolkits.mplot3d import Axes3D 5import matplotlib.animation as animation 6import matplotlib.colors as colors 7from matplotlib import cm 8 9#環境設定 10n=3 #x軸 11m=2*n #t 12theta = 3*(math.pi)/12 13 14P = [[np.cos(theta),np.sin(theta)],[0,0]] 15Q = [[0,0],[np.sin(theta),-np.cos(theta)]] 16x_list=[]#xline 17t_list=[]#time 18p_list=[]#probability 19s_list=[]#state 20a = 1/math.sqrt(2) 21b = 1j/math.sqrt(2) 22p_map=np.zeros([m,m]) 23#ランダムウォーク 24R=1/2 25L=1/2 26X_list=[] 27P_list=[] 28#step_list=[] 29 30#quantumwalk 31for j in range(0,2*n+1): 32 if j == n: 33 phai = [a ,b] 34 pro = 1 35 else: 36 phai = [0,0] 37 pro =0 38 p = np.dot(phai,np.conj(phai)) 39 40 x_list.append(j) 41 X_list.append(j) 42 s_list.append(phai) 43 p_list.append(p) 44 P_list.append(pro) 45 46 47 48for t in range(0,m+1): 49 t_list.append(t) 50 if t ==0: 51 s_list 52 p_list 53 P_list 54 else: 55 next_s_list = [0]*len(s_list) 56 next_P_list = [0]*len(P_list) #listと同じ要素の数ですべて0を用意(初期化) 57 for i in range(0,2*n+1): 58 if i == 0: 59 next_s_list[i] = np.dot(P, s_list[i+1]) 60 next_P_list[i] = P_list[i+1]*L 61 elif i == 2*n: 62 next_s_list[i] = np.dot(Q, s_list[i-1]) 63 next_P_list[i] = P_list[i-1]*R 64 else: 65 next_s_list[i] = np.dot(P, s_list[i+1]) + np.dot(Q, s_list[i-1]) 66 next_P_list[i] = P_list[i+1]*L + P_list[i-1]*R 67 68 p_list[i] = np.dot(next_s_list[i],np.conj(next_s_list[i])) 69 70 71 s_list = next_s_list 72 P_list = next_P_list 73 74 p_map =np.array(p_list) 75 print(p_map) 76 77 #plt.xlabel("x") 78 #plt.ylabel("probability") 79 #plt.ylim([0,0.1]) 80 #plt.xlim([-n,3*n]) 81 #plt.plot( x_list,np.real(p_list),color="red",linewidth=1.0,label="quantum walk") 82 #plt.plot(X_list, P_list,color="blue",linewidth=1.0,label="random walk") 83 #plt.legend(loc="best") 84 #plt.pause(0.01) 85 #plt.cla() 86 87 88 89#3次元 90fig = plt.figure() 91ax = Axes3D(fig) 92#X,Y = np.meshgrid(x_list, t_list) 93 94ax.set_xlabel("x") 95ax.set_ylabel("t") 96ax.set_zlabel("probability") 97 98ax.set_xlim(2*n,0) 99ax.set_ylim(0,m) 100ax.set_zlim(0,1) 101surf = ax.plot_surface(x_list, t_list, p_map, cmap =cm.coolwarm , linewidth=0) 102 103fig.colorbar(surf) 104plt.show() 105
これだけだと、わかりにくいので、このコードの結果
t=0 p_list=[0.+0.j 0.+0.j 0.+0.j 1.+0.j 0.+0.j 0.+0.j 0.+0.j] t=1 p_list=[0. +0.j 0. +0.j 0.5+0.j 0. +0.j 0.5+0.j 0. +0.j 0. +0.j] t=2 p_list=[0. +0.j 0.25+0.j 0. +0.j 0.5 +0.j 0. +0.j 0.25+0.j 0. +0.j] t=3 p_list=[0.125+0.j 0. +0.j 0.375+0.j 0. +0.j 0.375+0.j 0. +0.j 0.125+0.j] t=4 p_list=[0. +0.j 0.375+0.j 0. +0.j 0.125+0.j 0. +0.j 0.375+0.j 0. +0.j] t=5 p_list=[0.3125+0.j 0. +0.j 0.125 +0.j 0. +0.j 0.125 +0.j 0. +0.j 0.3125+0.j] t=6 p_list=[0. +0.j 0.21875+0.j 0. +0.j 0.125 +0.j 0. +0.j 0.21875+0.j 0. +0.j]
を3次元プロットにしたいということです。
tをy軸、listの各位置をx、各中身の値をz値としてです。
ただ、今,p_listは1次元配列なので、3次元プロットする為に、
p_map=np.array(p_list)
として、2次元配列に直して、プロットしようとしたところ、
ValueError: Argument Z must be 2-dimensional.
とでました。
これは、p_mapが2次元配列になっていないという意味だと思いました。
仮にそうだとしたら、np.zeros([m.m])を持ってきて一つ一つ代入するのも面倒くさそうなので、もう少し手軽なやり方はありますでしょうか?
回答1件
あなたの回答
tips
プレビュー