【问题标题】:matplotlib error: UserWarning: could not find rc filematplotlib 错误:用户警告:找不到 rc 文件
【发布时间】:2013-10-18 04:42:10
【问题描述】:

我有以下非常简单的代码:

import matplotlib.pyplot as plt 
import numpy as np
import matplotlib.gridspec as gridspec

x = np.random.randn(60) 
y = np.random.randn(60)
z = [np.random.random() for _ in range(60)]

fig = plt.figure()
gs = gridspec.GridSpec(1, 2)

ax0 = plt.subplot(gs[0, 0])
plt.scatter(x, y, s=20)

ax1 = plt.subplot(gs[0, 1])
cm = plt.cm.get_cmap('RdYlBu_r')
plt.scatter(x, y, s=20 ,c=z, cmap=cm, vmin=0, vmax=1)
cbaxes = fig.add_axes([0.6, 0.12, 0.1, 0.02]) 
plt.colorbar(cax=cbaxes, ticks=[0.,1], orientation='horizontal')

fig.tight_layout()

out_png = '/home/user/image_out.png'
plt.savefig(out_png, dpi=150)
plt.close()

如果我在我的机器上运行它,除了警告之外它可以工作:

/usr/local/lib/python2.7/dist-packages/matplotlib/figure.py:1533: UserWarning: This figure includes Axes that are not compatible with tight_layout, so its results might be incorrect.
  warnings.warn("This figure includes Axes that are not "

但是如果我在集群上运行它会退出并出现以下错误:

/usr/lib/pymodules/python2.7/matplotlib/__init__.py:611: UserWarning: Could not find matplotlibrc; using defaults
  warnings.warn('Could not find matplotlibrc; using defaults')
/usr/lib/pymodules/python2.7/matplotlib/__init__.py:698: UserWarning: could not find rc file; returning defaults
  warnings.warn(message)
Traceback (most recent call last):
  File "colorbar.py", line 7, in <module>
    import matplotlib.pyplot as plt 
  File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 23, in <module>
    from matplotlib.figure import Figure, figaspect
  File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 18, in <module>
    from axes import Axes, SubplotBase, subplot_class_factory
  File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 14, in <module>
    import matplotlib.axis as maxis
  File "/usr/lib/pymodules/python2.7/matplotlib/axis.py", line 10, in <module>
    import matplotlib.font_manager as font_manager
  File "/usr/lib/pymodules/python2.7/matplotlib/font_manager.py", line 1325, in <module>
    _rebuild()
  File "/usr/lib/pymodules/python2.7/matplotlib/font_manager.py", line 1275, in _rebuild
    fontManager = FontManager()
  File "/usr/lib/pymodules/python2.7/matplotlib/font_manager.py", line 962, in __init__
    paths = [os.path.join(rcParams['datapath'], 'fonts', 'ttf'),
  File "/usr/lib/python2.7/posixpath.py", line 77, in join
    elif path == '' or path.endswith('/'):
AttributeError: 'NoneType' object has no attribute 'endswith'

发生了什么,我该如何解决?

【问题讨论】:

  • 第一件事:不要混合使用pyplot和OO接口。我怀疑这是您问题的原因,但它会产生不必要的影响。例如:ax0 = fig.add_subplot(gs[0, 0]); ax0.scatter(x, y, s=20)
  • 顺便说一句:这两个警告是不同的问题,标题只与其中一个有关
  • 嗨 Francesco,第一个警告真的不重要,因为代码可以正常工作。这是我需要解决的第二个问题。
  • @FrancescoMontesano 我非常不同意,混合状态机和 OO 风格很好(并且可以让你的生活更轻松)。绝大多数plt.*() 电话都归结为gca().*(); plt.draw_when_idle(),所以我认为您正在做出区分而没有区别。
  • @FrancescoMontesano 我也猜你会大量使用plt.figure(),而不是直接与图形管理器或Figure 直接打交道。我同意使用 OO 风格更容易阅读、更便携、几乎在所有方面都更好,但我不会阻止在适当的时候混合和匹配接口(就像你在回答 plt.figureplt.subplots 时所做的那样;) )

标签: python python-2.7 matplotlib


【解决方案1】:
  1. 第一个警告与axessubplots 的创建方式有关。 axes 是指定大小创建的,而 subplots 将轴放置在规则网格中。

    所以tight_layout 不能调整axes 的大小,就像subplots 一样,你会收到警告。因此使用axessubplotstight_layout 可能需要对fig.add_axes([0.6, 0.12, 0.1, 0.02]) 进行大量调整

  2. 在我看来,您在集群上运行时遇到的错误与 matplotlib 安装的一些问题有关。警告就是这样:matplotlib 在任何standard places 中都找不到任何matplotlibrc 文件,因此它回退到默认(硬编码)参数。然后它期望找到一个参数'datapath'。但是不存在(或错误地定义为None)所以rcParams['datapath'] 返回None

    然后将参数传递给os.path.join,它需要字符串,而不是NoneType

    您可以查看getting a matplotlibrc file 并放入您的当前目录或~/.config/matplotlib 是否可以解决您的问题。

    您应该做的另一件事是检查 matplotlib 的版本,以及您运行的版本是否是您认为正在使用的版本

    python -c 'import matplotlib; print(matplotlib.__version__); print(matplotlib.__file__)'
    

ps:我认为你根本不需要gs = gridspec.GridSpec(1, 2),除非你用轴做一些额外的事情。 如果你这样做,你会得到完全相同的答案

fig = plt.figure(...)
ax0 = fig.add_subplot(121)
ax1 = fig.add_subplot(122)

fig, (ax0, ax1) = plt.subplots(nrows=1, ncols=2, ...)

【讨论】:

  • 添加一个matplotlibrc 文件似乎已经解决了这个问题,但现在我得到了另一个问题,可能与我使用版本 1.2.1 并且集群安装了版本 1.1.1rc2 的事实有关.无论如何,我将此答案标记为已接受,因为它正确解决了问题。谢谢弗朗西斯科!
  • 谢谢。可能是在1.1.1rc2datapathmatplotlib/font_manager.py 中需要的关键字,但在默认的rc 参数中没有添加。祝新问题好运。
猜你喜欢
  • 1970-01-01
  • 2015-07-23
  • 1970-01-01
  • 2021-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-08
相关资源
最近更新 更多