使用简单绘图脚本时出现属性错误



我正在尝试在Python中绘制一个简单的函数。我使用以下代码:

# lineplot.py
import numpy as np
import pylab as pl
# Make an array of x values
x = [1, 2, 3, 4, 5]
# Make an array of y values for each x value
y = [1, 4, 9, 16, 25]
# use pylab to plot x and y
pl.plot(x, y)
# show the plot on the screen
pl.show()

但是,我想查看输出的最后一行会导致以下错误:

AttributeError: 'NoneType' object has no attribute 'get_default_size'

有人知道我哪里出错了吗?

错误的完整回溯:

>>> pl.show()
Traceback (most recent call last):
    File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_gtk.py", line 394,    in expose_event
    self._render_figure(self._pixmap, w, h)
    File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_gtkagg.py", line 75, in _render_figure
    FigureCanvasAgg.draw(self)
    File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 394, in draw
    self.figure.draw(self.renderer)
    File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
    File "/usr/lib64/python2.7/site-packages/matplotlib/figure.py", line 798, in draw
    func(*args)
    File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
    File "/usr/lib64/python2.7/site-packages/matplotlib/axes.py", line 1946, in draw
    a.draw(renderer)
    File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
    File "/usr/lib64/python2.7/site-packages/matplotlib/axis.py", line 1017, in draw
    tick.draw(renderer)
    File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
    File "/usr/lib64/python2.7/site-packages/matplotlib/axis.py", line 234, in draw
    self.label1.draw(renderer)
    File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
    File "/usr/lib64/python2.7/site-packages/matplotlib/text.py", line 526, in draw
    bbox, info = self._get_layout(renderer)
    File "/usr/lib64/python2.7/site-packages/matplotlib/text.py", line 300, in _get_layout
    ismath=False)
    File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 180, in get_text_width_height_descent
    font = self._get_agg_font(prop)
    File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 226, in _get_agg_font
    size = prop.get_size_in_points()
    File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 798, in get_size_in_points
    default_size = fontManager.get_default_size()
    AttributeError: 'NoneType' object has no attribute 'get_default_size'
    Traceback (most recent call last):
    File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_gtk.py", line 394, in expose_event
    self._render_figure(self._pixmap, w, h)
    File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_gtkagg.py", line 75, in _render_figure
    FigureCanvasAgg.draw(self)
    File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 394, in draw
    self.figure.draw(self.renderer)
    File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
    File "/usr/lib64/python2.7/site-packages/matplotlib/figure.py", line 798, in draw
    func(*args)
    File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
    File "/usr/lib64/python2.7/site-packages/matplotlib/axes.py", line 1946, in draw
    a.draw(renderer)
    File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
    File "/usr/lib64/python2.7/site-packages/matplotlib/axis.py", line 1017, in draw
    tick.draw(renderer)
    File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
    File "/usr/lib64/python2.7/site-packages/matplotlib/axis.py", line 234, in draw
    self.label1.draw(renderer)
    File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
    File "/usr/lib64/python2.7/site-packages/matplotlib/text.py", line 526, in draw
    bbox, info = self._get_layout(renderer)
    File "/usr/lib64/python2.7/site-packages/matplotlib/text.py", line 300, in _get_layout
    ismath=False)
    File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 180, in get_text_width_height_descent
    font = self._get_agg_font(prop)
    File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 226, in _get_agg_font
    size = prop.get_size_in_points()
    File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 798, in get_size_in_points
    default_size = fontManager.get_default_size()
    AttributeError: 'NoneType' object has no attribute 'get_default_size'
    Traceback (most recent call last):
    File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_gtk.py", line 394, in expose_event
    self._render_figure(self._pixmap, w, h)
    File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_gtkagg.py", line 75, in _render_figure
    FigureCanvasAgg.draw(self)
    File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 394, in draw
    self.figure.draw(self.renderer)
    File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
    File "/usr/lib64/python2.7/site-packages/matplotlib/figure.py", line 798, in draw
    func(*args)
    File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
    File "/usr/lib64/python2.7/site-packages/matplotlib/axes.py", line 1946, in draw
    a.draw(renderer)
    File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
    File "/usr/lib64/python2.7/site-packages/matplotlib/axis.py", line 1017, in draw
    tick.draw(renderer)
    File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
    File "/usr/lib64/python2.7/site-packages/matplotlib/axis.py", line 234, in draw
    self.label1.draw(renderer)
    File "/usr/lib64/python2.7/site-packages/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
    File "/usr/lib64/python2.7/site-packages/matplotlib/text.py", line 526, in draw
    bbox, info = self._get_layout(renderer)
    File "/usr/lib64/python2.7/site-packages/matplotlib/text.py", line 300, in _get_layout
    ismath=False)
    File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 180, in get_text_width_height_descent
    font = self._get_agg_font(prop)
    File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_agg.py", line 226, in _get_agg_font
    size = prop.get_size_in_points()
    File "/usr/lib64/python2.7/site-packages/matplotlib/font_manager.py", line 798, in get_size_in_points
    default_size = fontManager.get_default_size()
    AttributeError: 'NoneType' object has no attribute 'get_default_size'

问题是OP使用的matplotlib版本(1.0.1)。 升级它解决了问题(正如聊天中的讨论所澄清的那样)。

这就是我们解决问题的方式(它应该在 Linux 上运行):

  1. 从下载页面下载最新的 matplotlib tar 文件
  2. 提取 TAR.gz存档
  3. CD 进入 matplotlib 目录
  4. python setup.py build
    

    如果它失败了,抱怨某些头文件(例如 png.h ) 从系统存储库安装相关的-devel软件包。当然需要python-devellibpng12-devel

  5. python setup.py install --user
    

    它将在$HOME/.local/lib/pythonX.X/site-packages中安装 matplotlib。此目录在任何系统范围的路径之前加载在 python 路径中。此外,没有弄乱系统范围安装的风险。

最新更新