Wednesday, January 17, 2018

matplotlib: set title color in stylesheet

Leave a Comment

I am setting a custom stylesheet in mpl. I found and modified some example settings online:

axes.titlesize  : 18 axes.labelsize  : large axes.labelcolor : ffffff 

I also want to change the font color of the title. From these settings, axes.titlecolor seemed like a good guess, but it doesn't work. Any ideas on how to do this?

3 Answers

Answers 1

I am not completely familiar with how the Mpl stylesheets are actually implemented but my guess is that they are just pre-edited mpl rc files.

If they are indeed mpl rc files then based on reading through my current rc file there is no attribute in the 'Axes' for the color of the title.

My work around for adding stylistic functionality that may or may not be implemented in the rc files is do do something like:

using_custom_style = true plt.use('my_style') ... if using_custom_style:     ax.set_title('my title', color=my_color_of_choice) else:     ax.set_title('my_title')  # this doesn't specify a color so it will just use whatever default vaule mpl knows to use 

Its not the best workaround since it adds a lot of clutter o the code but it the only thing I have found to work when I can't figure out where the default setting is actually stored in the mpl param files or stylesheets.

analyzing the mpl rc file if you run something like 'grep title matplotlibrc' it spits out the only instances it finds the word title in the file:

# special text sizes tick labels, axes, labels, title, etc, see the rc

#axes.titlesize : large # fontsize of the axes title

so there doesn't seem to be a default color setting for the title - or rather if tis there its called something else that isn't directly obvious

'grep color matplotlibrc' outputs all instances of the word color in the rc file

Again there doesn't seem to be anything directly related to title colors or even default text color.

Looking a little closer at the Mpl API, axes.set_title() takes kwargs like color=, that are just labeled as valid Text properties.

http://matplotlib.org/1.4.3/api/text_api.html#matplotlib.text.Text lists valid Text properties. For color all it says is 'any matplotlib color'

I'd imagine somewhere in the mpl source code there's a line that handles what to do when no color kwarg is specified for a given Text object. Somewhere there's a default color which I think is probably just 'k'. It isn't clear to me at this time, however, how to change that setting.

Sorry for the long discourse, hope this helps some.

Answers 2

May be bit later answer but anyway ) If you want to change default title color you could set text.color property in matplotrc file (default is black). Of course it will change colors of all your text outputs.

Answers 3

This may work (Refrenced From Here)

title_obj = plt.title('my random fig')  plt.getp(title_obj)                     plt.getp(title_obj, 'text')             plt.setp(title_obj, color='r')    #Sets it to the color red      
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment