如何控制python散点图中每个点的颜色和不透明度?

时间:2015-06-22 18:05:41

标签: python colors opacity scatter-plot 4d

我正在寻找使用不透明度来表示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)或沿着这些线的东西。

1 个答案:

答案 0 :(得分:1)

我会做以下事情:

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 heremake your own中的任何一个: enter image description here

相关问题