Mr Squid
Mr Squid

Reputation: 1276

Python matplotlib savefig: Change font size

I have some figures that I have plotted using matplotlib, however the font size is too small when I use it in a latex document. I've tried to alter the font size using the following code:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.transforms
from matplotlib.colors import LogNorm

plt.rcParams['figure.figsize'] = (10.0, 7.0) # set default size of plots
font = {'family' : 'sans',
        'weight' : 'normal',
        'size'   : 40}
matplotlib.rc('font', **font)

#================= Draw histogram =====================
fig, ax = plt.subplots()
ax.hist2d(flow[:, 0], flow[:, 1], bins=400, norm = LogNorm(), cmap='viridis')
for xc in gt_points[:, 0]:
    ax.axvline(x=xc, linestyle=':', c='r')
    print(xc)
ax.axhline(0, linestyle=':', c='r')
ax.set_xlabel('vx [pixels per second]')
ax.set_ylabel('vy [pixels per second]')
ax.set_xlim([k_means_x_min, k_means_x_max])
ax.set_ylim([k_means_y_min, k_means_y_max])
fig.tight_layout()
plt.show()
fig.savefig('histogram.pdf', bbox_inches='tight', transparent=True)

Except changing the 'size' : 40 parameter doesn't change anything and the resulting pdf has loads of whitespace around it and isn't 'tight' at all (see image). Can anyone tell me what I'm doing wrong?

Many thanks!

The resulting pdf

Edit: Adding the lines matplotlib.rc('xtick', labelsize=20) matplotlib.rc('ytick', labelsize=20) works for at least making the numbers on x and y axis larger. However, as mentioned matplotlib.rc('font', **font) does nothing...

Upvotes: 2

Views: 8124

Answers (2)

Mr Squid
Mr Squid

Reputation: 1276

As per the documentation, this works:

font = {'family' : 'normal',
        'weight' : 'normal',
        'size'   : 22}
matplotlib.rc('font', **font)

I'm guessing the problem was that 'family' : 'sans' isn't recognised by my python.

Upvotes: 4

Sam Comber
Sam Comber

Reputation: 1293

You can change axes labels directly using this:

ax.set_ylabel('vx [pixels per second]', fontsize=40)

Upvotes: 1

Related Questions