Display same figure with changing lines color and math fonts


The example of the result (computer modern) is:



This page show how to display several font all at once.
Computer modern, stix (Times New Roman), arial, cambria and arial (with colored line) are displayed.
In [1]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
xx = np.linspace(0,2,10)
y1 = xx
y2 = xx**2
y3 = xx**0.5
In [3]:
fonts1 = ["Times New Roman","Times New Roman","Arial","Times New Roman","Arial"]
fonts2 = ["cm","stix","dejavusans","custom","dejavusans"]
fonts3 = ["cm","times","arial","cambria","arial_color"]
colors = [ ["k"]*3 for i in range(4) ]
colors.append(["g","b","r"])
for font1,font2,font3,color in zip(fonts1,fonts2,fonts3,colors):
    plt.figure(figsize=(4,3),facecolor="w")
    plt.rcParams["mathtext.fontset"] = font2
    if font2 == "custom":
        plt.rcParams['mathtext.default'] = 'it'
        plt.rcParams["mathtext.it"] = "Cambria:italic"
        plt.rcParams["mathtext.bf"] = "Cambria:italic"
        plt.rcParams["mathtext.rm"] = "Times New Roman"
        plt.rcParams["mathtext.sf"] = "Cambria:italic"
        plt.rcParams["mathtext.tt"] = "Times New Roman" # 添え字が太字になってしまったのでttで回避する
    plt.rcParams["font.family"] = font1
    plt.plot(xx,y1,color=color[0],label="$x$")
    plt.plot(xx,y2,color=color[1],label="$x^2$")
    plt.plot(xx,y3,color=color[2],label="$\sqrt{x}$")
    plt.legend()
    plt.savefig("hoge_%s.png"%(font3),bbox_inches="tight",pad_inches=0.02)
    plt.show()