我有一张磁盘的三维图。
现在,我想根据数组中存储的值以颜色绘制曲面。
圆盘的半径为300毫米。数组可以是:
arr = np.array([[ 114.28, 14],
[ 128.57, 16],
[ 142.85,19],
[ 157.13,20],
[ 171.41,21],
[ 185.69,22],
[ 199.97,24],
[ 214.25,27],
[ 228.53,29],
[ 242.81,30],
[ 257.09,31],
[ 271.37,34],
[ 288.65,35],
[ 299.93,36],
[ 300,38]])
这意味着半径=114.28时,我的值是14我想画一个半径114.28的圆,颜色是蓝色第二个半径是128.57对于半径128.57,指定值16。这意味着我想用另一种颜色画一个圆,例如在图的表面画橙色等等。
我试着用轮廓图来解决这个问题(多亏了巴辛加)。它看起来和我想要的一模一样不幸的是,我刚刚意识到这并不是解决我问题的真正办法。也许我应该解释一下我想达到的目标。我想展示某些参数,如速度,是如何沿光盘分布的在这种情况下,等高线图也会纠正,因为速度增加,向外部。但也可能发生的情况是,参数必须可视化,而这些参数并不总是朝着外部不断增加。我尝试了这个场景,只在数组中间设置了一个小于它之前的值的值(我将arr中的值27更改为14),而轮廓图中除了图例更改之外什么都没有发生也许等值线图根本不是正确的方法也许我得画出单独的圆圈,给它们指定颜色,然后在圆圈之间进行插值来填补空白。
import numpy as np
import matplotlib as mlp
import matplotlib.pyplot as plt
import mpl_toolkits.mplot3d.axes3d as axes3d
ri = 100
ra = 300
h=20
# input xy coordinates
xy = np.array([[ri,0],[ra,0],[ra,h],[ri,h],[ri,0]])
# radial component is x values of input
r = xy[:,0]
# angular component is one revolution of 30 steps
phi = np.linspace(0, 2*np.pi, 2000)
# create grid
R,Phi = np.meshgrid(r,phi)
# transform to cartesian coordinates
X = R*np.cos(Phi)
Y = R*np.sin(Phi)
# Z values are y values, repeated 30 times
Z = np.tile(xy[:,1],len(Y)).reshape(Y.shape)
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1, projection='3d')
ax.set_zlim(0,200)
ax.plot_surface(X, Y, Z, alpha=0.5, color='lightgrey', rstride=1, cstride=1)
plt.contourf(X, Y, np.sqrt(X ** 2 + Y ** 2), zdir='z', offset=20,
cmap=plt.cm.rainbow)
cbar = plt.colorbar()
arr = np.array([[ 114.28, 14],
[ 128.57, 16],
[ 142.85,19],
[ 157.13,20],
[ 171.41,21],
[ 185.69,22],
[ 199.97,24],
[ 214.25,27],
[ 228.53,29],
[ 242.81,30],
[ 257.09,31],
[ 271.37,34],
[ 288.65,35],
[ 299.93,36],
[ 300,38]])
cbar.ax.set_yticklabels(arr[:,1])
plt.show()
我希望有人能帮忙,这对我很重要。
谨致问候!
最佳答案
这里有一个解决方案。我刚刚在第一个代码后面添加了以下几行,最后一行是ax.plot_surface(X, Y, Z, alpha=0.5, color='lightgrey', rstride=1, cstride=1)
。我还没有使用plt.show()
注:我使用phi
增加了phi = np.linspace(0, 2*np.pi, 2000)
的密度,因为最初你只有20
数据点。这是为了绘制相对平滑的圆。
plt.contourf(X, Y, np.sqrt(X ** 2 + Y ** 2), zdir='z', offset=20,
cmap=plt.cm.bwr)
plt.colorbar()
plt.show()
我选择了
z=20
作为等高线图的高度。你可以根据自己的选择修改它。下面是结果图希望对你有帮助。维尔·斯帕斯。
关于python - matplotlib:为半径分配颜色,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51684546/