2016-12-17 30 views
0

Ich habe diesen Latexausdruck mit Matplotlib gerendert, aber es hat den Text eingewickelt und deshalb eine mehrzeilige Ausgabe gegeben.Matplotlib, wie man den Text einpackt

Ich möchte die Ausgabe wie dieser Stelle aussehen: enter image description here

I set wrap = Falsch, aber es hat immer noch diese

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

Ich bin nicht sicher, warum es wickelt es um Noch 3 Zeilen.

Als Referenz wird der Latex-Ausdruck gerendert.

$\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)}$ 

Wie würde ich das gewünschte Ergebnis erhalten?

+0

Sie benötigen einen [MCVE] zur Verfügung zu stellen. Was ist der Backslash hinter "equiv"? – ImportanceOfBeingErnest

+0

Der zusätzliche Backslash vor dem Äquivalenz wurde entfernt, aber es ist immer noch unwichtig, der Latex-Ausdruck selbst ist unwichtig, das Problem liegt in Matplotlib, da es den Ausdruck textwrapping ist, und ich will das nicht passieren, also frage ich wie Dies kann überwunden werden @Im –

+0

Siehe meine Antwort unten, wie Sie das zu überwinden. – ImportanceOfBeingErnest

Antwort

0

Ich entfernte den Backslash hinter equiv in der Latexformel. Dann benutze ich den Code von this github, mit dem ich schon in der previous question verbunden bin ich bekomme die gewünschte Ausgabe.

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

+0

Es sieht für mich so aus, als wäre der Schlüssel im .format-Teil. Danke –

+0

Das '.format()' ist unwichtig. Ich denke, der Schlüssel ist mehr, dass Sie die Schriftgröße auf eine vernünftige Zahl reduzieren sollten. – ImportanceOfBeingErnest

+0

Ja, ich habe gerade festgestellt, dass es die Schriftgröße ist, danke –

Verwandte Themen