简体   繁体   中英

How to change all text sizes in figure when using python matplotlib?

I saw a long time ago a function that changed all text sizes to same in matplotlib. Now I can't find it anywhere. It was a simple one (or two) liner like:

for item in pylab.gca(): item.getLabel().setSize(10)

How should I do the above? The above is just pseudocode, but the idea is to change x and y labels, legends, titles, everything.

EDIT : ...inside one figure (object). I want the text size to be dependent on the figure width. The global font.size changes this for all figures? and I think it cannot be applied dynamically (settings are read only before the figure is created)?

EDIT 2 : I tested the font.size = 22 method. It has some weird behavior if you run it after eg legend(). The text vertical spaces are not updated. So, it should be something like getText().setTextSize().

Instead of changing the font size you could change the figsize (the font size stays the same):

# figsize = (8,6)
figsize = (4,3) # same ratio, bigger text
fig,(ax) = plt.subplots(1, 1, figsize=figsize)

EDIT (for completeness): The size of the pixels (on screen) is controlled by the dpi setting which is settable ie by Figure.set_dpi( val ).

EDIT 2 : I don't know how (and if) you can control the exact pixel height. I did some tests though:

#! /usr/bin/env python

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

x = np.arange(0, 2*np.pi, 0.1)
y = np.sin(x)

matplotlib.rcParams.update({'font.size': 10})
fig,(ax) = plt.subplots(1, 1, figsize=(10,10))
plt.plot(x,y)
plt.savefig('test.png', dpi=300)

# font-size, figsize, dpi => pixel-height
# 10, 20x20, 100 => 10
# 10, 20x20, 200 => 21
# 10, 10x10, 100 => 10
# 10, 10x10, 200 => 21
# 10, 10x10, 300 => 34

Also note that there are probably effects on the pixel height due to anti-aliasing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM