Matplotlib,如何禁用文本换行

时间:2016-12-17 23:37:41

标签: python matplotlib latex

我使用matplotlib渲染了这个乳胶表达式,但它已经包装了文本,因此给出了多行输出。

我希望输出看起来像这样: enter image description here

我设置wrap = False,但它仍然执行此操作

t = plt.text(0.5, 0.5, expr, fontsize=320, fontweight='bold', wrap=False, color='white',  horizontalalignment='center',verticalalignment='center')

我不确定为什么它仍然将它包装成3行。

作为参考,这是正在渲染的乳胶表达式。

$\equiv\ \frac{x^{3}}{3} + \frac{x^{2}}{2} \operatorname{asin}{\left (x \right )} + \frac{x^{2}}{2} + \frac{x}{4} \sqrt{- x^{2} + 1} + \begin{cases} 2 i \sqrt{x - 1} - 2 \log{\left (\sqrt{x} \right )} + \log{\left (x \right )} + 2 i \operatorname{asin}{\left (\frac{1}{\sqrt{x}} \right )} & \text{for}\: \left|{x}\right| > 1 \\2 \sqrt{- x + 1} + \log{\left (x \right )} - 2 \log{\left (\sqrt{- x + 1} + 1 \right )} & \text{otherwise} \end{cases} - \frac{1}{4} \operatorname{asin}{\left (x \right )}$

我如何获得理想的结果?

1 个答案:

答案 0 :(得分:0)

我删除了乳胶配方中equiv后面的反斜杠。 然后,使用我this github已经链接的previous question代码,我得到了所需的输出。

import matplotlib.pyplot as plt
import numpy as np
plt.rc('text', usetex=True)
plt.rcParams['text.latex.preamble'] = r'\usepackage{amsmath}'

def plot_equation(eq, fontsize=50, outfile=None, padding=0.1):
    """
    Function taken from
    https://gist.github.com/ahwillia/c7e54f875913ebc3de3852e9f51ccc69
    Plot an equation as a matplotlib figure.
    Parameters
    ----------
    eq : string
        The equation that you wish to plot. Should be plottable with
        latex. If `$` is included, they will be stripped.
    fontsize : number
        The fontsize passed to plt.text()
    outfile : string
        Name of the file to save the figure to.
    padding : float
        Amount of padding around the equation in inches.
    Returns
    -------
    ax : matplotlib axis
        The axis with your equation.
    """
    # clean equation string
    eq = eq.strip('$').replace(' ', '')

    # set up figure
    f = plt.figure()
    ax = plt.axes([0,0,1,1])    
    r = f.canvas.get_renderer()

    # display equation
    t = ax.text(0.5, 0.5, '${}$'.format(eq), fontsize=fontsize,
        horizontalalignment='center',verticalalignment='center')

    # resize figure to fit equation
    bb = t.get_window_extent(renderer=r)
    w,h = bb.width/f.dpi,np.ceil(bb.height/f.dpi)
    f.set_size_inches((padding+w,padding+h))

    # set axis limits so equation is centered
    plt.xlim([0,1])
    plt.ylim([0,1])
    ax.grid(False)
    ax.set_axis_off()

    if outfile is not None:
        plt.savefig(outfile)

    return ax

if __name__ == "__main__":
    plot_equation('x = \\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}',outfile=__file__[:-3]+"1.png",padding=0.1)

    eq =r" \equiv \frac{x^{3}}{3} + \frac{x^{2}}{2} \operatorname{asin}{\left (x \right )} + \frac{x^{2}}{2} + \frac{x}{4} \sqrt{- x^{2} + 1} + \begin{cases} 2 i \sqrt{x - 1} - 2 \log{\left (\sqrt{x} \right )} + \log{\left (x \right )} + 2 i \operatorname{asin}{\left (\frac{1}{\sqrt{x}} \right )} & \text{for}\: \left|{x}\right| > 1 \\2 \sqrt{- x + 1} + \log{\left (x \right )} - 2 \log{\left (\sqrt{- x + 1} + 1 \right )} & \text{otherwise} \end{cases} - \frac{1}{4} \operatorname{asin}{\left (x \right )}"
    plot_equation( eq ,outfile=__file__[:-3]+"2.png",padding=0.1, fontsize=10)

enter image description here