【问题标题】:Matplotlib, how to disable text wrappingMatplotlib,如何禁用文本换行
【发布时间】:2016-12-17 23:37:40
【问题描述】:

我已经使用 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 )}$

我怎样才能得到想要的结果?

【问题讨论】:

  • 您需要提供minimal reproducible exampleequiv 后面的反斜杠是什么意思?
  • equiv 前面的多余反斜杠已被删除,但它仍然不重要,乳胶表达式本身并不重要,问题在于 matplotlib 因为它正在对表达式进行文本包装,我不希望这种情况发生,所以我正在询问如何克服@Im
  • 在下面查看我的回答,了解如何克服这个问题。

标签: python matplotlib latex


【解决方案1】:

我删除了乳胶公式中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)

【讨论】:

  • 在我看来,关键在于 .format 部分。谢谢
  • .format() 不重要。我想关键是你应该将字体大小减少到一个合理的数字。
猜你喜欢
  • 1970-01-01
  • 2012-08-30
  • 2022-01-28
  • 1970-01-01
  • 2021-08-05
  • 1970-01-01
  • 1970-01-01
  • 2010-11-24
  • 2013-02-14
相关资源
最近更新 更多