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...