Issue
Edit: Solution below, original question misses a bit of key information
I am having difficulties changing the title color for my figure in Matplotlib.
I have a figure containing subplots:
import matplotlib as mpl
import matplotlib.pyplot as plt
fig, ax = plt.subplots(3, 4, sharex='all', sharey='all',
figsize=(20, 9), dpi=600)
The figure has 10 subplots with data using the same symbols on each plot, so I want a joint legend for the figure, not for each axis.
I have blanked the upper right and middle right axes - I want the legend to go here:
ax[0,3].axis('off')
ax[1,3].axis('off')
I've created my legend elements:
legend_elements = [Line2D([0], [0], marker='x', color='w', markeredgecolor='b', label='Measured data points'),
Line2D([0], [0], marker='o', color='w', markeredgecolor='g', label='Means of data points'),
Line2D([0], [0], color='r', label='Curve fitted to model')]
When writing my legend, I'm having real difficulty changing the color of the title text. I have tried the following examples:
legend = fig.legend(handles=legend_elements, fontsize=10, labelcolor='c',
loc='lower left', bbox_to_anchor=(0.77, 0.63, 1, 1))
legend.set_title('Legend\n', color='c')
TypeError: Legend.set_title() got an unexpected keyword argument 'color'
...
legend = fig.legend(handles=legend_elements, fontsize=10, labelcolor='c',
loc='lower left', bbox_to_anchor=(0.77, 0.63, 1, 1),
title='Legend\n', title_fontsize=10)
plt.setp(legend.get_title(), color='c')
AttributeError: 'NoneType' object has no attribute 'get_title'
...
fig.setp(legend.get_title(), color='c')
AttributeError: 'Figure' object has no attribute 'setp'
...
legend._legend_title_box._text.set_color('c')
AttributeError: 'NoneType' object has no attribute '_legend_title_box'
I've also looked at FontProperties, something along the lines of the following, but FontProperties doesn't have a color kwarg as far as I can tell:
import matplotlib.font_manager as font_manager
legend_title_props = font_manager.FontProperties(color='c')
legend = fig.legend(handles=legend_elements, fontsize=10, labelcolor='c',
loc='lower left', bbox_to_anchor=(0.77, 0.63, 1, 1),
title='Legend\n', title_fontsize=10,
title_fontproperties=legend_title_props)
TypeError: FontProperties.__init__() got an unexpected keyword argument 'color'
Does anyone have a workaround? I can't seem to find any other options online. I've got a feeling it stems from the fact that my legend is being stored as a 'NoneType' object, but I can't work out what I'm doing wrong to cause this.
Solution
Found the solution to this. I'll pop it here in case anyone else comes across the problem.
I had simplified the example somewhat from my original code, to make the question more clear. One of the simplifications I had done was remove some style editing that followed the legend, which turned out to be a fairly critical omission. The original statement in my code looked something like this, which resulted in legend being a NoneType object
legend = fig.legend().get_frame().set_boxstyle('Round')
I've now altered the code to the following which fixes things.
legend = fig.legend(handles=legend_elements, fontsize=10, labelcolor='c',
loc='lower left', bbox_to_anchor=(0.77, 0.63, 1, 1),
title='Legend\n', title_fontsize=10)
plt.setp(legend.get_title(), color='c')
legend.get_frame().set_boxstyle('Round')
Answered By - georussell
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.