[英]How to plot different shades of a color for each point in a scatter plot in matplotlib?
[英]How to color the point of a scatter plot separately?
我想以下面的方式繪制一個三變量函數。 首先,我使用散點圖在3d圖中繪制均勻分布的點,而不是根據函數的值使用方法為立方體中的每個點着色。 函數是f(x,y,z)=(x ^ 2 + y ^ 2-z ^ 2)^ 2。
到目前為止,我設法用小球均勻填充情節,但着色不想工作。 我試圖查找如何處理色彩映射,但結果要么是最基本的,要么是太硬核了。
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
import math as mt
fig = plt.figure()
ax = fig.gca(projection='3d')
# Make data.
X = np.linspace(-5, 5, 6)
Y = np.linspace(-5, 5, 6)
Z = np.linspace(-5, 5, 6)
X, Y, Z = np.meshgrid(X, Y, Z)
# Plot the surface.
t = (X**2+Y**2-Z**2)**2
surf = ax.scatter(X, Y, Z, c=t, cmap=cm.viridis)
ax.set_zlim(-5, 5)
ax.zaxis.set_major_locator(LinearLocator(5))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
ax.set_xlabel('X axis')
ax.set_ylabel('Y axis')
ax.set_zlabel('Z axis')
# Add a color bar which maps values to colors.
#fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
我認為即使沒有錯誤,我嘗試的也不會工作,因為顏色與點的配對不會被處理。 我想過使用plot而不是scatter,但我再沒有在colormap上找到任何有用的材料。
我和你所做的有三點不同
ax.scatter3D
代替了ax.scatter
t.flatten()
) 這里的代碼和情節
In [31]: from mpl_toolkits.mplot3d import Axes3D
...: import matplotlib.pyplot as plt
...: from matplotlib import cm
...: from matplotlib.ticker import LinearLocator, FormatStrFormatter
...: import numpy as np
...: import math as mt
...:
...: fig = plt.figure()
...: ax = fig.gca(projection='3d')
...:
...: # Make data.
...: X = np.linspace(-5, 5, 6)
...: Y = np.linspace(-5, 5, 6)
...: Z = np.linspace(-5, 5, 6)
...: X, Y, Z = np.meshgrid(X, Y, Z)
...: # Plot the surface.
...: t = Z
...: ax.scatter3D(X, Y, Z, c=t.flatten())
請注意,為了證明圖層,我已經旋轉了默認視圖,並且Matplotlib也使用一種空中透視來繪制點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.