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.

Related Question Matplotlib - How can I add labels to legend Matplotlib legend mapped with colour? How can I map my Matplotlib custom legend to my Networkx graph? how can I change the colour of my text in python? How can I have matplotlib support Chinese for **legend**, not only for “ax.text” on colab? How can I add data points to my line plot in same colour (matplotlib)? How can I set the text on my matplotlib figure? How can I do to center my text using matplotlib? For scatterplot with matplotlib how to include in the legend gradient for dot size and colour? How can I make legend error bars horizontal in matplotlib
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM