![](/img/trans.png)
[英]Python matplotlib: How to assign a different opacity to each point in a scatter plot?
[英]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
。 您可以在此處選擇任何顏色圖,也可以自己制作 :
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.