Set font for matplotlib ticks - matplotlib

I want the ticks in matplotlib to be typeset using Computer Modern sans-serif. How do I do this?
I would have expected this would do the trick, but it doesn't:
mpl.rcParams['font.family'] = 'computer modern sans serif'

Try this:
mpl.rcParams['font.family'] = 'sans-serif'
mpl.rcParams['font.sans-serif'] = ['computer modern']
The font.family property has five values:
'serif' (e.g., Times),
'sans-serif' (e.g., Helvetica),
'cursive' (e.g., Zapf-Chancery),
'fantasy' (e.g., Western), and
'monospace' (e.g., Courier).
After setting the font family you provide a list of fonts for matplotlib to try to find in order.

Related

How to set a default font weight for x & y labels with matplotlib

Using matplotlib 3.0.1, I had to make a figure where all text had to be in Open Sans Light. As the figure is quite complex, with several panels etc, I wanted to set that up using matplotlib.pyplot.rc(..) to define the default font and font weight for all elements of the figure.
The code below provides a minimal example. Because not everybody might have Open Sans installed, here I use the default font and the 'bold' weight, instead of 'light', to make it more general. So I set the default font weight, and size, then I plot a basic axis with x and y labels and some extra text.
import matplotlib.pyplot as plt
plt.style.use('default')
plt.rc('font', weight='bold', size=15)
fig, ax = plt.subplots(num=1, clear=True)
ax.set(xlabel='x-label', ylabel='y-label')
ax.text(0.2,0.4,'Text')
I expect all bits of text to be in 'bold' font weight. The tick labels and the extra bit of text are, but the x-label and y-label are not. They are in the normal weight. Screen capture here.
The x and y labels seem to get the default font name and size, but not the default weight.
As a workaround, I tried to set a default font weight in the 'axes' options, but that does not seem to be possible. "axes.labelsize" affects the font size of the x,y labels, but the font weight cannot be separately specified.
So it seems the font size is inherited by the x and y labels, if not otherwise specified through axes.labelsize, but the font weight is not inherited by the labels, nor can it be specified as a default for the axes. Note that the font name behaves yet differently. It is inherited by the x, y labels but cannot have a different default in the 'axes' option.
Is there a logic, or is that a bug?
Is there a solution to this? Or do I have to directly set the font weight onto the x-label and y-label objects?

matplotlib - savefig with usetex=True

I am having problems saving a figure created by matplotlib as a .eps or .ps when I enable usetex=True. This works when this is not enabled. Here's an example:
plt.plot([1,2,3], [1,2,3], 'b.')
plt.text(2,2,r'\textbf{(a)} \lambda_{1} value', usetex=True, fontsize=16, fontname='Times New Roman')
plt.savefig('check.eps')
I receive this error:
File "C:\Python27\lib\site-packages\matplotlib\backends\backend_ps.py", line 671, in draw_tex
thetext = 'psmarker%d' % self.textcnt
AttributeError: 'RendererPS' object has no attribute 'textcnt'
I also cannot set the font to Time New Roman using the text command when I enable usetex=True.
Even if it is a bit late, I would like to give an answer, if someone else searches for this error.
Saving eps files starts the RendererPS backend, which checks whether plt.rcParams['text.usetex'] is set to True to initialise its textcnt attribute. Since usetex is set only for the text object, the backend does not expect to have to handle LaTeX and throws an error when it tries to. These kind of inconsistencies concerning LaTeX rendering are sadly present in a number of places in matplotlib.
If you cannot achieve the text formatting with the standard matplotib functionality, one solution is to set usetex globally: plt.rcParams['text.usetex'] = True. This render all the text of the figure with LaTeX (e.g. also tick labels or axis labels). I would recommend to do this in any case, to have consistent visuals.
Concerning the font, the fontname argument only affects standard matplotlib formatting. For LaTeX rendering, you have to specify the font like you would do in LaTeX. To get a font like Times New Roman, you need to load the respective package e.g. mathptmx (the times package is deprecated) by plt.rcParams['text.latex.preamble'] = [r'\usepackage{mathptmx}']. Of course, the package has to be installed in your local LaTeX installation. The standard font family in matplotlib is sans-serif, so this has to be changed to serif with plt.rcParams['font.family'] = 'serif'.
The final code is:
import matplotlib.pyplot as plt
# LaTeX setup
plt.rcParams['text.latex.preamble'] = [r'\usepackage{mathptmx}'] # load times roman font
plt.rcParams['font.family'] = 'serif' # use serif font as default
plt.rcParams['text.usetex'] = True # enable LaTeX rendering globally
plt.plot([1,2,3], [1,2,3], 'b.')
plt.text(2,2,r'\textbf{(a)} $\lambda_1$ value', fontsize=16)
plt.savefig('check.eps')
Note, that you have to enclose the math variable in dollar signs $\lambda_1$, otherwise you get an error or warning in LaTeX. (Also, single character subscripts do not need to be enclosed in curly brackets).
As a side note: I encountered the same error, when trying to save a figure as eps after turning usetex off again.

Matplotlib need help using a downloaded font gill sans what is the font family

noob here. I can't figure out the correct matplotlib line to use my newly loaded gillius font.
So I installed gillius font of the arkandis digital foundry on my machine
ttf-adf-gillius
here is the place i "think" it put it
usr/share/fonts/truetype/adf/GilliusADF-Regular.otf: Gillius
ADF:style=Regular
SO if I want to use it in my Python 3.4 program that imports matplotlib. I set the font_family= 'Gillius" and the program doesn't find it. What do I set the font family to to use my font please?
here is an example message from my python program
Warning (from warnings module):
File
"/usr/lib/python3/dist-packages/matplotlib/font_manager.py", line 1279
(prop.get_family(), self.defaultFamily[fontext]))
UserWarning: findfont: Font
family ['Gillius'] not found. Falling back to Bitstream Vera
Sans
Warning (from warnings module):
File
"/usr/lib/python3/dist-packages/matplotlib/font_manager.py", line 1289
UserWarning)
UserWarning: findfont: Could not match :family=Bitstream Vera
Sans:style=normal:variant=normal:weight=normal:stretch=normal:size=20.0.
Returning /usr/share/matplotlib/mpl-data/fonts/ttf/cmmi10.ttf
I had already tried using the suggested link
How to use a (random) *.otf or *.ttf font in matplotlib?
but it didn't seem to do anything. My question is assuming the font is installed correctly what is the font family I should be entering to have matplotlib use it?
Ok I "think" this is a way to do it based on all the GREAT help I got on SO. thanks to all. I am new to matplotlib so if I screwed up happy to correct it.
import matplotlib
import matplotlib.font_manager as fm
from matplotlib import pyplot as plt
font = fm.FontProperties(
family = 'Gill Sans',
fname = '/usr/share/fonts/truetype/adf/GilliusADF-Regular.otf')
data = range(5)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.bar(data, data)
plt.ylabel('some y numbers')
plt.xlabel('some x numbers')
ax.set_yticklabels(ax.get_yticks(), fontproperties = font)
ax.set_xticklabels(ax.get_xticks(), fontproperties = font)
ax.set_title('this is a test of Gill sans font')
plt.show()

changing the font of the xthicklabels from a stylesheet in matplotlib

I want to use a non-standard font for labels on the axes of my matplotlib graph. I cannot assume users have installed the font, so I am providing the ttf file.
I have it working by doing the following:
fname = '/bla/bla/myfont.ttf'
ticks_font = matplotlib.font_manager.FontProperties(fname = fontPath, style='normal', size=12, weight='normal', stretch='normal')
for label in ax.get_xticklabels() :
label.set_fontproperties(ticks_font)
However, as I have many graphs with different styles, I want to move all layout to stylesheets. I've tried to do:
font.serif : '/bla/bla/myfont.ttf'
font.family :'serif'
But that doesn't seem to work.

Matplotlib estimates width of text wrong

When using Matplotlib (Pylab) for rendering Text with the same metrics as AriaL (e.g., Arial itself or Liberation Sans) output looks ok - e.g., the width of the legend box is right.
However, when using other fonts it seems that Matplotlib wrongly estimates the expected size of the text. For example, with Bitstream Vera Sans the text seems to be wider than expected by Matplotlib: E.g., the right-hand side of the text sometimes overlaps with the right-hand side of the legend's box.
Similiarly, when using use_latex to let Latex render the output the text seems to be narrower than expected, and there's an additional space between the right-hand side of the text and the legend's box.
Is this a bug in Matplotlib? Or am I expected to use fonts with the metrics of Arial?
I'm running matplotlib version 0.99.1.1 on linux (ubuntu 10.04) and cannot reproduce the problem. Here is the code I'm using.
import pylab
a = [1,2,1]
label = "This long label isn't too long."
for family in ['Arial', 'Liberation Sans', 'Bitstream Vera Sans']:
pylab.figure()
pylab.plot(a)
pylab.legend([label], prop={'family':family})
pylab.show()