Programming Machine Learning: some plot files are throwing error

I got this error when executing the plot files on macOS Ventura 13.0.1 with Python 3.10.8 and matplotlib 3.6.1:

programming_ML/code/03_gradient:

$ python3 plot_loss_with_path.py
Traceback (most recent call last):
  File "/Users/germain/Code/lang/python/programming_ML/code/03_gradient/plot_loss_with_path.py", line 70, in <module>
    ax = plt.figure().gca(projection="3d")
TypeError: FigureBase.gca() got an unexpected keyword argument 'projection'
$ python3 plot_loss_with_slices.py
Traceback (most recent call last):
  File "/Users/germain/Code/lang/python/programming_ML/code/03_gradient/plot_loss_with_slices.py", line 70, in <module>
    ax = plt.figure().gca(projection="3d")
TypeError: FigureBase.gca() got an unexpected keyword argument 'projection'

programming_ML/code/04_hyperspace :

$ python3 plot_plane.py
Traceback (most recent call last):
  File "/Users/germain/Code/lang/python/programming_ML/code/04_hyperspace/plot_plane.py", line 17, in <module>
    ax = plt.figure().gca(projection="3d")
TypeError: FigureBase.gca() got an unexpected keyword argument 'projection'

There is a fix ?
tks˜

Hey, @ggerico. Did you follow the instructions to set up the right libraries? The ones in the book are potentially outdated, so I recommend these ones.

If you still have the problem after following the instructions on progml.com, then it’s something that I should look into. In that case, could you please let me know?

I have used the instruction Installing with Conda from progml.com/setup using the macOS package. I followed the bash commands you mentioned there, they worked well, except for numpy where I had to go to anaconda.org and used conda install -c cctbx202105 numpy instead.

Everything’s alright now, tks again!

Thank you for the info! I’ll update the setup instructions ASAP.

I had the same problem using matplotlib 3. 7. 2.
with

fig = plt.figure()
ax = fig.add_subplot(projection="3d")

instead of

ax = plt.figure().gca(projection="3d")

it works.

Thanks for all the additional code and explanations completing this excellent book!
Claudia

1 Like

Claudia, thank you for reporting this issue, and sorry that I took so long to reply! At this stage, it’s becoming hard for me to update the instructions for the book. I’ll add a note to the instructions on progml.com, and leave the fix for the next edition.