20

I try to plot a frequency histogram with matplotlib but it doesn t work and i don t know where is the problem...

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()

A part of the error message :

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)

...

7
  • 1
    "it doesn t work"... What doesn't work? Explain the problem. Do you get an error? An unexpected result? Dec 27, 2014 at 14:45
  • 1
    Sorry for the lack of explanations...It crashs and I get : sh: 1: dvipng: not found Exception in Tkinter callback Dec 27, 2014 at 14:46
  • There must be something wrong either with how you're running the code, or how you've installed it. It runs fine for me on Mac OS X using Python 3.4 and matplotlib 1.4.2.
    – Ffisegydd
    Dec 27, 2014 at 14:52
  • 2
    @user3601754, please edit the whole error message into your question. The exact error will help us to understand your problem and others when trying to find a solution for a similar problem.
    – cel
    Dec 27, 2014 at 14:52
  • 1
    @user3601754 I don't see how it comes to call dvipng but if you're on linux you can install dvipng in 30" and see if your problem goes away
    – gboffi
    Dec 27, 2014 at 14:54

4 Answers 4

50

In Ubuntu 14.04 I used this command to solve the problem:

sudo apt-get install dvipng
3
  • 5
    Thanks. Works for Ubuntu 18.04 LTS, too.
    – mab
    Jan 7, 2019 at 16:02
  • 1
    I don't have sudo permission. what can I do?
    – gota
    Apr 29, 2020 at 9:00
  • usually you need sudo permission to install something. try sudo -i to become root, after that try apt-get install dvipng Apr 29, 2020 at 17:22
4

It looks like you are having a problem with the renderer or backend. You might want to try a different backend, by adding this at the beginning of your code:

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

For other renderers, see here: http://matplotlib.org/faq/usage_faq.html#what-is-a-backend

1

I found that if you have some Latex distr. installed you may also go with:

 sudo tlmgr install dvipng

This is especially helpful for Mac, alternatively you can use ports:

sudo port install dvipng
3
  • With macports, I get an "Error: Port dvipng not found".
    – pfincent
    Dec 29, 2021 at 12:19
  • 1
    @pfincent interesting, according to this: ports.macports.org/port/dvipng , it should be available. Maybe try updating the ports sudo port -v selfupdate?
    – LemurPwned
    Dec 29, 2021 at 16:10
  • I had just installed macports a few minutes earlier before trying to download dvipng, so I didn't assume that was necessary, but it did indeed fix the problem. Thanks!
    – pfincent
    Dec 29, 2021 at 17:59
0

For MacOS add this to your code!

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.