假设需要根据某个变量更改 matplotlib 标记的边缘颜色,是否可以为标记的边缘颜色分配某种离散颜色图? 这类似于通过 cmap 更改标记的面颜色。
当使用绘图范围之外的箭头显示限制时,我似乎无法根据另一个变量来改变箭头颜色。 例如:在下面的代码中,箭头的颜色不会随着 z 的函数而改变。
plt.scatter(x,y, c=z, marker=u'$\u2191$', s=40,cmap=discrete_cmap(4, 'cubehelix') )
最佳答案
您可以使用分散的 edgecolors
参数来完成此操作。
您需要创建一个颜色列表以提供给scatter
。我们可以使用您选择的 colormap
和 Normalize
实例来执行此操作,将 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()
关于python - 是否可以在 matplotlib 中为边缘颜色分配颜色图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33930784/