乳胶标签与seaborn

时间:2016-07-18 16:09:10

标签: python plot labels seaborn

我试图使用带有LaTeX标签的seaborn.kdeplot进行绘图。我尝试的是以下内容:

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的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

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

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

sudo apt update 

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

如果在 notebook 中,请重启 Kernel,然后字体应该使用 Latex 显示。