diedro
diedro

Reputation: 613

matplotlib set up font computer modern and bold

I would like to have a plot where the font are in "computer modern" (i.e. Latex style) but with x-ticks and y-ticks in bold.

Due to the recent upgrade of matplotlib my previous procedure does not work anymore.

This is my old procedure:

plt.rc('font', family='serif',size=24)
matplotlib.rc('text', usetex=True)
matplotlib.rc('legend', fontsize=24) 
matplotlib.rcParams['text.latex.preamble'] = [r'\boldmath']

This is the output message:

test_font.py:26: MatplotlibDeprecationWarning: Support for setting an rcParam that expects a str value to a non-str value is deprecated since 3.5 and support will be removed two minor releases later.
  matplotlib.rcParams['text.latex.preamble'] = [r'\boldmath']

I have decide that a possible solution could be to use the "computer modern" as font. This is my example:

import matplotlib
import matplotlib.pyplot as plt
import numpy as np


font = {'family' : 'serif',
        'weight' : 'bold',
        'size'   : 12
        }

matplotlib.rc('font', **font)


# Data for plotting
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)

fig, ax = plt.subplots(1,figsize=(9,6))

ax.plot(t, s)

ax.set(xlabel='time (s)  $a_1$', ylabel='voltage (mV)',
       title='About as simple as it gets, folks')
ax.grid()

fig.savefig("test.png")
plt.show()

This is the result:

enter image description here

I am not able, however, to set-up in font the font style.

I have tried to set the font family as "cmr10". This the code:

font = {'family' : 'serif',
         'weight' : 'bold',
         'size'   : 12,
         'serif':  'cmr10'
         }

matplotlib.rc('font', **font)

It seems that the "cmr10" makes disappear the bold option. Have I made some errors? Do you have in mind other possible solution?

Thanks

Upvotes: 4

Views: 3878

Answers (1)

mtzd
mtzd

Reputation: 789

You still can use your old procedure, but with a slight change. The MatplotlibDeprecationWarning that you get states that the parameter expects a str value but it's getting something else. In this case what is happening is that you are passing it as a list. Removing the brackets will do the trick:

import matplotlib
import matplotlib.pyplot as plt
import numpy as np

plt.rc('font', family='serif',size=24)
matplotlib.rc('text', usetex=True)
matplotlib.rc('legend', fontsize=24)
matplotlib.rcParams['text.latex.preamble'] = r'\boldmath'


# Data for plotting
t = np.arange(0.0, 2.0, 0.01)
s = 1 + np.sin(2 * np.pi * t)

fig, ax = plt.subplots(1,figsize=(9,6))

ax.plot(t, s)

ax.set(xlabel='time (s)  $a_1$', ylabel='voltage (mV)',
       title='About as simple as it gets, folks')
ax.grid()

fig.savefig("test.png")
plt.show()

The code above produces this plot without any errors:

enter image description here

Upvotes: 3

Related Questions