【问题标题】:Matplotlib 3D plot dismiss values not in circleMatplotlib 3D绘图消除不在圆圈中的值
【发布时间】:2017-04-26 09:40:01
【问题描述】:

我正在使用 matplotlib 绘制一个 3D“直方图”,其中包含角度辐射的正弦和余弦值的线框。该图应形成一个圆圈:

现在我试图只绘制圆圈​​及其山丘,但不绘制圆圈及其周围的值。 我试过用

来消除每个为零的值
hist[hist==0] = np.nan

但是我的情节看起来像这样,圆圈中的一些值也为零,因此线框情节不再“接触地面”。

那么有没有办法消除圆圈中及其周围的值,但情节仍然一直下降到零?

这是我的代码:

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()

【问题讨论】:

    标签: python numpy matplotlib plot


    【解决方案1】:

    您可能希望在极坐标中计算直方图。这样可以更轻松地根据中心的半径过滤掉不需要的点。

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-26
      • 1970-01-01
      • 1970-01-01
      • 2013-10-30
      • 1970-01-01
      • 2020-03-01
      相关资源
      最近更新 更多