python - matplotlib 散点图的颜色参数是 `c` ,但是 `color` 也可以;产品。差异。结果

标签 python matplotlib

我在 matplotlib 的散点图中不小心使用了 color 而不是 c 作为颜色参数(c 在文档中列出`)它起作用了,但结果是不同的:默认情况下边缘颜色消失了。现在,我想知道这是否是期望的行为,以及它如何以及为何起作用......

enter image description here

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(10,10))

samples = np.random.randn(30,2)

ax[0][0].scatter(samples[:,0], samples[:,1], 
            color='red',
            label='color="red"')

ax[1][0].scatter(samples[:,0], samples[:,1],
            c='red',
            label='c="red"')

ax[0][1].scatter(samples[:,0], samples[:,1], 
            edgecolor='white', 
            c='red',
            label='c="red", edgecolor="white"')

ax[1][1].scatter(samples[:,0], samples[:,1], 
            edgecolor='0', 
            c='1',
            label='color="1.0", edgecolor="0"')

for row in ax:
    for col in row:
        col.legend(loc='upper left') 

plt.show()

最佳答案

这不是错误,而是 IMO,matplotlib 的文档有点含糊。

标记的颜色,可以通过ccoloredgecolorfacecolor 来定义。

caxes.pyscatter()的源码中。这相当于 facecolor。使用c='r'时,edgecolor未定义,matplotlib.rcParams中的默认值生效,有默认值k(黑色)的值。

coloredgecolorfacecolor 被传递给 collection.Collection 对象 scatter() 返回。正如您将在源代码 collections.py 中看到的(set_color()set_edgecolor()set_facecolor() 方法),set_color() 基本上调用了 set_edgecolorset_facecolor,因此将这两个属性设置为相同的值。

我希望这些可以解释您在 OP 中描述的行为。在 c='red' 的情况下,边缘为黑色,表面颜色为红色。在 color=red 的情况下,面颜色和边颜色都是红色。

关于python - matplotlib 散点图的颜色参数是 `c` ,但是 `color` 也可以;产品。差异。结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24749215/

相关文章:

python - 将字符串相乘,使其垂直向下移动一页?

python - 将实时输出保存到 python 中的文件中

python - 如何使用 PyBrain?

python - 如何自定义散点矩阵以查看所有标题?

python - 使用 MethodType 将方法从一个实例动态添加到另一个实例

python - 没有名为 rest_framework 的模块。 Django + docker

python - 在 y = 0 和 y 的正值之间填充

python - 使用 seaborn 在 python 中绘制 3 列的热图

Python BoxPlot 错误 - 'Series' 对象没有属性 'boxplot'

python - 如何使用 cartopy 绘制 SRTM 源栅格?