5

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 e.g. legend(). The text vertical spaces are not updated. So, it should be something like getText().setTextSize().

Juha
  • 2,053
  • 23
  • 44

1 Answers1

-3

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 i.e. 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.

arose
  • 562
  • 1
  • 4
  • 13
  • How do I get the figsize in the same units as the text (inches => screen pixels)? I want the ratio = figwidth / textheight = 30. – Juha Jun 05 '12 at 12:45
  • ah, ok... It was set in the text... "When displaying to the screen, or creating an image (PNG) the pixel size of text and line widths, etc is determined by the dpi setting, which is set by Figure.set_dpi( val )". So, adding pylab.gcf().set_dpi(180) I can adjust the figure size on the screen. Could you add this to your solution and I will accept it. – Juha Jun 05 '12 at 13:02
  • hmm, still some problems... even if I set the dpi by hand, the text is not in points units... I mean, if the text size is 10 and dpi is 10, the text is not an inch high on the screen... How to fix it? – Juha Jun 05 '12 at 13:25