繁体   English   中英

如何在 matplotlib 中编辑我的图例的文本颜色?

[英]How can I edit the text colour of my legend in matplotlib?

我目前正在尝试将我的图例的文本颜色设置为“白色”,但无法使其正常工作。 我当前的代码:

legend_elements = [Line2D([0], [0], color='white', lw=1, label='Connection to Early Émigré'),
                   Line2D([], [], marker='o',linestyle='None', color='white', label='Early Émigré (by 1935)',
                   markerfacecolor='white', markersize=5),Line2D([], [],  marker='o',linestyle='None', color='black', label='Not Emigrated by 1935', markerfacecolor='black', markersize=5)]

ax = map_df.plot(color='dimgrey', edgecolor='black', linewidth=0.075)
ax.legend(handles=legend_elements,, loc='lower right',prop={'size': 6})

在 ax.legend function 中,您可以为 facecolor(图例的背景)和 edgecolor(图例边框)传递关键字参数 (kwarg)。 文档在这里)

在这种情况下,将最后一行替换为以下内容:

ax.legend(handles=legend_elements,, loc='lower right',prop={'size': 6}, facecolor="white")

如果您只需要更改文本颜色,请参阅this answer to a similar question。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM