Python中的饼图出现IOError: [Errno 13] 权限被拒绝

2 投票
1 回答
1139 浏览
提问于 2025-04-17 23:49

我正在尝试用Python画一个简单的饼图。我可以成功生成饼图,但标签却没有显示出来。

这是我的代码:

from pylab import *

frac = [10, 40, 15, 35]
labels = ['Part A', 'Part B', 'Part C', 'Part D']
explode = [0.03, 0.03, 0.03, 0.03]

# Create pie chart
pie(frac, explode, labels, shadow=True)
# Give it a title
title('Pie Chart')

# Save the plot to a PDF file
savefig('Pie Chart.pdf', dpi=200, format='PDF')

这是我在构建时遇到的错误:

Traceback (most recent call last):
  File "H:\Projects\Developing Quality Software\simple pie chart.py", line 13, in <module>
    savefig('Pie Chart.pdf', dpi=200, format='PDF')
  File "C:\PYTHON27\lib\site-packages\matplotlib\pyplot.py", line 561, in savefig
    return fig.savefig(*args, **kwargs)
  File "C:\PYTHON27\lib\site-packages\matplotlib\figure.py", line 1421, in savefig
    self.canvas.print_figure(*args, **kwargs)
  File "C:\PYTHON27\lib\site-packages\matplotlib\backends\backend_wxagg.py", line 85, in print_figure
    FigureCanvasAgg.print_figure(self, filename, *args, **kwargs)
  File "C:\PYTHON27\lib\site-packages\matplotlib\backend_bases.py", line 2220, in print_figure
    **kwargs)
  File "C:\PYTHON27\lib\site-packages\matplotlib\backend_bases.py", line 1952, in print_pdf
    return pdf.print_pdf(*args, **kwargs)
  File "C:\PYTHON27\lib\site-packages\matplotlib\backends\backend_pdf.py", line 2340, in print_pdf
    self.figure.draw(renderer)
  File "C:\PYTHON27\lib\site-packages\matplotlib\artist.py", line 54, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\PYTHON27\lib\site-packages\matplotlib\figure.py", line 1034, in draw
    func(*args)
  File "C:\PYTHON27\lib\site-packages\matplotlib\artist.py", line 54, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\PYTHON27\lib\site-packages\matplotlib\axes.py", line 2086, in draw
    a.draw(renderer)
  File "C:\PYTHON27\lib\site-packages\matplotlib\artist.py", line 54, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "C:\PYTHON27\lib\site-packages\matplotlib\text.py", line 547, in draw
    bbox, info, descent = self._get_layout(renderer)
  File "C:\PYTHON27\lib\site-packages\matplotlib\text.py", line 320, in _get_layout
    ismath=False)
  File "C:\PYTHON27\lib\site-packages\matplotlib\backends\backend_pdf.py", line 1965, in get_text_width_height_descent
    font = self._get_font_ttf(prop)
  File "C:\PYTHON27\lib\site-packages\matplotlib\backends\backend_pdf.py", line 1997, in _get_font_ttf
    filename = findfont(prop)
  File "C:\PYTHON27\lib\site-packages\matplotlib\font_manager.py", line 1362, in findfont
    font = fontManager.findfont(prop, **kw)
  File "C:\PYTHON27\lib\site-packages\matplotlib\font_manager.py", line 1258, in findfont
    _rebuild()
  File "C:\PYTHON27\lib\site-packages\matplotlib\font_manager.py", line 1343, in _rebuild
    pickle_dump(fontManager, _fmcache)
  File "C:\PYTHON27\lib\site-packages\matplotlib\font_manager.py", line 939, in pickle_dump
    with open(filename, 'wb') as fh:
IOError: [Errno 13] Permission denied: 'C:\\Users\\c1366554\\.matplotlib\\fontList.cache'
[Finished in 1.7s with exit code 1]

我看到很多关于IOError: [ErrNo13]的问题,所以如果答案在里面我很抱歉,但我看了所有的内容,还是不明白为什么会出现这个错误。我从来没有遇到过权限方面的问题。

如果我把标题和标签的部分注释掉,那么我就能顺利生成饼图的PDF,没有任何错误。我只需要一种方法来识别饼图的各个部分——无论是标签、图例还是其他什么。

1 个回答

0

我也遇到过同样的问题。我删除了错误信息中提到的那些缓存文件,然后就好了。再次运行脚本时,这些缓存文件又被重新创建了,所以没有问题了。

撰写回答