![](/img/trans.png)
[英]Matplotlib - plot_surface : get the x,y,z values written in the bottom right corner
[英]Matplotlib: Set cmap in plot_surface to x and y-axes
如何設置與圖形半徑相關的顏色圖?
我怎樣才能關閉圓柱體的末端(在元件上,而不是頂部和底部)?
我的腳本:
import numpy as np
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from math import sin, cos, pi
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
h, w = 60,30
znew = np.random.randint(low=90, high=110, size=(60,30))
theta = np.linspace(0,2*pi, h)
Z = np.linspace(0,1,w)
Z,theta = np.meshgrid(Z, theta)
R = 1
X = (R*np.cos(theta))*znew
Y = (R*np.sin(theta))*znew
ax1 = ax.plot_surface(X,Y,Z,linewidth = 0, cmap="coolwarm",
vmin= 80,vmax=130, shade = True, alpha = 0.75)
fig.colorbar(ax1, shrink=0.9, aspect=5)
plt.show()
首先,你需要使用facecolors
關鍵字參數plot_surface
與任意(非提請表面Z
為基礎)的顏色。 您必須為每個點傳遞一個顯式的 RGBA 顏色 4,這意味着我們需要使用每個點的半徑給定的鍵對顏色圖對象進行采樣。 最后,這將破壞結果表面的可映射屬性,因此我們必須通過手動告訴它使用我們的半徑來構建顏色條:
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.cm as cm
from matplotlib.colors import Normalize
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
h, w = 60,30
#znew = np.random.randint(low=90, high=110, size=(h,w))
theta = np.linspace(0,2*np.pi, h)
Z = np.linspace(0,1,w)
Z,theta = np.meshgrid(Z, theta)
znew = 100 + 10*np.cos(theta/2)*np.cos(2*Z*np.pi)
R = 1
X = (R*np.cos(theta))*znew
Y = (R*np.sin(theta))*znew
true_radius = np.sqrt(X**2 + Y**2)
norm = Normalize()
colors = norm(true_radius) # auto-adjust true radius into [0,1] for color mapping
cmap = cm.get_cmap("coolwarm")
ax.plot_surface(X, Y, Z, linewidth=0, facecolors=cmap(colors), shade=True, alpha=0.75)
# the surface is not mappable, we need to handle the colorbar manually
mappable = cm.ScalarMappable(cmap=cmap)
mappable.set_array(colors)
fig.colorbar(mappable, shrink=0.9, aspect=5)
plt.show()
請注意,我將半徑更改為平滑的東西,以減少看起來混亂的結果。 true_radius
數組包含數據單元中的實際半徑,歸一化后變為colors
(本質上是colors = (true_radius - true_radius.min())/true_radius.ptp()
)。
結果:
最后,請注意我生成的半徑使得圓柱體不會無縫閉合。 這模仿您的隨機示例輸入。 沒有什么,只要半徑不2π周期,你可以做這個theta
。 這與可視化無關,這是幾何。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.