Skip to content

Instantly share code, notes, and snippets.

@gryan11
Last active February 19, 2021 13:15
Show Gist options
  • Save gryan11/d9a83b90c842d64de814c112f60899ba to your computer and use it in GitHub Desktop.
Save gryan11/d9a83b90c842d64de814c112f60899ba to your computer and use it in GitHub Desktop.
How to generate figures in colab, and avoiding type3 font errors on latex submissions.

import plotting libs

import numpy as np
import pandas as pd
import seaborn as sns

import matplotlib.font_manager as fm

import matplotlib
import matplotlib.pyplot as plt
from matplotlib import rc
from mpl_toolkits.mplot3d import Axes3D
import itertools

install latex to colab instance

! sudo apt-get install texlive-latex-recommended 
! sudo apt install texlive-latex-extra
! sudo apt install dvipng

download font files (some different options commented)

# !wget https://github.com/MaxGhenis/random/raw/master/Roboto-Regular.ttf
# !wget https://github.com/googlefonts/Inconsolata/raw/master/fonts/ttf/Inconsolata-Regular.ttf
# !wget https://github.com/googlefonts/opensans/raw/main/fonts/ttf/OpenSans-Regular.ttf

# !wget https://github.com/googlefonts/noto-fonts/raw/master/hinted/ttf/NotoSans/NotoSans-Regular.ttf
!wget https://github.com/googlefonts/noto-fonts/raw/master/hinted/ttf/NotoSans/NotoSans-Medium.ttf

# !wget https://github.com/googlefonts/noto-fonts/raw/master/hinted/ttf/NotoSansMono/NotoSansMono-Regular.ttf
!wget https://github.com/googlefonts/noto-fonts/raw/master/hinted/ttf/NotoSansMono/NotoSansMono-Medium.ttf

set pyplot font, use tex fonts (type=42)

# fm.fontManager.addfont('NotoSans-Regular.ttf')
fm.fontManager.addfont('NotoSans-Medium.ttf')
# fm.fontManager.addfont('NotoSansMono-Regular.ttf')
fm.fontManager.addfont('NotoSansMono-Medium.ttf')

rc('font', family='Noto Sans', weight='regular')
matplotlib.rcParams['pdf.fonttype'] = 42

Example title font, position:

def scope()
    marksize = 9
    figsize = (3.4, 2.2)
    fontsize = 13
    
    x = np.arange(-4, 5)

    y = x+1
    
    plt.figure(figsize=figsize)
    plt.plot(x, y, '.', markersize=marksize, color='k')
    plt.gca().spines['right'].set_visible(False)
    plt.gca().spines['top'].set_visible(False)
    plt.gca().spines['bottom'].set_linewidth(1.5)
    plt.gca().spines['left'].set_linewidth(1.5)
    plt.xticks([0,4,8], fontsize=fontsize)
    plt.yticks([1,5,9], fontsize=fontsize)
    plt.gca().set_xticklabels([0,4,8])
    plt.gca().set_yticklabels([1,5,9])

    plt.ylim([0,10])
    plt.xlim([-0.5,8.5])

    plt.title('y = x+1', fontsize=fontsize+2, 
              **{'fontname':'Noto Sans Mono','weight':'medium'},
              y=0.95)
    plt.xlabel('x (int)',**{'fontsize':fontsize+1,
                            'fontname':'Noto Sans', 'weight':'medium'})
    plt.ylabel('y (int)',**{'fontsize':fontsize+1,
                            'fontname':'Noto Sans', 'weight':'medium'})
    
    plt.tick_params(
        which='both', 
        bottom=True,      
        top=False,         
        left=True,
        right=False,
        labelbottom=True) 
        
    plt.tight_layout()
    
    plt.savefig('example1.pdf')
    
scope()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment