matplotlib错误:UserWarning:找不到rc文件

Gab*_*iel 0 python matplotlib python-2.7

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

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()
Run Code Online (Sandbox Code Playgroud)

如果我在我的机器上运行它,它会起作用,除了警告:

/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 "
Run Code Online (Sandbox Code Playgroud)

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

/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'
Run Code Online (Sandbox Code Playgroud)

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

Fra*_*ano 5

  1. 第一个警告与方式有关axes并且subplots已创建.axes创建时指定大小,同时subplots将轴放在常规网格中.

    所以tight_layout不能调整大小axes,就像它一样,subplots你得到警告.因此,使用axes,subplotstight_layout可能需要大量的调整中fig.add_axes([0.6, 0.12, 0.1, 0.02])

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

    然后将参数传递给os.path.joinwich期望字符串,而不是NoneType.

    您可以查看是否获取matplotlibrc文件并放入当前目录或者~/.config/matplotlib可以解决您的问题.

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

    python -c 'import matplotlib; print(matplotlib.__version__); print(matplotlib.__file__)'
    
    Run Code Online (Sandbox Code Playgroud)

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

fig = plt.figure(...)
ax0 = fig.add_subplot(121)
ax1 = fig.add_subplot(122)
Run Code Online (Sandbox Code Playgroud)

要么

fig, (ax0, ax1) = plt.subplots(nrows=1, ncols=2, ...)
Run Code Online (Sandbox Code Playgroud)