简体   繁体   English

Matplotlib 3D绘图忽略不在圆圈中的值

[英]Matplotlib 3D plot dismiss values not in circle

I am using matplotlib to plot a 3D 'histogram' with a wireframe of sine and cosine values of radiants of angles. 我正在使用matplotlib绘制一个3D'直方图',其中包含正弦和余弦角度的线框。 The plot forms a circle as it should: 该图形应该形成一个圆圈:

在此输入图像描述

Now I am trying to just have the circle and its hills plotted but not the values in the circle and around. 现在我试图让圆圈和它的山丘绘制,但不是圆圈和周围的值。 I have tried dismissing every value that is zero with 我试图解雇每个零值

hist[hist==0] = np.nan

but then my plot looks like this, where also some values in the circle are zero and so the wireframe plot does not 'touch the ground' anymore. 但是我的情节看起来像这样,圆圈中的一些值也是零,因此线框图不再“触地”。

在此输入图像描述

So is there a way to dismiss the values in the circle and around it but that the plot still goes all the way down to zero? 那么有没有办法解除圆圈及其周围的值,但是情节仍然一直下降到零?

This is my code: 这是我的代码:

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

You might want to calculate the histogram in polar coordinates. 您可能想要计算极坐标中的直方图。 This makes it easier to filter out the unwanted points according to the radius from the center. 这样可以更容易地根据中心的半径过滤掉不需要的点。

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

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM