散点图中的各个alpha值 [英] Individual alpha values in scatter plot
问题描述
我想知道是否可以使用Matplotlib的scatter
函数为每个点绘制单独的alpha值.我需要绘制一组点,每个点都有其alpha值.
I'm wondering if it is possible to have individual alpha values for each point to be plotted using the scatter
function of Matplotlib. I need to plot a set of points, each one with its alpha value.
例如,我有这段代码可以绘制一些点
For example, I have this code to plot some points
def plot_singularities(points_x, p, alpha_point, file_path):
plt.figure()
plt.scatter(points_x, points_y, alpha=alpha_point)
plt.savefig(file_path + '.png', dpi=100)
plt.close()
我的所有points_x
,points_y
和alpha_point
都具有n个值.但是,我无法将数组分配给scatter()
中的alpha
参数.如何为每个点设置不同的alpha值?我可以逐个循环并绘制每个特定的alpha值,但这似乎不是一个好方法.
All my points_x
, points_y
and alpha_point
have n values. However, I can't assign an array to the alpha
parameter in scatter()
. How can I have a different alpha value for each point? I can loop and plot point by point with each specific alpha value, but this doesn't seem like a good approach.
推荐答案
tcaswell的建议是正确的,您可以这样做:
tcaswell's suggestion is correct, you can do it like this:
import numpy as np
import matplotlib.pylab as plt
x = np.arange(10)
y = np.arange(10)
alphas = np.linspace(0.1, 1, 10)
rgba_colors = np.zeros((10,4))
# for red the first column needs to be one
rgba_colors[:,0] = 1.0
# the fourth column needs to be your alphas
rgba_colors[:, 3] = alphas
plt.scatter(x, y, color=rgba_colors)
plt.show()
这篇关于散点图中的各个alpha值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!