0

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})
Seanny123
  • 8,776
  • 13
  • 68
  • 124
  • please add a complete example so we can try to run it and help – puhs Aug 14 '20 at 18:26
  • 2
    Does this answer your question? [How to change the text colour of font in legend?](https://stackoverflow.com/questions/18909696/how-to-change-the-text-colour-of-font-in-legend) – Trenton McKinney Aug 14 '20 at 18:30
  • ```legend_elements = [Line2D([0], [0], color='white', lw=1, label='x'), Line2D([], [], marker='o', linestyle='None', color='white', label='y', markerfacecolor='white', markersize=5), Line2D([], [], marker='o', linestyle='None', color='black', label='z', 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}, facecolor="white") ax.set_axis_off()geo_df2.plot(ax=ax, color='white', linewidth=0.35, zorder=1)``` – KLB241893 Aug 14 '20 at 18:57

1 Answers1

1

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.