[英]Matplotlib, how to disable text wrapping
我已經使用matplotlib渲染了這個乳膠表達式,但是它包裝了文本,因此給出了多行輸出。
我設置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 )}$
我如何獲得理想的結果?
我刪除了乳膠配方中equiv
后面的反斜杠。 然后,使用我在上一個問題中已鏈接到的github中的代碼,獲得所需的輸出。
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.