Matplotlib 使图中的红色变暗

Matplotlib dulls the color red in the plot

当我使用Matplotlib 在Spyder 中绘制图形时,红色有时会变暗。例如,

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0,10,100)
y = 2*x

plt.plot(x,y, color = 'r', linewidth = 10)
plt.show()

将生成图像

而不是

如果我在看到暗淡的红色后重新启动 Spyder,那么它会生成具有正常红色的良好绘图。我只在用红色绘图时才注意到这一点。我使用的版本是 Python 3.7.0、Spyder 3.3.4、IPython 7.2.0 和 Matplotlib 3.0.2。如果相关的话,我也在使用 Mac Mojave。

当我 运行 你的代码时,我得到了第二个正常红色的图。我正在使用 Matplotlib 3.0.3 版本。 顺便说一句,你总是可以通过在你的代码中指定 rgb 来玩弄颜色(rgb 应该在 0.0 到 1.0 的范围内)来获得你喜欢的颜色:

plt.plot(x,y, color = (0.95, 0, 0), linewidth = 10)

在执行代码之前的某个地方,你会看到一行类似

的内容
import seaborn as sns
sns.set(style="white")

或类似的。

完整代码:

import seaborn as sns
sns.set(style="white")

import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0,10,100)
y = 2*x

plt.plot(x,y, color = 'r', linewidth = 10)
plt.show()

解释是,如果 seaborn.set 参数设置为 True(默认值),seaborn.set 会更新颜色。

color_codes : bool If True and palette is a seaborn palette, remap the shorthand color codes (e.g. "b", "g", "r", etc.) to the colors from this palette.

seaborn 文档中也有一个example

所以你可以使用

import seaborn as sns
sns.set(style="white", color_codes=False)

获得

或者根本不使用 .set