0

This is my second post as I am learning python and tried my hand at matplotlib. I used the following example from the matplot website, and it works fine (i.e. shows the graph) if I remove 3 after python from the shebang line in my plotexample.py file. I am on Ubuntu 12.04 and I ran the following commands on my terminal:

$chmod +x plotexample.py
$./plotexample.py

#!/usr/bin/env python3

import matplotlib
from numpy.random import randn
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter

def to_percent(y, position):
    # Ignore the passed in position. This has the effect of scaling the default
    # tick locations.
    s = str(100 * y)

    # The percent symbol needs escaping in latex
    if matplotlib.rcParams['text.usetex'] == True:
        return s + r'$\%$'
    else:
        return s + '%'

x = randn(5000)

# Make a normed histogram. It'll be multiplied by 100 later.
plt.hist(x, bins=50, normed=True)

# Create the formatter using the function to_percent. This multiplies all the
# default labels by 100, making them all percentages
formatter = FuncFormatter(to_percent)

# Set the formatter
plt.gca().yaxis.set_major_formatter(formatter)
plt.savefig("test.pdf") 
plt.show()

I looked at some of the other posts and reading from those, ran the below:

>>> import matplotlib
>>> matplotlib.get_backend()
'TkAgg'

I also installed tk-dev(8.5.0-2) from the package library, and retried everything but with no luck yet. How do I fix the show() issue as I don't want to do savefig() everytime.

I also have run the below to make sure I have the right installation (or so I think) and that installed fine, but does not solve the problem

sudo pip3 install matplotlib

As I await a response, I also tried the following from the python prompt:

>>> import matplotlib
>>> matplotlib.use('GTKAgg')
>>> 

So, I edited my file contents to read:

#!/usr/bin/env python3

import matplotlib
matplotlib.use('GTKAgg')
from numpy.random import randn
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter

But then on running the program, I get the following error:

Traceback (most recent call last):
  File "/usr/local/lib/python3.2/dist-packages/matplotlib-1.3.1-py3.2-linux-i686.egg/matplotlib/backends/backend_gtk.py", line 12, in <module>
    import gobject
ImportError: No module named gobject

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./plotexample.py", line 6, in <module>
    import matplotlib.pyplot as plt
  File "/usr/local/lib/python3.2/dist-packages/matplotlib-1.3.1-py3.2-linux-i686.egg/matplotlib/pyplot.py", line 98, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/usr/local/lib/python3.2/dist-packages/matplotlib-1.3.1-py3.2-linux-i686.egg/matplotlib/backends/__init__.py", line 28, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "/usr/local/lib/python3.2/dist-packages/matplotlib-1.3.1-py3.2-linux-i686.egg/matplotlib/backends/backend_gtkagg.py", line 10, in <module>
    from matplotlib.backends.backend_gtk import gtk, FigureManagerGTK, FigureCanvasGTK,\
  File "/usr/local/lib/python3.2/dist-packages/matplotlib-1.3.1-py3.2-linux-i686.egg/matplotlib/backends/backend_gtk.py", line 16, in <module>
    raise ImportError("Gtk* backend requires pygtk to be installed.")
ImportError: Gtk* backend requires pygtk to be installed.

I did some more googl'ing and find that there is no pygtk for Python3. Is that correct. How then do I fix this issue or am I stuck using the older version of Python if I want show() to work

I ran sudo pip3 install matplotlib after installing tk-dev, removing python3-tk and reinstalling python3-tk.

 Requirement already satisfied (use --upgrade to upgrade): matplotlib in /usr/local/lib/python3.2/dist-packages/matplotlib-1.3.1-py3.2-linux-i686.egg
    Requirement already satisfied (use --upgrade to upgrade): numpy>=1.5 in /usr/lib/python3/dist-packages (from matplotlib)
    Requirement already satisfied (use --upgrade to upgrade): python-dateutil in /usr/local/lib/python3.2/dist-packages/python_dateutil-2.2-py3.2.egg (from matplotlib)
    Requirement already satisfied (use --upgrade to upgrade): tornado in /usr/local/lib/python3.2/dist-packages (from matplotlib)
    Requirement already satisfied (use --upgrade to upgrade): pyparsing>=1.5.6,!=2.0.0 in /usr/local/lib/python3.2/dist-packages/pyparsing-2.0.2-py3.2.egg (from matplotlib)
    Requirement already satisfied (use --upgrade to upgrade): nose in /usr/local/lib/python3.2/dist-packages (from matplotlib)
    Requirement already satisfied (use --upgrade to upgrade): six in /usr/local/lib/python3.2/dist-packages/six-1.7.2-py3.2.egg (from python-dateutil->matplotlib)
    Cleaning up...
user3766123
  • 27
  • 1
  • 8
  • It's important to keep your question focused on relevant parts. Do you really need all that code in the beginning to demonstrate the problem? – Lev Levitsky Jun 24 '14 at 20:15
  • possible duplicate of [matplotlib wont draw python3](http://stackoverflow.com/questions/12948446/matplotlib-wont-draw-python3) – Jonathan Villemaire-Krajden Jun 24 '14 at 21:07
  • After installing tk-dev, you will need to install python-tk and rebuild matplotlib. Matplotlib "learns" about the backends when it is built. – Jonathan Villemaire-Krajden Jun 24 '14 at 21:10
  • I had python-tk installed and after reading your comment, I just removed it and reinstalled it. How do I rebuild matplotlib. I had initially run `sudo pip3 install matplotlib`, should I just re-run that command – user3766123 Jun 24 '14 at 21:35
  • I just updated my post with the message I get by running the install matplotlib after the python3-tk remove and install I just did – user3766123 Jun 24 '14 at 21:42
  • some kind human put a message up on my post showing me the link to a similar problem. Ironically, that is what I had read before I installed tk-dev. Now, I don't know how to rebuild matplotlib :-(, and I don't think it is fair for me to start another post asking that q?. Hopefully I can get a response on my original post itself :-) – user3766123 Jun 24 '14 at 21:53
  • try with --ignore-installed: `sudo pip3 install --ignore-installed matplotlib` – Jonathan Villemaire-Krajden Jun 24 '14 at 21:57
  • did that, it ran it's thing, this is the tail end ` `no previously-included directories found matching 'doc/.build' Installing nosetests script to /usr/local/bin Installing nosetests-3.2 script to /usr/local/bin Successfully installed matplotlib numpy python-dateutil tornado pyparsing nose six Cleaning up...` but show() still doesn't work. what am i missing – user3766123 Jun 24 '14 at 23:26
  • what is strange is that when I run the exact same code in Ipython Qt Console with the shebang line ending in `python3` it works fine. – user3766123 Jun 24 '14 at 23:31
  • I felt it necessary to mention this. I was misled here by @JonathanVillemaire-Krajden and following his instructions assuming he knew what he was talking about, has ruined my setup, so that what was working has stopped. If you don't know, don't suggest trial and errors to someone who is new and is seeking suggestions to fix stuff. There should be some way to mark his response as misleading. – user3766123 Jun 28 '14 at 18:12

0 Answers0