简体   繁体   中英

Python matplotlib: How to assign a different opacity to each point in a scatter plot?

I have a series of points I want to put on a scatter plot. Each point is weighted by how much I care about it. I want the dot on the scatter plot to have an opacity set according to how much I care about it.

This is my current attempt, which fails

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()

The plt.scatter line fails with the exception

AttributeError: 'tuple' object has no attribute 'view'

tcaswell is correct. I simply need to add .tolist()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM