如何控制 python 散点图中每个点的颜色和不透明度?
How can I control the color and opacity of each point in a python scatter plot?
我正在寻找使用不透明度来表示强度的 4D 数据集(X、Y、Z、强度)的图形。我还希望颜色也取决于 Z 变量,以便更好地显示深度。
相关代码到此为止,我是菜鸟 Python:
.
.
.
x_list #list of x values as floats
y_list #list of y values as floats
z_list #list of z values as floats
i_list #list of intensity values as floats
.
.
.
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
Axes3D.scatter(ax, x_list, y_list, z_list)
.
.
.
那我该怎么做呢?
我认为颜色可能是 z_list 和颜色图(例如 hsv)之间的线性关系,不透明度也可能是线性关系,i_list/max(i_list) 或类似的东西。
我会做如下事情:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# choose your colormap
cmap = plt.cm.jet
# get a Nx4 array of RGBA corresponding to zs
# cmap expects values between 0 and 1
z_list = np.array(z_list) # if z_list is type `list`
colors = cmap(z_list / z_list.max())
# set the alpha values according to i_list
# must satisfy 0 <= i <= 1
i_list = np.array(i_list)
colors[:,-1] = i_list / i_list.max()
# then plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x_list, y_list, z_list, c=colors)
plt.show()
这里有一个 x_list = y_list = z_list = i_list
的例子。您可以选择 colormaps here or make your own 中的任何一个:
我正在寻找使用不透明度来表示强度的 4D 数据集(X、Y、Z、强度)的图形。我还希望颜色也取决于 Z 变量,以便更好地显示深度。
相关代码到此为止,我是菜鸟 Python:
.
.
.
x_list #list of x values as floats
y_list #list of y values as floats
z_list #list of z values as floats
i_list #list of intensity values as floats
.
.
.
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
Axes3D.scatter(ax, x_list, y_list, z_list)
.
.
.
那我该怎么做呢?
我认为颜色可能是 z_list 和颜色图(例如 hsv)之间的线性关系,不透明度也可能是线性关系,i_list/max(i_list) 或类似的东西。
我会做如下事情:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# choose your colormap
cmap = plt.cm.jet
# get a Nx4 array of RGBA corresponding to zs
# cmap expects values between 0 and 1
z_list = np.array(z_list) # if z_list is type `list`
colors = cmap(z_list / z_list.max())
# set the alpha values according to i_list
# must satisfy 0 <= i <= 1
i_list = np.array(i_list)
colors[:,-1] = i_list / i_list.max()
# then plot
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x_list, y_list, z_list, c=colors)
plt.show()
这里有一个 x_list = y_list = z_list = i_list
的例子。您可以选择 colormaps here or make your own 中的任何一个: