Python:在 Seaborn 中更改标记类型

Python: Changing Marker Type in Seaborn

在常规的 matplotlib 中,您可以为绘图指定各种标记样式。但是,如果我导入 seaborn、'+' 和 'x' 样式停止工作并导致绘图不显示 - 其他标记类型,例如'o'、'v' 和“*”有效。

简单示例:

import matplotlib.pyplot as plt
import seaborn as sns

x_cross = [768]
y_cross = [1.028e8]
plt.plot(x_cross, y_cross, 'ok')

plt.gca().set_xlim([10, 1e4])
plt.gca().set_ylim([1, 1e18])
plt.xscale('log')
plt.yscale('log')

plt.show()

产生这个:Simple Seaborn Plot

将第 6 行的 'ok' 更改为 '+k' 但是,不再显示标绘点。如果我不导入 seaborn 它会正常工作:Regular Plot With Cross Marker

谁能告诉我在使用 seaborn 时如何将标记样式更改为十字型?

很喜欢做个bug。然而,您可以通过 mew 关键字设置标记边缘线宽以获得您想要的:

import matplotlib.pyplot as plt
import seaborn as sns

x_cross = [768]
y_cross = [1.028e8]

# set marker edge line width to 0.5
plt.plot(x_cross, y_cross, '+k', mew=.5)

plt.gca().set_xlim([10, 1e4])
plt.gca().set_ylim([1, 1e18])
plt.xscale('log')
plt.yscale('log')

plt.show()

此行为的原因是 seaborn 将标记边缘宽度设置为零。 (参见 source)。

正如 seaborn known issues

中指出的那样

An unfortunate consequence of how the matplotlib marker styles work is that line-art markers (e.g. "+") or markers with facecolor set to "none" will be invisible when the default seaborn style is in effect. This can be changed by using a different markeredgewidth (aliased to mew) either in the function call or globally in the rcParams.

This issue is telling us about it as well as this one

在这种情况下,解决方案是将 markeredgewidth 设置为大于零的值,

  • 使用 rcParams(导入 seaborn 后):

    plt.rcParams["lines.markeredgewidth"] = 1
    
  • 使用 markeredgewidthmew 关键字参数

    plt.plot(..., mew=1)
    

然而,正如@mwaskom 在评论中指出的那样,实际上还有更多内容。在 this issue 中,认为标记应分为两种 类,散装样式标记和线条艺术标记。这已在 matplotlib 2.0 版中部分完成,您可以获得 "plus" 作为标记,使用 marker="P" 并且即使使用 markeredgewidth=0.

也可以看到此标记
plt.plot(x_cross, y_cross, 'kP')