用seaborn和latex误差绘制密度

2024-09-27 14:18:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用seaborn从一些数据绘制密度图

没有乳胶它可以很好地工作,但是当我尝试用乳胶绘图时,它给了我一个错误

以下是设置:

rcParams['text.usetex'] = True
rc('font',**{'family':'sans-serif','sans-serif':['Helvetica']})
rc('text', usetex=True)
plt.rcParams.update()
sns.set_context("paper")
sns.set(font_scale=1.5, rc={'text.usetex' : True})
sns.set_style('white', {'font.family':'serif', 'font.serif':'Times New Roman', 'background':'white'})
sns.despine()

图中所示为:

sns.kdeplot(np.array(data), bw=0.4, cut=0)
plt.tight_layout(True)
plt.savefig('file.png', dpi=300)

以下是错误:

  File "plotGraphs.py", line 214, in <module>
    plotCarsReached()
  File "plotGraphs.py", line 118, in plotCarsReached
    plt.tight_layout(True)
  File "/Users/iMac/.pyenv/versions/3.8.0/lib/python3.8/site-packages/matplotlib/pyplot.py", line 1337, in tight_layout
    gcf().tight_layout(pad=pad, h_pad=h_pad, w_pad=w_pad, rect=rect)
  File "/Users/iMac/.pyenv/versions/3.8.0/lib/python3.8/site-packages/matplotlib/cbook/deprecation.py", line 358, in wrapper
    return func(*args, **kwargs)
  File "/Users/iMac/.pyenv/versions/3.8.0/lib/python3.8/site-packages/matplotlib/figure.py", line 2491, in tight_layout
    kwargs = get_tight_layout_figure(
  File "/Users/iMac/.pyenv/versions/3.8.0/lib/python3.8/site-packages/matplotlib/tight_layout.py", line 355, in get_tight_layout_figure
    kwargs = auto_adjust_subplotpars(fig, renderer,
  File "/Users/iMac/.pyenv/versions/3.8.0/lib/python3.8/site-packages/matplotlib/tight_layout.py", line 109, in auto_adjust_subplotpars
    tight_bbox_raw = union([ax.get_tightbbox(renderer) for ax in subplots
  File "/Users/iMac/.pyenv/versions/3.8.0/lib/python3.8/site-packages/matplotlib/tight_layout.py", line 109, in <listcomp>
    tight_bbox_raw = union([ax.get_tightbbox(renderer) for ax in subplots
  File "/Users/iMac/.pyenv/versions/3.8.0/lib/python3.8/site-packages/matplotlib/axes/_base.py", line 4317, in get_tightbbox
    bb_xaxis = self.xaxis.get_tightbbox(renderer)
  File "/Users/iMac/.pyenv/versions/3.8.0/lib/python3.8/site-packages/matplotlib/axis.py", line 1197, in get_tightbbox
    bboxes = [
  File "/Users/iMac/.pyenv/versions/3.8.0/lib/python3.8/site-packages/matplotlib/axis.py", line 1198, in <genexpr>
    *(a.get_window_extent(renderer)
  File "/Users/iMac/.pyenv/versions/3.8.0/lib/python3.8/site-packages/matplotlib/text.py", line 905, in get_window_extent
    bbox, info, descent = self._get_layout(self._renderer)
  File "/Users/iMac/.pyenv/versions/3.8.0/lib/python3.8/site-packages/matplotlib/text.py", line 299, in _get_layout
    w, h, d = renderer.get_text_width_height_descent(
  File "/Users/iMac/.pyenv/versions/3.8.0/lib/python3.8/site-packages/matplotlib/backends/backend_agg.py", line 203, in get_text_width_height_descent
    w, h, d = texmanager.get_text_width_height_descent(
  File "/Users/iMac/.pyenv/versions/3.8.0/lib/python3.8/site-packages/matplotlib/texmanager.py", line 451, in get_text_width_height_descent
    with dviread.Dvi(dvifile, 72 * dpi_fraction) as dvi:
  File "/Users/iMac/.pyenv/versions/3.8.0/lib/python3.8/site-packages/matplotlib/dviread.py", line 198, in __init__
    self.file = open(filename, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: '/Users/iMac/.matplotlib/tex.cache/bff659c457d4aecfc8f90b11c18fa8b1.dvi'

如果我删除初始设置,绘图将正常工作。 其他使用matplotlib而不是seaborn的绘图也可以使用

有人知道怎么解决这个问题吗


Tags: inpypyenvgetmatplotliblibpackagesline
2条回答

出现错误是因为我在一个轴标签中有一个“%”,当我试图用latex打印它时,我没有将其更改为“\%”

我有同样的问题,但能够解决它。问题是python找不到latex和kpsewhich文件。我通过在Mac上执行以下步骤并使用PyCharm解决了此问题:

  1. 通过Matplotliblatex安装所有推荐的软件包

brew cask安装mactex ghostscript

  1. 在.bash\u配置文件的开头将latex bin目录路径手动添加到$PYTHONPATH

导出PYTHONPATH=“/usr/local/texlive/2019/bin/x86\u 64darwin:/usr/local/bin:$PYTHONPATH”

工作起来很有魅力-Matplotlib示例输出: Text rendering With LaTeX

黑客迫使PyCharm更新其环境 在“运行/编辑配置”中: 首先取消选中“将内容根添加到PYTHONPATH”,运行它,它会抱怨一些事情。。 然后重新检查,然后再次运行。这会触发有关目标环境中路径的某种更新

相关问题 更多 >

    热门问题