2

I want to change the font and the size of one of the labels of a pie chart. The font is called 'Berkelium Type' and according to the fontList.cache file in my .matplotlib directory Matplotlib could load it.

The code is

import matplotlib.pyplot as plt

count  = (1., 2., 3.)
labels = ('Text1', 'Text2', 'Text3')

pie = plt.pie(count, labels=labels)
pie[1][0].set_fontsize(24)
pie[1][0].set_family('Berkelium Type')

plt.axis('equal')
plt.draw()
plt.show()

Changing the font size worked so far. However, it did not change the font itself.

I did a test with a simple text

test = plt.text(0.5, 0.5, 'This is a test.')

Neither

test.set_family('serif')

nor

test.set_family('sans-serif')

has any effect on the text.

So I'm wondering if I'm doing something conceptionally wrong here. Is this the right approach to change the font of a text?

Update:

I did exactly what this post is suggesting:

import matplotlib.font_manager as fm
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot(range(10))

prop = fm.FontProperties(fname='/home/sebastian/.local/share/font/kkberktp.ttf')
ax.set_title('This is some random font', fontproperties=prop, size=32)

plt.show()

As a result I get this:

Result of the script.

This is not the font I've chosen.

Could there be anything that is overwriting my font settings?

The font is available here.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sebastian
  • 755
  • 3
  • 7
  • 22

2 Answers2

1

To change the font globally, you can set this using rcParams. Place this line just after the matplotlib import:

plt.rcParams['font.family'] = 'Berkelium Type'

To just change the font for the pie chart, you can use the textprops option to pie:

pie = plt.pie(count, labels=labels, textprops={'family':'Berkelium Type'})

To only change the font to one of the text objects, you can use update:

pie[1][0].update({'family':'Berkelium Type'})
tmdavison
  • 64,360
  • 12
  • 187
  • 165
  • While this works now for 'serif' and 'sans-serif', it still doesn't work for 'Berkelium Type' or any other font I have. Furthermore, I do not want to change the font globally, but only for certain labels. – Sebastian Feb 15 '16 at 13:49
  • OK, see my edit with a couple of other options. Both work for me with a font I know I have (e.g. Times New Roman). I don't appear to have Berkelium Type on my system, so I can't check that, but if you have it installed this should work... – tmdavison Feb 15 '16 at 13:55
  • This is weird. I can change other parameters like size and color of the text with the update function. But 'family' has no effect. Neither with 'sans-serif' nor with any font. – Sebastian Feb 15 '16 at 14:11
-2

Solved it: text.usetex has to be false:

rc('text', usetex=False)
Sebastian
  • 755
  • 3
  • 7
  • 22