【问题标题】:Python matplotlib: How to assign a different opacity to each point in a scatter plot?Python matplotlib:如何为散点图中的每个点分配不同的不透明度?
【发布时间】:2014-02-13 22:44:42
【问题描述】:

我想将一系列点放在散点图上。每一点都取决于我对它的关心程度。我希望散点图上的点根据我对它的关心程度设置不透明度。

这是我目前的尝试,失败了

import pandas as pd
import matplotlib.pyplot as plt

x = pd.Series([0, .2, .4, .6, .8, 1])
y = pd.Series([0, .2, .4, .6, .8, 1])
weights = pd.Series([0, .2, .4, .6, .8, 1])
# this is me trying to create RGBA tuples from the weights
colors = weights.apply(lambda x: (0,0,1,x))
plt.scatter(x, y, c=colors)
plt.show()

plt.scatter 行失败并出现异常

AttributeError: 'tuple' 对象没有属性 'view'

【问题讨论】:

  • 尝试将您的 pandas 对象 (colors) 转换为列表或 ndarray。
  • 请包含完整的回溯。

标签: python matplotlib pandas


【解决方案1】:

tcaswell 是正确的。我只需要添加 .tolist()

colors = weights.apply(lambda x: (0,0,1,x)).tolist()

【讨论】:

    猜你喜欢
    • 2019-11-12
    • 2022-08-03
    • 2015-09-08
    • 1970-01-01
    • 2020-03-26
    • 2020-12-19
    • 2015-07-18
    • 2021-12-19
    相关资源
    最近更新 更多