我试图在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'
答案 0 :(得分:2)
问题是OP使用的matplotlib
版本(1.0.1)。升级它解决了问题(澄清了聊天中的讨论)。
这就是我们解决问题的方法(它应该适用于Linux):
matplotlib
tar文件
运行
python setup.py build
如果它抱怨某些头文件(例如png.h
)失败,请从系统存储库安装相关的-devel
包。确保需要python-devel
和libpng12-devel
运行
python setup.py install --user
它将在$HOME/.local/lib/pythonX.X/site-packages
中安装matplotlib。此目录在任何系统范围之前加载到python路径中。此外,不存在弄乱系统安装的风险。