[英]How to color the point of a scatter plot separately?
I would like to plot a three variable function in the following way. 我想以下面的方式绘制一个三变量函数。 First I plot evenly distributed points in the 3d figure with scatter plot, than I want to use a method to color every point in the cube according the value of the function.
首先,我使用散点图在3d图中绘制均匀分布的点,而不是根据函数的值使用方法为立方体中的每个点着色。 The function is f(x,y,z)=(x^2+y^2-z^2)^2.
函数是f(x,y,z)=(x ^ 2 + y ^ 2-z ^ 2)^ 2。
So far I managed to fill the plot with little balls evenly, but the coloring does not want to work. 到目前为止,我设法用小球均匀填充情节,但着色不想工作。 I tried to look up how to handle colormap, but the results was either the most basic or it was too hardcore.
我试图查找如何处理色彩映射,但结果要么是最基本的,要么是太硬核了。
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()
I think what I tried wont work even if there is no error, since the pairing of colors with point is not handled. 我认为即使没有错误,我尝试的也不会工作,因为颜色与点的配对不会被处理。 I thought about using plot instead of scatter, but again I did not find any useful material on colormaps.
我想过使用plot而不是scatter,但我再没有在colormap上找到任何有用的材料。
I have three differences from what you have done 我和你所做的有三点不同
ax.scatter3D
in place of ax.scatter
ax.scatter3D
代替了ax.scatter
t.flatten()
) t.flatten()
) and here the code and the plot 这里的代码和情节
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())
Note that, to put in evidence the layers, I have rotated the default view and also that Matplotlib draws the points using a sort of aerial perspective . 请注意,为了证明图层,我已经旋转了默认视图,并且Matplotlib也使用一种空中透视来绘制点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.