【问题标题】:Python: Changing Marker Type in SeabornPython:在 Seaborn 中更改标记类型
【发布时间】:2017-07-14 23:37:33
【问题描述】:

在常规 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 时如何将标记样式更改为十字类型吗?

【问题讨论】:

    标签: python matplotlib graph seaborn


    【解决方案1】:

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

    正如seaborn known issues中指出的那样

    matplotlib 标记样式如何工作的一个不幸结果是,当默认 seaborn 样式生效时,线条艺术标记(例如 "+")或 facecolor 设置为 "none" 的标记将不可见。这可以通过在函数调用中或全局 rcParams 中使用不同的 markeredgewidth(别名为 mew)来更改。

    This issuethis one 都在告诉我们这件事。

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

    • 使用 rcParams(导入 seaborn 后):

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

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

    然而,正如@mwaskom 在 cmets 中指出的那样,实际上还有更多内容。在this issue 中,有人认为标记应该分为两类,散装样式标记和线条艺术标记。这已在 matplotlib 2.0 版中部分完成,您可以使用marker="P" 获得“加号”作为标记,即使使用markeredgewidth=0,该标记也将可见。

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

    【讨论】:

    • 啊;我想我以后应该总是先检查“已知问题”。感谢您扩展上一个答案。
    • 您链接到的两个问题实际上是一个相似但独立的问题,它是 matplotlib 1.4.2 中的一个错误。最相关的问题在 matplotlib github 中:github.com/matplotlib/matplotlib/issues/4679。这是在 matplotlib 2 中处理的。通过添加“填充”版本的线稿标记,例如无论线宽设置如何,"P" 都会画一个加号。
    【解决方案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()
    

    【讨论】:

      猜你喜欢
      • 2016-04-20
      • 2021-11-17
      • 2017-06-30
      • 2023-03-10
      • 2021-12-22
      • 1970-01-01
      • 2016-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多