前提・実現したいこと
Pythonを用いて3次元のCSVファイルを、指定したxまたはyまたはzの値のときの数値をreshapeを用いて3Dグラフにプロットしたいです。xやyの値を指定したときは動作しグラフが表示されるのですがzを指定したときはエラーが発生してしまい表示されません。Pythonは初めて使用したのでreshapeの使い方が間違っているかもしれませんが教えて頂ければと思います。
発生している問題・エラーメッセージ
Traceback (most recent call last): File "C:\Users\Z.py", line 18, in <module> gridX = dataYZ[:,0].reshape((42,-1)) ValueError: cannot reshape array of size 2519 into shape (20,newaxis)
該当のソースコード
Python
1import numpy as np 2import matplotlib.pyplot as plt 3from mpl_toolkits.mplot3d import Axes3D 4import shutil 5 6Z = 0 7N = 450 # N.csvまで保存 8 9fig = plt.figure() 10ax = fig.add_subplot(111, projection='3d') 11ax.set_title("title") 12 13for i in range(400,N+1): 14 filename = 'C:csv/{}.csv'.format(i) 15 data = np.loadtxt(filename,delimiter=',') 16 dataYZ = data[data[:,2]==Z,:] 17 gridX = dataYZ[:,0].reshape((42,-1)) 18 gridY = dataYZ[:,1].reshape((42,-1)) 19 out = dataYZ[:,3].reshape((42,-1)) 20 ax.view_init(elev=90,azim=270) 21 22 ax.auto_scale_xyz([-0.04,0.04],[-0.05,0.05],[-0.16,0.04]) 23 surf = ax.plot_surface(gridX.astype(int), gridY.astype(int), out, cmap='bwr', linewidth=0) 24 #surf.set_clim(0,1.2) #ラベルの固定範囲 25 cb = fig.colorbar(surf) 26 plt.savefig('C:/Users/Desktop/test/test{}.png'.format(i),transparent=True) # fig保存 27 plt.pause(.1) 28 cb.remove() 29 surf.remove() 30
正常に動作したx,yを指定したコード
Xを指定したものについては
dataYZ = data[data[:,0]==X,:] #[行,列] gridY = dataYZ[:,1].reshape((42,-1)) gridZ = dataYZ[:,2].reshape((42,-1)) out = dataYZ[:,3].reshape((42,-1)) ax.view_init(elev=90,azim=270) ax.auto_scale_xyz([-0.04,0.04],[-0.05,0.05],[-0.16,0.04]) surf = ax.plot_surface(gridY.astype(int), gridZ.astype(int), out, cmap='bwr', linewidth=0)
Yを指定したものについては
dataYZ = data[data[:,1]==Y,:] #[行,列] gridX = dataYZ[:,0].reshape((42,-1)) gridZ = dataYZ[:,2].reshape((42,-1)) out = dataYZ[:,3].reshape((42,-1)) ax.view_init(elev=90,azim=270) #plt.xticks(np.arange(0,37,5)) #plt.yticks(np.arange(0,20,5)) ax.auto_scale_xyz([-0.7,1.5],[-1.4,1.4],[-0.5,0.5]) surf = ax.plot_surface(gridX.astype(int), gridZ.astype(int), out, cmap='bwr', linewidth=0)
で正常動作しています。
補足情報(FW/ツールのバージョンなど)
読み込みCSVファイルは下記のように左からx,y,z,データのようにカンマ区切りになったものです。今回テスト用に用いたデータは43×61×21です。20×20×20のような正方行列では正常に動作しました。
0,0,0,0.000e+00
0,0,1,0.000e+00
0,0,2,0.000e+00
0,0,3,0.000e+00
0,0,4,0.000e+00
0,0,5,0.000e+00
0,0,6,0.000e+00
0,0,7,0.000e+00
0,0,8,0.000e+00
0,0,9,0.000e+00
0,0,10,0.000e+00
0,0,11,0.000e+00
0,0,12,0.000e+00
0,0,13,0.000e+00
0,0,14,0.000e+00
0,0,15,0.000e+00
0,0,16,0.000e+00
0,0,17,0.000e+00
0,0,18,0.000e+00
0,0,19,0.000e+00
0,0,20,0.000e+00
0,1,0,0.000e+00
0,1,1,0.000e+00
0,1,2,0.000e+00
(以下略)
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。