rof*_*122 6 python matplotlib contour python-3.x contourf
我有一个磁盘的 3D 图。现在我想根据数组中存储的值以颜色绘制表面。例如圆盘的半径是300mm。该数组可能是这样的:
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]])
Run Code Online (Sandbox Code Playgroud)
这意味着对于半径 = 114.28,我的值为 14。我想以某种颜色(例如蓝色)在半径 114.28 处绘制一个圆。第二个半径是 128.57。对于半径 128.57,分配值 16。这意味着我想用另一种颜色绘制一个圆圈,例如在图的表面上绘制橙色等等。
我尝试用轮廓图解决这个问题(感谢bazingaa)。它看起来正是我想要的。不幸的是,我刚刚意识到这并不是真正解决我的问题。也许我应该解释一下我想要实现的目标。我想展示某些参数(例如速度)如何沿光盘分布。在这种情况下,随着速度向外侧增加,等高线图也会纠正。但也可能发生这样的情况:参数必须可视化,而这些参数并不总是向外部持续增加。我尝试了这种情况,只是在数组中间设置了一个比之前的值更小的值(我将值 27 从 arr 更改为 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()
Run Code Online (Sandbox Code Playgroud)
我希望有人能提供帮助,这对我来说非常重要。此致 !
这是一种解决方案。我刚刚在您的第一个代码(最后一行是 )之后添加了以下几行ax.plot_surface(X, Y, Z, alpha=0.5, color='lightgrey', rstride=1, cstride=1)。我还没用过plt.show()。phiPS:我通过使用来增加密度,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()
Run Code Online (Sandbox Code Playgroud)
我选择z=20作为等高线图的高度。您可以根据您的选择对其进行修改。
以下是结果图。希望对您有帮助。维尔斯帕斯。
| 归档时间: |
|
| 查看次数: |
815 次 |
| 最近记录: |