【问题标题】:How can I control the color and opacity of each point in a python scatter plot?如何控制 python 散点图中每个点的颜色和不透明度?
【发布时间】:2015-09-08 07:28:53
【问题描述】:

我正在寻找使用不透明度来表示强度的 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) 或类似的东西。

【问题讨论】:

    标签: python colors opacity scatter-plot 4d


    【解决方案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 中的任何一个:

    【讨论】:

    • 非常感谢!虽然当我尝试运行它时,我收到错误“未知属性 depthshade”,尽管它编译的 ax.scatter 语句中没有 depthshade=False。另外,如何去除图表中显示的每个数据点的黑色轮廓?
    • 好的,我从我的答案中删除了 depthshade kwarg(尽管它在API 中)。您可以尝试设置lw=0 来去除散点的边缘。
    • 我能够使用 edgecolors='none' 删除边缘,尽管您使用 depthshade=False 是正确的,但由于某种原因它不接受它并给我那个错误。有什么想法吗?
    • 我猜 depthshade 是添加到最新版本的 MPL 中的关键字,因为它看起来不像在 1.3.x 版本中存在。你用的是什么版本?我上面的情节是用 1.4.3 制作的。
    • 我不确定,我使用的是 PyCharm 4.5.2,虽然 depthshade 仍然存在,但我看到它发生在我的样本集中,因为不透明度用于表示重要的数据,这可不是什么好事。。
    猜你喜欢
    • 1970-01-01
    • 2022-08-03
    • 2020-03-26
    • 2016-01-22
    • 2015-07-04
    • 2021-01-08
    • 2019-11-12
    • 2017-05-09
    • 2015-03-29
    相关资源
    最近更新 更多