2

I'm currently reading "Machine Learning in Action". In the chapter 8 of Regression, p.158 , there are few codes to plot original data point and a fitted line together.

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(xMat[:,1].flatten().A[0] , yMat.T[:, 0].flatten().A[0])

xCopy = xMat.copy()
xCopy.sort(0)
yHat = xCopy*ws
ax.plot(xCopy[:,1], yHat)
plt.show()

xCopy and yHat are both matrix object defined by numpy.

When I use Python 3.2, running this code throws out error:

Traceback (most recent call last):
  File "F:\ML\AC\Regression.py", line 44, in <module>
    ax.plot(xCopy[:,1], yHat)
  File "C:\Python32\lib\site-packages\matplotlib\axes.py", line 3998, in plot
    for line in self._get_lines(*args, **kwargs):
  File "C:\Python32\lib\site-packages\matplotlib\axes.py", line 332, in _grab_next_args
    for seg in self._plot_args(remaining, kwargs):
  File "C:\Python32\lib\site-packages\matplotlib\axes.py", line 291, in _plot_args
    linestyle, marker, color = _process_plot_format(tup[-1])
  File "C:\Python32\lib\site-packages\matplotlib\axes.py", line 99, in _process_plot_format
    if fmt.find('--')>=0:
AttributeError: 'matrix' object has no attribute 'find'

But using Python 2.7, the exact same code clip works fine. Is there something difference between numpy for 2.7 and 3.2, or the matplotlib library has changed syntex from 2.7 to Python 3?

3
  • matplotlib is developed in 2.7 and the python3 version is generated via 2to3, so the interface will not change. Could you add some data to make this runable? Commented May 1, 2013 at 13:33
  • It looks like a bug with how plot splits up the arguments you give it. You should open a github issue with this. Commented May 1, 2013 at 14:09
  • The data source is available here: manning.com/pharrington/MLiA_SourceCode.zip Commented May 1, 2013 at 15:44

1 Answer 1

2

Python 3 support for matplotlib is very new, so you may experience a few bugs - and this is one of them.

As you've realised, numpy and matplotlib support for python 2.X is good, so I would stick with that if you don't want to encounter more hidden 'features'.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.