将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)。看起来填充的标记有效,而其他标记则无效。这可能与填充颜色还是边缘颜色有关?
有人可以解释为什么海洋标记不能按我的预期显示或我做错了什么吗?谢谢。
答案 0 :(得分:2)
您是正确的。由于某种原因,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()