I made a scatter plot with 3 different colors and I want to match the color of the symbol and the text in the legend.
A nice solution exist for the case of line plots:
leg = ax.legend()
# change the font colors to match the line colors:
for line,text in zip(leg.get_lines(), leg.get_texts()):
text.set_color(line.get_color())
However, scatter plot colors cannot be accessed by get_lines().For the case of 3 colors I think I can manually set the text colors one-by-one using eg. text.set_color('r'). But I was curious if it can be done automatically as lines. Thanks!


get_childrenorget_patches? Could you provide a minimum working example? You may want to look into the legend methods for other ideas: matplotlib.org/1.3.0/api/legend_api.html - Aleksander Lidtkeleg.get_patches()before and nothing returns. I just triedleg.get_children()and it returnsmatplotlib.offsetbox.VPackerandmatplotlib.patches.FancyBboxPatchobjects, none of which has the propertyget_color(). The solution link has a working example for lines, sorry with scatter plots I only has a non-working example... - Phyinmilabelcoloras described in this answer: https://stackoverflow.com/a/63273370/6135182 - Zaus