2

I have created fractional values for my x tick labels, but the fractions look a little cramped to me. Is there a way to increase the vertical spacing?

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.set_xlim(0, 10)
ax.set_xticklabels(
    ['$\\frac{{{0}}}{{{1}}}$'.format('M', x) for x in ax.get_xticks()],
    fontsize=12)

plt.show()

enter image description here

onewhaleid
  • 355
  • 9
  • 18
  • Does the following work any better for you? plt.rc('text', usetex=True) plt.rc('font', family='serif') ax.set_xticklabels( [r'$\frac{{{0}}}{{{1}}}$'.format('M', x) for x in ax.get_xticks()], fontsize=12) – Nipun Batra Jul 05 '17 at 05:44
  • @NipunBatra thank you, but that does not improve things. Also, I want my type to be san-serif. – onewhaleid Jul 06 '17 at 07:07

2 Answers2

1

Using \dfrac might do the trick. However, we would need to modify the preamble.

import matplotlib.pyplot as plt
plt.rc('text', usetex=True)
plt.rc('text.latex', preamble=r'\usepackage{amsmath}')
fig, ax = plt.subplots()

ax.set_xlim(0, 10)
ax.set_xticklabels(
    ['$\\dfrac{{{0}}}{{{1}}}$'.format('M', x) for x in ax.get_xticks()],
    fontsize=12)

enter image description here

Some more information here: How to write your own LaTeX preamble in Matplotlib?

Nipun Batra
  • 11,007
  • 11
  • 52
  • 77
1

The vertical spacing can be improved by forcing Matplotlib to use LaTeX rather than mathtext:

plt.rcParams['text.usetex'] = True
plt.rcParams['text.latex.preamble'] = [r'\usepackage{sansmath}', r'\sansmath']

The sansmath package retains the sans-serif type:

before: enter image description here after: enter image description here

onewhaleid
  • 355
  • 9
  • 18