I have a page that contains multiple matplotlib subplots. I originally added a title for these subplots by deploying the following code:
title = fig.suptitle('This is a title',horizontalalignment='center',verticalalignment='top')
However, I wanted to add a solid background color to this title and it did not seem that I could using the above method.
Therefore, I set the title using the below code:
fig.text(x=0.5,y=0.97,ha='center',va='top',s='This is a title',color='w',backgroundcolor='blue')
This does add the background color, but it only extends as far as just outside the text itself, as per the attached image.
What I want to do is to be able to have the text where it is, but extend the background color to the width of my page (this page as an example), so that the whole title bar is blue, with the white text displaying in the center where it is at the moment.
Is there a way to achieve this by writing directly onto the figure canvas i.e. with the fig.suptitle or the fig.text methods. Open to other solutions that achieve the goal, but I am looking to avoid deploying a subplot ax object in itself as a title (this seems a bit crude).
Hope that makes sense!
Thanks!
* Update *
I have attempted to deploy the following code:
title = fig.text(x=0.5,y=0.97,ha='center',va='top',s='This is a title',color='w',backgroundcolor='blue')
bb = title.get_bbox_patch()
bb.set_width(w=100)
This relates to the suggestion to look at a similar post in the comments. However, the above code does not provide a solution. The title is simply unaffected by the bbox implementation. Still looking for a solution.
Thanks!
* Update *
Further looking through the other post, gives this as a potential solution and this does work!
title = fig.text(x=0.5,y=0.97,ha='center',va='top',s='This is a title',color='w',backgroundcolor='blue')
title._bbox_patch._mutation_aspect = 0.02
title.get_bbox_patch().set_boxstyle("square", pad=47.8)
However, this seems incredibly crude. An inordinate amount of time is spent with trial and error setting the mutation aspect and the pad so that the background color fits as desired.
See updated image below:
There must be a more prescribed Pythonic method for affecting this outcome, but with more specific control over the positioning and size of the background color! This is the solution that I am really looking for!
Thanks!