Python:在Seaborn中更改标记类型

2024-04-25 00:40:03 发布

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

在常规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行的“确定”更改为“+k”,将不再显示打印点。如果我不导入seaborn,它就会正常工作:Regular Plot With Cross Marker

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


Tags: 标记importlog绘图类型plotmatplotlibas
2条回答

很像是一只虫子。但是,您可以通过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()

enter image description here

这种行为的原因是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和{a4}一样在告诉我们这件事。在

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

  • 使用rcParams(导入seaborn后):

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

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

然而,正如@mwaskom在评论中指出的,事实上还有更多。在this issue中,有人认为标记应分为两类,即大块式标记和线条艺术标记。这在matplotlib版本2.0中已经完成了一部分,您可以使用marker="P"获得一个“plus”作为标记,并且即使使用markeredgewidth=0,该标记也将可见。在

plt.plot(x_cross, y_cross, 'kP')

enter image description here

相关问题 更多 >