Search code examples
pythonmatplotliblatex

matplotlib mathtext slanted fractions


Is it possible with matplotlib to create a slanted fractions ( /sfrac{1}{1} in Latex)

I tried r'$/sfrac{1}{2}$', but I get nothing...


Solution

  • You will have to use real LaTeX as follows:

    import matplotlib
    import matplotlib.pyplot as plt
    
    # Use LaTeX for rendering
    matplotlib.rcParams["text.usetex"] = True
    # load the xfrac package
    matplotlib.rcParams["text.latex.preamble"].append(r'\usepackage{xfrac}')
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    ax.plot([0,1],[1,0])
    ax.text(.5, .5, r'$\sfrac{1}{2}$')
    

    This creates:

    enter image description here

    You need to have a working LaTeX in your system plus of course the LaTeX xfrac package.