首页 > 解决方案 > 更改 maptlotlib 的默认样式会破坏图例标记

问题描述

将 maptlotlib 的默认样式更改为 seaborn 会导致某些图例标记无法正确显示。

考虑以下 MWE,其中绘制了官方文档中的所有图例标记。首先,绘图是使用默认样式制作的,然后是 Seaborn 的样式。

from matplotlib.lines import Line2D
import matplotlib.pyplot as plt

# https://matplotlib.org/api/markers_api.html
keys = ["point", "pixel", "circle", r"triangle_down", r"triangle_up",
        r"triangle_left", r"triangle_right", r"tri_down", r"tri_up", 
        r"tri_left", r"tri_right", "octagon", "square", "pentagon",
        "plus (filled)", "star", "hexagon1", "hexagon2", "plus", "x",
        "x (filled)", "diamond", r"thin_diamond", "vline", "hline"]

values = [".", ",", "o", "v", "^", "<", ">", "1", "2", "3", "4", "8", "s", "p",
          "P", "*", "h", "H", "+", "x", "X", "D", "d", "|", "_"]

# Make dictionary of markers and their description
markers = dict(zip(keys, values))

# Plot all markers with default style and seaborn style
for style in 'default', 'seaborn':
    plt.style.use(style)

    # Create handles for legend call
    legend_elements = []
    for key, value in markers.items():
        legend_elements.append(Line2D([0], [0], markersize=10, marker=value,
                                      label=key))
    # Create the figure
    fig, ax = plt.subplots(figsize=[6, 4])
    ax.legend(handles=legend_elements, loc='center', ncol=3)
    plt.show()

上面给了我以下两个图表:

默认样式图例 seaborn 风格传奇

请注意,使用默认样式时,所有标记都按预期显示。但是,对于 seaborn,一些标记不会(tri_down、tri_up、tri_left、tri_right、plus、x、vline)。看起来填充的标记有效,但其他标记无效。这可能与填充颜色与边缘颜色有关吗?

有人可以解释为什么seaborn标记没有按我预期的那样显示或者我做错了什么?谢谢。

标签: pythonmatplotlibseaborn

解决方案


你是对的。由于某种原因,seaborn 的样式表中的标记线宽为零,这使得未填充的标记消失了

print("Value of 'lines.markeredgewidth' in rcParams")
for style in 'default', 'seaborn':
    plt.style.use(style)
    print(style, ': ', matplotlib.rcParams['lines.markeredgewidth'])

>>

Value of 'lines.markeredgewidth' in rcParams
default :  1.0
seaborn :  0.0

在 rcParams 中更改此参数可防止这种情况发生:

from matplotlib.lines import Line2D
import matplotlib.pyplot as plt

# https://matplotlib.org/api/markers_api.html
keys = ["point", "pixel", "circle", r"triangle_down", r"triangle_up",
        r"triangle_left", r"triangle_right", r"tri_down", r"tri_up", 
        r"tri_left", r"tri_right", "octagon", "square", "pentagon",
        "plus (filled)", "star", "hexagon1", "hexagon2", "plus", "x",
        "x (filled)", "diamond", r"thin_diamond", "vline", "hline"]

values = [".", ",", "o", "v", "^", "<", ">", "1", "2", "3", "4", "8", "s", "p",
          "P", "*", "h", "H", "+", "x", "X", "D", "d", "|", "_"]

# Make dictionary of markers and their description
markers = dict(zip(keys, values))

# Plot all markers with default style and seaborn style
for style in 'default', 'seaborn':
    plt.style.use(style)
    matplotlib.rcParams['lines.markeredgewidth'] = 1.
    # Create handles for legend call
    legend_elements = []
    for key, value in markers.items():
        legend_elements.append(Line2D([0], [0], markersize=10, marker=value,
                                      label=key))
    # Create the figure
    fig, ax = plt.subplots(figsize=[6, 4])
    ax.legend(handles=legend_elements, loc='center', ncol=3)
    plt.show()

推荐阅读