Skip to main content
 首页 » 编程设计

python之更改 maptlotlib 的默认样式会破坏图例标记

2024年09月03日38飞鱼

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

考虑以下 MWE,其中所有来自 official documentation 的图例标记被绘制。首先使用默认样式制作绘图,然后使用 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,一些标记不会(tri_down、tri_up、tri_left、tri_right、plus、x、vline)。看起来填充的标记有效,但其他标记无效。这可能与填充颜色和边缘颜色有关吗?

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

请您参考如下方法:

你是对的。由于某些原因,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()