创建matplotlib图时“dvipng:not found”

时间:2014-12-27 14:41:14

标签: python matplotlib histogram

我尝试使用matplotlib绘制频率直方图,但它不起作用,我不知道问题出在哪里......

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

data = np.array([58.35, 71.83, 49.25, 38.89, 12.6, 58.34, 34.5, 11.6, 64.66, \
89.14, 101.84, 26.91, 38.74, 65.03, 35.23, 70.73, 54.52, 73.36, 74.35, \
60.54, 73.52, 24.58, 50.31, 55.63, 14.6, 53.64, 81.6])

fig = plt.figure()
ax = fig.add_subplot(1,1,1)

n, bins, patches=ax.hist(data, 10, facecolor='green', alpha=0.75)

ax.yaxis.set_major_formatter(ticker.FuncFormatter(lambda y, pos: ('%.2f')%(y*1e-3)))
ax.set_ylabel('Frequency (000s)')

plt.show()

错误消息的一部分:

sh: 1: dvipng: not found
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1535, in __call__
    return self.func(*args)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 276, in resize
    self.show()
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_tkagg.py", line 348, in draw
    FigureCanvasAgg.draw(self)
  File "/usr/lib/pymodules/python2.7/matplotlib/backends/backend_agg.py", line 451, in draw
    self.figure.draw(self.renderer)
  File "/usr/lib/pymodules/python2.7/matplotlib/artist.py", line 55, in draw_wrapper
    draw(artist, renderer, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/figure.py", line 1034, in draw
    func(*args)

...

2 个答案:

答案 0 :(得分:28)

在Ubuntu 14.04中,我使用此命令来解决问题:

sudo apt-get install dvipng

答案 1 :(得分:2)

看起来你的渲染器或后端有问题。您可能希望尝试使用不同的后端,方法是在代码的开头添加:

import matplotlib as mpl
mpl.use('macOsX')

对于其他渲染器,请参见此处: http://matplotlib.org/faq/usage_faq.html#what-is-a-backend