如何根据除X、Y、Z之外的一个值来更改3D散点图的颜色。

huangapple go评论91阅读模式
英文:

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

请看Colormap Normalization

以下是示例:

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()

得到的结果如下:

如何根据除X、Y、Z之外的一个值来更改3D散点图的颜色。

英文:

See Colormap Normalization

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:

如何根据除X、Y、Z之外的一个值来更改3D散点图的颜色。

huangapple
  • 本文由 发表于 2023年5月25日 15:06:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76329678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定