Skip to content Skip to sidebar Skip to footer

Customize Font When Using Style Sheet

I am using the 'ggplot' style sheet. Now, the style is great, except that I would like to specifically change the font style. Is that possible? If found the documentation about cus

Solution 1:

Combining styles

I think that the most elegant is to combine styles.

For example, you could define your own font settings in mystyle.mplstyle (see below where to save it, and what it could look like). To get the ggplot style with your own font settings you would then only have to specify:

plt.style.use(['ggplot', 'mystyle'])

This solution is elegant, because it allows consistent application in all your plots and allows you to mix-and-match.

Where to save your custom style?

Taken from one of my own styles mystyle.mplstyle could have the following entries (you should customise to your need obviously):

font.family  :seriffont.serif   :CMUSeriffont.weight  :boldfont.size    :18text.usetex  :true

Which you should save it matplotlib's configuration directory. For me this is ~/.matplotlib/stylelib/, but use

import matplotlib
matplotlib.get_configdir()

to find out what to use on your operating system. See documentation. You could also write a Python function to install in the right location.

Where to find existing styles?

Then the final part of your question. First, it is usefull to know that you can obtain a list with available styles using

import matplotlib.pyplotas plt
plt.style.available

See the documentation for a graphical representation.

How to inspect for example ggplot.mplstyle? I think that the best reference in matplotlib's source. You can also find the *.mplstyle files on your system. Where, however, depends on your operating system and installation. For me

find / -iname 'ggplot.mplstyle'2>/dev/null

gives

/usr/local/lib/python3.7/site-packages/matplotlib/mpl-data/stylelib/ggplot.mplstyle

Or more generally you could search for all styles:

find / -iname '*.mplstyle'2>/dev/null

For Windows I am not really an expert, but maybe the file-paths that were listed above give you a clue where to look.

Python script to install style in the right location

To install your custom styles in the right location, you could build a script that looks something like:

defcopy_style():

    import os
    import matplotlib

    # style definition(s)

    styles = {}

    styles['mystyle.mplstyle'] = '''
font.family          : serif
'''# write style definitions# directory name where the styles are stored
    dirname = os.path.abspath(os.path.join(matplotlib.get_configdir(), 'stylelib'))

    # make directory if it does not yet existifnot os.path.isdir(dirname): os.makedirs(dirname)

    # write all stylesfor fname, style in styles.items():
        open(os.path.join(dirname, fname),'w').write(style)

Solution 2:

Yes, it is possible. And you can do it either locally by passing the font to individual labels

font = {'fontname':'your font'}
plt.xlabel('xlabel', **hfont)
plt.ylabel('xlabel', **hfont)

or globally

import matplotlib.pyplotas plt
 plt.rcParams['font.family'] = 'your font'

Solution 3:

Try rcParams

matplotlib.rcParams.update({'font.size': 12})

Read more matplotlib

Post a Comment for "Customize Font When Using Style Sheet"