LateX文本渲染不适用于Matplotlib

时间:2018-08-22 00:10:44

标签: python matplotlib latex

我正在尝试使用此简单代码来显示包含变量及其值的数学表达式,但出现了一个大错误。看来Latex无法与Matplotlib一起使用。

看图片

代码:

import matplotlib.pyplot as plt
from matplotlib import rc   
rc('text', usetex=True)

formula = r'm=\frac{{w}}{{g}} = \frac{{{0}}}{"C:\Program Files (x86)\Python36-32\python.exe" 

"C:/Users/Techno/PycharmProjects/try/MathExpression .py"

Exception in Tkinter callback

Traceback (most recent call last):

  File "C:\Program Files (x86)\Python36-32\lib\tkinter\__init__.py", line 1702, in __call__
    return self.func(*args)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\backends\_backend_tk.py", line 227, in resize
    self.draw()

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 12, in draw
    super(FigureCanvasTkAgg, self).draw()

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\backends\backend_agg.py", line 433, in draw
    self.figure.draw(self.renderer)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\figure.py", line 1475, in draw
    renderer, self, artists, self.suppressComposite)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\image.py", line 141, in _draw_list_compositing_images
    a.draw(renderer)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\axes\_base.py", line 2607, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\image.py", line 141, in _draw_list_compositing_images
    a.draw(renderer)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\axis.py", line 1192, in draw
    renderer)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\axis.py", line 1130, in _get_tick_bboxes
    extent = tick.label1.get_window_extent(renderer)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\text.py", line 922, in get_window_extent
    bbox, info, descent = self._get_layout(self._renderer)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\text.py", line 309, in _get_layout
    ismath=ismath)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\backends\backend_agg.py", line 232, in get_text_width_height_descent
    s, fontsize, renderer=self)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\texmanager.py", line 501, in get_text_width_height_descent
    dvifile = self.make_dvi(tex, fontsize)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\texmanager.py", line 365, in make_dvi
    texfile], tex)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\texmanager.py", line 335, in _run_checked_subprocess
    stderr=subprocess.STDOUT)

  File "C:\Program Files (x86)\Python36-32\lib\subprocess.py", line 336, in check_output
    **kwargs).stdout

  File "C:\Program Files (x86)\Python36-32\lib\subprocess.py", line 403, in run
    with Popen(*popenargs, **kwargs) as process:
  File "C:\Program Files (x86)\Python36-32\lib\subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "C:\Program Files (x86)\Python36-32\lib\subprocess.py", line 997, in _execute_child
    startupinfo)

FileNotFoundError: [WinError 2] The system cannot find the file specified
Exception in Tkinter callback
Traceback (most recent call last):

  File "C:\Program Files (x86)\Python36-32\lib\tkinter\__init__.py", line 1702, in __call__
    return self.func(*args)

  File "C:\Program Files (x86)\Python36-32\lib\tkinter\__init__.py", line 746, in callit
    func(*args)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\backends\_backend_tk.py", line 310, in idle_draw
    self.draw()

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 12, in draw
    super(FigureCanvasTkAgg, self).draw()

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\backends\backend_agg.py", line 433, in draw
    self.figure.draw(self.renderer)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\figure.py", line 1475, in draw
    renderer, self, artists, self.suppressComposite)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\image.py", line 141, in _draw_list_compositing_images
    a.draw(renderer)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\axes\_base.py", line 2607, in draw
    mimage._draw_list_compositing_images(renderer, self, artists)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\image.py", line 141, in _draw_list_compositing_images
    a.draw(renderer)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
    return draw(artist, renderer, *args, **kwargs)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\axis.py", line 1192, in draw
    renderer)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\axis.py", line 1130, in _get_tick_bboxes
    extent = tick.label1.get_window_extent(renderer)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\text.py", line 922, in get_window_extent
    bbox, info, descent = self._get_layout(self._renderer)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\text.py", line 309, in _get_layout
    ismath=ismath)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\backends\backend_agg.py", line 232, in get_text_width_height_descent
    s, fontsize, renderer=self)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\texmanager.py", line 501, in get_text_width_height_descent
    dvifile = self.make_dvi(tex, fontsize)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\texmanager.py", line 365, in make_dvi
    texfile], tex)

  File "C:\Program Files (x86)\Python36-32\lib\site-packages\matplotlib\texmanager.py", line 335, in _run_checked_subprocess
    stderr=subprocess.STDOUT)

  File "C:\Program Files (x86)\Python36-32\lib\subprocess.py", line 336, in check_output
    **kwargs).stdout

  File "C:\Program Files (x86)\Python36-32\lib\subprocess.py", line 403, in run
    with Popen(*popenargs, **kwargs) as process:

  File "C:\Program Files (x86)\Python36-32\lib\subprocess.py", line 709, in __init__
    restore_signals, start_new_session)

  File "C:\Program Files (x86)\Python36-32\lib\subprocess.py", line 997, in _execute_child
    startupinfo)

FileNotFoundError: [WinError 2] The system cannot find the file specified

Process finished with exit code 0
}'.format(100,20)

plt.plot( [0,1,2,3], [0,1,2,3], '.')
plt.text(1,1,r'\[{}\]'.format(formula),fontsize=20)
plt.show()

这是错误:

package main
import (
    "fmt"
    "os"
    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/s3/s3manager"
)

const (
    AccessKeyId     = "XXXXXXXXXXXXXXXXXX"
    SecretAccessKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    Region          = "eu-west-1"
    Bucket          = "XXXXX-XXXX-XXX"
)

func main() {
    os.Setenv("AWS_ACCESS_KEY_ID",     AccessKeyId)
    os.Setenv("AWS_SECRET_ACCESS_KEY", SecretAccessKey)

    filename := os.Args[1]

    file, err := os.Open(filename)
    if err != nil {
        fmt.Println("Failed to open file", filename, err)
        os.Exit(1)
    }
    defer file.Close()

    conf := aws.Config{Region: aws.String(Region)}
    sess := session.New(&conf)

    svc := s3manager.NewUploader(sess)

    fmt.Println("Uploading file to S3...")
    result, err := svc.Upload(&s3manager.UploadInput{
        Bucket: aws.String(Bucket),
        Key:    aws.String(filepath.Base(filename)),
        Body:   file,
    })
    if err != nil {
        fmt.Println("error", err)
        os.Exit(1)
    }
}

1 个答案:

答案 0 :(得分:0)

我只是尝试运行您的代码,结果是这样的:

enter image description here

假设这是您想要的输出,这意味着您的代码编写正确,并且您的问题很可能与系统中Python的安装或配置方式有关。

当您的错误消息指出Tkinter出现问题时,图形后端很可能是罪魁祸首,因此这可能是开始调查的好地方。

在这里,我从Anaconda Distribution安装中运行64位Python 3.6.5,并将Spyder用作我的IDE。在Spyder中,我可以通过转到Tools-Preferences-IPython Console-Graphics-Graphics backend在Tkinter之间(以及其他)更改图形后端。我尝试在后端设置为Tkinterautomatic的情况下运行您的代码,在两种情况下都可以正常工作。我可以从您的屏幕截图中看到您没有使用Spyder,但是希望这可以帮助您找到自己的系统方式。