2016-11-06 13 views
0

私はMatplolibを使って3Dサーフェスを視覚化しようとしています。残念ながらmaplotlibの3Dプロット

#!/usr/bin/env python 
from mpl_toolkits.mplot3d import Axes3D 
from matplotlib import cm 
from matplotlib.ticker import LinearLocator 
import matplotlib.pyplot as plt 
import numpy as np 
import pandas as pd 


FRAME = pd.read_csv('RESULTS.csv') 
fig = plt.figure() 
ax = fig.gca(projection='3d') 
X = np.arange(-4, 4, 1.0) 
xlen = len(X) 
Y = np.arange(-4, 4, 1.0) 
ylen = len(Y) 
X, Y = np.meshgrid(X, Y) 
Z = np.array(FRAME['Energy']) 

colortuple = ('y', 'b') 
colors = np.empty(X.shape, dtype=str) 
for y in range(ylen): 
    for x in range(xlen): 
     colors[x, y] = colortuple[(x + y) % len(colortuple)] 

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, facecolors=colors, 
         linewidth=0, antialiased=False) 

ax.set_zlim3d(-1, 1) 
ax.w_zaxis.set_major_locator(LinearLocator(6)) 

plt.show() 

それは期待した方法では動作しませんし、見返りに、次のエラーを取得しています:私は、スクリプトを書いて、その後

T1,T2,Energy 
0,0.0,0.0,0.0 
1,0.0,-1.0,0.02326751 
2,0.0,1.0,0.0232677 
3,0.0,-2.0,0.09329646 
4,0.0,2.0,0.0932964 
5,0.0,-3.0,0.20991799 
6,0.0,3.0,0.2099179 
7,0.0,-4.0,0.37299244 
8,0.0,4.0,0.37299269 
9,0.0,-5.0,0.58232459 
10,0.0,5.0,0.58232482 
11,0.0,-6.0,0.83765862 
12,0.0,6.0,0.83765867 
13,-1.0,0.0,0.02297095 
14,1.0,0.0,0.02297112 
15,-1.0,-1.0,0.09457491 
16,-1.0,1.0,-0.00195612 
17,1.0,-1.0,-0.00195583 
18,1.0,1.0,0.09457513 
19,-1.0,-2.0,0.21270257 
20,-1.0,2.0,0.01988884 
21,1.0,-2.0,0.01988887 
22,1.0,2.0,0.21270261 
23,-1.0,-3.0,0.37714697 
24,-1.0,3.0,0.08936601 
25,1.0,-3.0,0.08936597 
26,1.0,3.0,0.37714726 
27,-1.0,-4.0,0.58764634 
28,-1.0,4.0,0.20399978 
29,1.0,-4.0,0.20399997 
30,1.0,4.0,0.58764618 

: 私は結果とRESULTS.csvファイルを得ました:

File "./test.py", line 27, in <module> 
    linewidth=0, antialiased=False) 
    File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mpl_toolkits/mplot3d/axes3d.py", line 1586, in plot_surface 
    X, Y, Z = np.broadcast_arrays(X, Y, Z) 
    File "/usr/local/lib/python2.7/site-packages/numpy/lib/stride_tricks.py", line 191, in broadcast_arrays 
    shape = _broadcast_shape(*args) 
    File "/usr/local/lib/python2.7/site-packages/numpy/lib/stride_tricks.py", line 126, in _broadcast_shape 
    b = np.broadcast(*args[:32]) 
ValueError: shape mismatch: objects cannot be broadcast to a single shape 

このエラーを解決する方法を教えてください。

答えて

1

なぜこのバギーの「pandas」モジュールが必要ですか?私は誰かがnumpyのと一緒に「パンダ」の操作を実行し、不条理なエラーが表示されmatplotlibの参照

毎回..あなたがxlrdで、従来あなたの.csvファイルを読み込み、numpyのに各セルの値を追加する場合

アレイ、およびすべての3列 "X"、 "Y" 及び "Z" は

はmatplotlibの例を見ることをプロット問題がない、同じ長さを有する http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html

from mpl_toolkits.mplot3d import Axes3D 
from matplotlib import cm 
from matplotlib.ticker import LinearLocator, FormatStrFormatter 
import matplotlib.pyplot as plt 
import numpy as np 

fig = plt.figure() 
ax = fig.gca(projection='3d') 
X = np.arange(-5, 5, 0.25) 
Y = np.arange(-5, 5, 0.25) 
X, Y = np.meshgrid(X, Y) 
R = np.sqrt(X**2 + Y**2) 
Z = np.sin(R) 

print len(X) 
print len(Y) 
print len(Z) #all 3 arrays must have same length! 

surf = ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap=cm.coolwarm, 
         linewidth=0, antialiased=False) 
ax.set_zlim(-1.01, 1.01) 

ax.zaxis.set_major_locator(LinearLocator(10)) 
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) 

fig.colorbar(surf, shrink=0.5, aspect=5) 

plt.show()