2017-04-26 96 views
1

我正在使用matplotlib繪製一個3D直方圖,其中包含角度輻射角的正弦和餘弦值的線框。該地塊形成圓形,因爲它應該:Matplotlib三維繪圖忽略不在圈中的值

enter image description here

現在我想只是有圓形和山丘繪製,但不是在圈和周圍的值。 我試圖與

hist[hist==0] = np.nan 

駁回每個值是零,但後來我的情節是這樣的,其中在圈內也有一些值均爲零,因此線框情節不「觸地」了。

enter image description here

那麼,有沒有辦法解僱在圈內和周圍但情節仍下降到零,一路值是多少?

這是我的代碼:

from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt 
import numpy as np 

fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 

cos, sin = np.genfromtxt('out_dhdrls_test0123.csv', delimiter=' ').transpose() 

hist, xedges, yedges = np.histogram2d(cos, sin, bins=50, range=[[-1, 1], [-1, 1]]) 
hist[hist==0] = np.nan 
xpos, ypos = np.meshgrid(xedges[:-1], yedges[:-1]) 
zpos = np.zeros_like(xpos) 
ax.set_xlabel('xlabel') 
ax.set_ylabel('ylabel') 

ax.plot_wireframe(xpos, ypos, hist) 
plt.show() 

回答

0

您可能要計算在極座標直方圖。這使得根據來自中心的半徑濾出不需要的點更容易。

import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D 
import numpy as np; np.random.seed(1) 

r = np.ones(100)*0.9 
phi = np.random.rand(100)*2.*np.pi 

hist, xedges, yedges = np.histogram2d(r, phi, bins=25, range=[[0, 1.2], [0,2*np.pi*26./25]]) 
R,Phi = np.meshgrid(xedges[:-1], yedges[:-1]) 
X = R*np.cos(Phi) 
Y = -R*np.sin(Phi) 

fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 
X[(R < 0.75) | (R > 1)] = np.nan 
ax.plot_surface(X,Y, hist.T, alpha=0.2) 
ax.plot_wireframe(X,Y, hist.T) 
plt.show() 

enter image description here