I am trying to get user selected points (to get a polygon) from an image. I have already embedded a matplotlib.figure in a lot of my code, so I would MUCH prefer to use this style over pylab’s figure. I am trying to do the follow:
import pylab
from matplotlib.figure import Figure
x1 = pylab.rand(103, 53)
figure = Figure(figsize=(4, 4), dpi=100)
axes = figure.add_subplot(111)
axes.imshow(x1)
x = figure.ginput(2)
print(x)
But I get the following error:
Traceback (most recent call last):
File "ginput_demo.py", line 17, in <module>
x = figure.ginput(2)
File "C:\Python27\lib\site-packages\matplotlib\figure.py", line 1177, in ginpu
t
show_clicks=show_clicks)
File "C:\Python27\lib\site-packages\matplotlib\blocking_input.py", line 282, i
n __call__
BlockingInput.__call__(self,n=n,timeout=timeout)
File "C:\Python27\lib\site-packages\matplotlib\blocking_input.py", line 94, in
__call__
self.fig.show()
AttributeError: 'Figure' object has no attribute 'show'
The original pylab code that works that I am trying to more or less reproduce is from here:
import pylab
x1 = pylab.rand(103, 53)
fig1 = pylab.figure(1)
ax1 = fig1.add_subplot(111)
ax1.imshow(x1)
ax1.axis('image')
ax1.axis('off')
x = fig1.ginput(2)
fig1.show()
So basically, is there a way to get pylab.ginput to work with a matplotlib.figure or matplotlib.axes reference??
Thanks,
tylerthemiler
You should use
pylab.ginputinstead ofmyfigure.ginput.After changing this, you will realize that
axes.imshowis not plotting, you can fix it usingpylab.imshow.And finally you will find that after clicking and getting the position numbers, the figure disappears, so you want to add a
pylab.showat the end.This works, trying to follow as close as possible your prefered way of coding mpl:
I think the problem here comes from mixing different modules (coding styles) from matplotlib.
Your
myfigure.ginput()complaints about its Figure class not having ashowmethod. However it works withpylab.figure.ginput().In fact,
pylab.figure, that is actually the one defined in the pyplot module:although being of the class
matplotlib.figure.Figureis not the same as the Figure instancepyplot.figureimplements a couple of additional methods, one of themshow():that’s why you got the
AttributeErrorwith theFigureinstance.