是否可以在matplotlib中为edgecolors指定颜色映射?

2024-09-28 22:03:09 发布

您现在位置:Python中文网/ 问答频道 /正文

假设需要根据某个变量更改matplotlib标记的边缘颜色,是否可以为标记的边缘颜色指定某种离散颜色映射? 这类似于通过cmap更改标记的面部颜色

当使用超出绘图范围的箭头显示限制时,我似乎无法根据另一个变量改变箭头的颜色。 例:在下面的代码中,箭头的颜色不随z的变化而变化

plt.scatter(x,y, c=z, marker=u'$\u2191$', s=40,cmap=discrete_cmap(4, 'cubehelix') )

Tags: 代码标记绘图matplotlib颜色plt箭头marker
1条回答
网友
1楼 · 发布于 2024-09-28 22:03:09

可以使用edgecolors参数来分散

你需要列一个颜色列表来输入scatter。我们可以使用您选择的colormapNormalizate实例,将z函数重缩放到0-1范围

我假设您的discrete_cmap函数类似于链接的here

import matplotlib.pyplot as plt
import matplotlib.colors as colors
import numpy as np

# def discrete_cmap() is omitted here...

# some sample data
x = np.linspace(0,10,11)
y = np.linspace(0,10,11)
z = x+y

# setup a Normalization instance
norm = colors.Normalize(z.min(),z.max())

# define the colormap
cmap = discrete_cmap(4, 'cubehelix')

# Use the norm and cmap to define the edge colours
edgecols = cmap(norm(z))

# Use that with the `edgecolors` argument. Set c='None' to turn off the facecolor
plt.scatter(x,y, edgecolors=edgecols, c = 'None', marker='o', s=40 )

plt.show()

enter image description here

相关问题 更多 >