Seaborn中的Latex标签

9
我想使用seaborn.kdeplot绘制带有LaTeX标签的图形。我尝试了以下方法:
import numpy as np
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(font_scale=1.5, rc={'text.usetex' : True})

x = np.linspace(-50,50,100)
y = np.sin(x)**2/x

fig = plt.figure(1)
sns.set_style('white')
sns.kdeplot(np.array(y), label='hey')
fig.gca().set(xlabel=r'$e(t_0)$ [s]', ylabel='PDF')
fig.savefig("seaborntest.png", close = True, verbose = True)

我运行时遇到了以下错误:

Traceback (most recent call last):
  File "./sns_problem.py", line 17, in <module>
    fig.savefig("seaborntest.png", close = True, verbose = True)
  File "/usr/lib64/python2.7/site-packages/matplotlib/figure.py", line 1363, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "/usr/lib64/python2.7/site-packages/matplotlib/backend_bases.py", line 2093, in print_figure
    **kwargs)
  File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 491, in print_png
    FigureCanvasAgg.draw(self)
  File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 439, in draw
    self.figure.draw(self.renderer)
  File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 54, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib64/python2.7/site-packages/matplotlib/figure.py", line 999, in draw
    func(*args)
  File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 54, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib64/python2.7/site-packages/matplotlib/axes.py", line 2086, in draw
    a.draw(renderer)
  File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 54, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib64/python2.7/site-packages/matplotlib/axis.py", line 1050, in draw
    renderer)
  File "/usr/lib64/python2.7/site-packages/matplotlib/axis.py", line 999, in _get_tick_bboxes
    extent = tick.label1.get_window_extent(renderer)
  File "/usr/lib64/python2.7/site-packages/matplotlib/text.py", line 752, in get_window_extent
    bbox, info = self._get_layout(self._renderer)
  File "/usr/lib64/python2.7/site-packages/matplotlib/text.py", line 304, in _get_layout
    ismath=False)
  File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 196, in get_text_width_height_descent
    texmanager = self.get_texmanager()
  File "/usr/lib64/python2.7/site-packages/matplotlib/backend_bases.py", line 597, in get_texmanager
    self._texmanager = TexManager()
  File "/usr/lib64/python2.7/site-packages/matplotlib/texmanager.py", line 137, in __init__
    ff = rcParams['font.family'].lower()
AttributeError: 'list' object has no attribute 'lower'

什么可能是问题?奇怪的是,即使我删除添加标签的代码行,仍然会出现此错误。在 seaborn 绘图标签中使用 LaTeX 的正确方式是什么?

你的代码对我来说完美无缺。也许是你的LaTeX安装出了问题? - Serenity
谢谢,很高兴知道它对你有用。我认为这不是一个latex问题,因为我经常使用latex,没有注意到任何类似的情况。也许这可能是我的seaborn安装的问题。顺便说一下,我通过使用PyQt-Fit而不是seaborn来避免了这个问题。 - Beno94
我唯一看到的问题是图像底部有点被裁剪了。 - hrokr
对我来说也可以正常工作(python3,matplotlib 3.3.2,seaborn 0.11.0)。你的模块版本是什么?你尝试使用python3了吗? - mozway
1个回答

2

如果您的计算机上没有安装latex,这可能会导致错误。

对于Linux用户,请尝试安装Latex:

sudo apt update 

sudo apt-get install texlive-latex-extra texlive-fonts-recommended dvipng cm-super

如果在笔记本中,重启Kernel,然后字体应该可以使用Latex显示。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接