英文:
How to change color of a 3D scatter plot w.r.t. one value other than X,Y,Z
问题
I can provide the translation for the code part as requested:
我可以提供代码部分的翻译,如下所示:
# Import necessary libraries
# 导入必要的库
from matplotlib import pyplot as plt
# Create a 3D scatter plot
# 创建一个三维散点图
fig = plt.figure(figsize=(30, 30))
ax = fig.add_subplot(projection='3d')
ax.scatter(X, Y, Z)
Please note that the translation doesn't include the part about associating the "Values" array with the scatter plot. If you need help with that part, please let me know.
英文:
I have 4 numpy arrays for X, Y, Z, and Values.
X= [20,30,50,60,..]
Y= [25,35,55,65,...]
Z= [5,6,7,8,...]
Values = [-8,5,0.8,-1.2....]
All arrays are of the same size and the index of all arrays is matched. Eg. X1,Y1,Z1,Values1 all corresponds to same point.
Based on X, Y, and Z I have to make a scatter plot. But colour of the scatter plot should change according to the Values array. If abs value of items from the Values array is high then scatter point should have colour red and gradually change to blue if value is less. Any colormap will do.
I have managed to plot a 3d scatter plot using X,Y,Z but I am not sure how to associate Values with it.
from matplotlib import pyplot as plt
fig = plt.figure(figsize=(30, 30))
ax = fig.add_subplot(projection='3d')
ax.scatter(X,Y,Z)
答案1
得分: 1
以下是示例:
import numpy as np
from matplotlib import colors
from matplotlib import pyplot as plt
N = 50
X = np.random.rand(N)
Y = np.random.rand(N)
Z = np.random.rand(N)
values = np.random.randn(N)
vmin, vcenter, vmax = -2, 1, 2
divnorm = colors.TwoSlopeNorm(vmin=vmin, vcenter=vcenter, vmax=vmax)
fig = plt.figure()
ax = fig.add_subplot(projection="3d")
p = ax.scatter(X, Y, Z, c=values, cmap="coolwarm", norm=divnorm)
cb = fig.colorbar(p, extend="both")
cb.set_ticks(np.arange(vmin, vmax + 1))
plt.show()
得到的结果如下:
英文:
Here is an example:
import numpy as np
from matplotlib import colors
from matplotlib import pyplot as plt
N = 50
X = np.random.rand(N)
Y = np.random.rand(N)
Z = np.random.rand(N)
values = np.random.randn(N)
vmin, vcenter, vmax = -2, 1, 2
divnorm = colors.TwoSlopeNorm(vmin=vmin, vcenter=vcenter, vmax=vmax)
fig = plt.figure()
ax = fig.add_subplot(projection="3d")
p = ax.scatter(X, Y, Z, c=values, cmap="coolwarm", norm=divnorm)
cb = fig.colorbar(p, extend="both")
cb.set_ticks(np.arange(vmin, vmax + 1))
plt.show()
Which gives:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论