Skip to content Skip to sidebar Skip to footer

Type 1 Fonts With Log Graphs

I'm trying to use Matplotlib graphs as part of a camera-ready submission, and the publishing house requires the use of Type 1 fonts only. I'm finding that the PDF backend happily o

Solution 1:

This is the code I use for camera-ready submissions:

from matplotlib import pyplot as plt

defSetPlotRC():
    #If fonttype = 1 doesn't work with LaTeX, try fonttype 42.
    plt.rc('pdf',fonttype = 1)
    plt.rc('ps',fonttype = 1)

defApplyFont(ax):

    ticks = ax.get_xticklabels() + ax.get_yticklabels()

    text_size = 14.0for t in ticks:
        t.set_fontname('Times New Roman')
        t.set_fontsize(text_size)

    txt = ax.get_xlabel()
    txt_obj = ax.set_xlabel(txt)
    txt_obj.set_fontname('Times New Roman')
    txt_obj.set_fontsize(text_size)

    txt = ax.get_ylabel()
    txt_obj = ax.set_ylabel(txt)
    txt_obj.set_fontname('Times New Roman')
    txt_obj.set_fontsize(text_size)

    txt = ax.get_title()
    txt_obj = ax.set_title(txt)
    txt_obj.set_fontname('Times New Roman')
    txt_obj.set_fontsize(text_size)

The fonts won't appear until you run savefig

Example:

import numpy as np

SetPlotRC()

t = np.arange(0, 2*np.pi, 0.01)
y = np.sin(t)

plt.plot(t,y)
plt.xlabel("Time")
plt.ylabel("Signal")
plt.title("Sine Wave")

ApplyFont(plt.gca())
plt.savefig("sine.pdf")

Solution 2:

The preferred method to get Type 1 fonts via matplotlib seems to be to use TeX for typesetting. Doing so results in all axis being typeset in the default math font, which is typically undesired but which can be avoided by using TeX-commands.

Long story short, I found this solution:

import matplotlib.pyplot as mp
import numpy as np

mp.rcParams['text.usetex'] = True#Let TeX do the typsetting
mp.rcParams['text.latex.preamble'] = [r'\usepackage{sansmath}', r'\sansmath'] #Force sans-serif math mode (for axes labels)
mp.rcParams['font.family'] = 'sans-serif'# ... for regular text
mp.rcParams['font.sans-serif'] = 'Helvetica, Avant Garde, Computer Modern Sans serif'# Choose a nice font here

fig = mp.figure()
dim = [0.1, 0.1, 0.8, 0.8]

ax = fig.add_axes(dim)
ax.text(0.001, 0.1, 'Sample Text')
ax.set_xlim(10**-4, 10**0)
ax.set_ylim(10**-2, 10**2)
ax.set_xscale("log")
ax.set_yscale("log")
ax.set_xlabel('$\mu_0$ (mA)')
ax.set_ylabel('R (m)')
t = np.arange(10**-4, 10**0, 10**-4)
y = 10*t

mp.plot(t,y)

mp.savefig('tmp.png', dpi=300)

Then results in this resulting image

Inspired by: https://stackoverflow.com/a/20709149/4189024 and http://wiki.scipy.org/Cookbook/Matplotlib/UsingTex

Post a Comment for "Type 1 Fonts With Log Graphs"