python/ python-3.x/ matplotlib
I am currently trying to set the text colour of my legend to "white" but can not get it to work. My current code:
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})
In the ax.legend function, you can pass a keyword argument (kwarg) for both facecolor (background of the legend) and edgecolor (legend border). ( Documentation here)
In this case, replace the last line with the following:
ax.legend(handles=legend_elements,, loc='lower right',prop={'size': 6}, facecolor="white")
If you need to change only the text color, refer to this answer to a similar question.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.