python - 更改 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,一些标记不会(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()
推荐阅读
- automation - 使用 InDesign 数据合并导出多个文件
- visual-studio - Visual Studio 2019:我在哪里可以关闭“共享编译”
- google-apps-script - 谷歌表格导出为 txt
- python - 在Python中将矩阵中的每个元素相乘
- python - keras load_model TypeError:int()参数'NoneType'
- php - 使用 php 从两个 db 表中查询
- c - 在 C 中读取具有不一致输入的文件
- javascript - 将表单重定向到 Gatsby 中的唯一 URL?
- c++ - 如何将此函数重写为c ++中的指针?
- python - 需要 Selenium 和基本的 python 帮助