change font name (under certain font.family)

Dear all,

I was trying to change all figure fonts to Arial, or Times, but without any luck.

below is the section I modify the property.

···

from numpy import *
from matplotlib import pyplot as plt
import pylab
params = {‘font.size’ : 16,
‘axes.labelsize’ : 16,
‘font.style’ : ‘normal’,

      'font.family' : 'sans-serif',
    'font.sans-serif' : 'Arial'

}
pylab.rcParams.update(params)


The font.family line seems to be working ( I get different fonts when I specify ‘sans-serif’ or ‘monospace’), but changing font.sans-serif has no effect at all. If the resulting figure is not changing, does it mean the font used is always the default in sans-serif family(Bitstream Vera Sans)? I wonder why matplotlib doesn’t use more common fonts as default, like Arial or Times which are accepted by most journals…

Thank you!

Yi (Miranda) Shang

PhD candidate

Graduate Program in Molecular and Cellular Biology

Stony Brook University

Yi,

This is just a guess, but I wonder if there might be a slight mistake on how you are specifying the font. If ‘font.family’ is set to ‘monospace’, then the font name has to be assigned to ‘font.monospace’, not ‘font.sans-serif’. Does that make a difference?

Ben Root

···

On Fri, Aug 27, 2010 at 2:33 PM, Yi Shang <mirandaisbest@…878…287…> wrote:

Dear all,

I was trying to change all figure fonts to Arial, or Times, but without any luck.

below is the section I modify the property.


from numpy import *
from matplotlib import pyplot as plt
import pylab
params = {‘font.size’ : 16,
‘axes.labelsize’ : 16,
‘font.style’ : ‘normal’,

      'font.family' : 'sans-serif',
    'font.sans-serif' : 'Arial'

}
pylab.rcParams.update(params)


The font.family line seems to be working ( I get different fonts when I specify ‘sans-serif’ or ‘monospace’), but changing font.sans-serif has no effect at all. If the resulting figure is not changing, does it mean the font used is always the default in sans-serif family(Bitstream Vera Sans)? I wonder why matplotlib doesn’t use more common fonts as default, like Arial or Times which are accepted by most journals…

Thank you!

Hi Ben,

Thanks for the reply. I did try to match font.family and font.$family and I am using matplotlib 0.99.0.

I did find something interesting…

For testing, I tried only using font.family line, (deleting font.$family line). I got reasonable font for “monospace”, “fantasy” “sans-serif” and “serif” family, but “cursive” looked exactly the same as “sans-serif”, which is the default font.family value.

Then, I added font.$family line. When font.family and font.$family match, as you suggested, I always get the default font as if I only specified font.family as sans-serif. {’‘font.family’ : ‘fantasy’, ‘font.fantasy’ : ‘Chicago’} gives me the default font, {’‘font.family’ : ‘fantasy’, ‘font.fantasy’ : ‘foo’} also gives me the default font ('Chicago is actually in ‘fantasy’ family, ‘foo’ is not).

Then I tried to mismatch font.family and font.$family, since they are not matching, font.$family is not taking effect, I am getting whatever font.family line is giving me.

I think the syntax I used may be wrong. Anyone has any idea how to specify a specific font name within a font family?

···

On Fri, Aug 27, 2010 at 4:13 PM, Benjamin Root <ben.root@…1304…> wrote:

On Fri, Aug 27, 2010 at 2:33 PM, Yi Shang <mirandaisbest@…1972…> wrote:

Dear all,

I was trying to change all figure fonts to Arial, or Times, but without any luck.

below is the section I modify the property.


from numpy import *
from matplotlib import pyplot as plt
import pylab
params = {‘font.size’ : 16,
‘axes.labelsize’ : 16,
‘font.style’ : ‘normal’,

      'font.family' : 'sans-serif',
    'font.sans-serif' : 'Arial'

}
pylab.rcParams.update(params)


The font.family line seems to be working ( I get different fonts when I specify ‘sans-serif’ or ‘monospace’), but changing font.sans-serif has no effect at all. If the resulting figure is not changing, does it mean the font used is always the default in sans-serif family(Bitstream Vera Sans)? I wonder why matplotlib doesn’t use more common fonts as default, like Arial or Times which are accepted by most journals…

Thank you!

Yi,

This is just a guess, but I wonder if there might be a slight mistake on how you are specifying the font. If ‘font.family’ is set to ‘monospace’, then the font name has to be assigned to ‘font.monospace’, not ‘font.sans-serif’. Does that make a difference?

Ben Root


Yi (Miranda) Shang

PhD candidate

Graduate Program in Molecular and Cellular Biology

Stony Brook University

From: Yi Shang [mailto:mirandaisbest@…287…]
Sent: Friday, August 27, 2010 17:33

Hi Ben,

Thanks for the reply. I did try to match font.family and font.$family and I am using matplotlib 0.99.0.

I did find something interesting…

For testing, I tried only using font.family line, (deleting font.$family line). I got reasonable font for “monospace”, “fantasy” “sans-serif” and “serif” family, but “cursive” looked exactly the same as “sans-serif”, which is the default font.family value.

Do you have any of the fonts in plt.rcParams[‘font.cursive’]? If not, matplotlib falls back to another font. You might have gotten a warning message of the form “… font_manager.py:1242: UserWarning: findfont: Font family [‘cursive’] not found. Falling back to Bitstream Vera Sans”.

Then, I added font.$family line. When font.family and font.$family match, as you suggested, I always get the default font as if I only specified font.family as sans-serif. {’‘font.family’ : ‘fantasy’, ‘font.fantasy’ : ‘Chicago’} gives me the default font, {’‘font.family’ : ‘fantasy’, ‘font.fantasy’ : ‘foo’} also gives me the default font ('Chicago is actually in ‘fantasy’ family, ‘foo’ is not).

Then I tried to mismatch font.family and font.$family, since they are not matching, font.$family is not taking effect, I am getting whatever font.family line is giving me.

I think the syntax I used may be wrong. Anyone has any idea how to specify a specific font name within a font family?

The font families font.sans-serif, font.serif, etc., must be lists of names of fonts, whereas font.family is a string naming either one of the families or a font. So, you could use either

params = {'font.family' : 'sans-serif', 'font.sans-serif' : ['Arial']}

or

params = {'font.family' : 'Arial'}

to specify a font.

From: Yi Shang [mailto:mirandaisbest@…287…]
Sent: Friday, August 27, 2010 15:34

from numpy import *
from matplotlib import pyplot as plt
import pylab
params = {‘font.size’ : 16,
‘axes.labelsize’ : 16,
‘font.style’ : ‘normal’,
‘font.family’ : ‘sans-serif’,
‘font.sans-serif’ : ‘Arial’
}
pylab.rcParams.update(params)

By the way, the update() method above bypasses the validation [1] provided by the RcParams class. Thus you must take care to give a list of font names for font.sans-serif and other font families. If you use

for (k, v) in {'font.sans-serif': 'Arial'}.items():
    plt.rcParams[k] = v

or

plt.rc('font', **{'sans-serif': 'Arial'})

then your parameters will be validated, a process that automatically encloses into a list font names given for families.

[1] http://matplotlib.sourceforge.net/users/customizing.html#dynamic-rc-settings