Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Latex labels with seaborn

I'm trying to plot with seaborn.kdeplot with LaTeX labels. What I tried is the following:

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)

Running this I've got the following error:

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'

What could be the problem? The strange thing is that I got this error even if I remove the line adding the labels. What is the proper way of using LaTeX in seaborn plot labels?

like image 807
Beno94 Avatar asked Jul 18 '16 16:07

Beno94


1 Answers

If you do not have latex install on your machine, this could be causing errors.

For Linux users try installing Latex:

sudo apt update 

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

If in notebook, restart the Kernel, and then the font should display using Latex.

like image 105
Akin Wilson Avatar answered Oct 18 '22 03:10

Akin Wilson