TypeError: plot_function() got an unexpected keyword argument 'ylim'

I am following along the lesson 3 code but I am getting this issue

plot_function(
    f,
    tx=None,
    ty=None,
    title=None,
    min=-2,
    max=2,
    figsize=(6, 4),
)

Error Message is written below

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File ~\anaconda3\envs\torch\lib\site-packages\ipywidgets\widgets\interaction.py:257, in interactive.update(self, *args)
    255     value = widget.get_interact_value()
    256     self.kwargs[widget._kwarg] = value
--> 257 self.result = self.f(**self.kwargs)
    258 show_inline_matplotlib_plots()
    259 if self.auto_display and self.result is not None:

Input In [10], in plot_quad(a, b, c)
      2 @interact(a = 1.5 , b = 1.5 , c = 1.5)
      3 def plot_quad(a , b , c):
      4     plt.scatter(x, y)
----> 5     plot_function(mk_quad(a , b , c), ylim = (-3,12))

TypeError: plot_function() got an unexpected keyword argument 'ylim'

This works fine for me. Maybe something changed accidentally in the definition of plot_function? You can check plot_function?? from a notebook cell, it should be:

def plot_function(f, title=None, min=-2.1, max=2.1, color='r', ylim=None):
    x = torch.linspace(min,max, 100)[:,None]
    if ylim: plt.ylim(ylim)
    plt.plot(x, f(x), color)
    if title is not None: plt.title(title)

or try to run a fresh copy of the notebook and see if it still gives that error.

My plot_function() is like this

def plot_function(f, tx=None, ty=None, title=None, min=-2, max=2, figsize=(6,4)):
    x = torch.linspace(min,max, 100)
    fig,ax = plt.subplots(figsize=figsize)
    ax.plot(x,f(x))
    if tx is not None: ax.set_xlabel(tx)
    if ty is not None: ax.set_ylabel(ty)
    if title is not None: ax.set_title(title)

Ok, it seems like you are using the 2020 version of the course. The current version uses a different plot_function which I was refering to (and which has the y_limit parameter), maybe there was a mixup. You can find the current course here and the function in particular in notebook 04. Does this solve you problem? :slight_smile:

3 Likes

No I am actually going through course 22 but I don’t knew how this is happening , I have newly installed it

Can you tell me from where are are importing all that

I did not import this. I used the one defined in the first cell of 04-how-does-a-neural-net-really-work.ipynb from the course22 repo. :slight_smile:

2 Likes

Thanks for your help @benkarr , I had the same issue as @mohitmishra . It seems we just had that function imported from fastbook. I wonder weather we should send a PR updating the function there to the one used in the course lectures.

2 Likes

Thank you!

I had no clue there was this repo, I was only bungling around with the fastbook, thank you so much! (Despite it being pinned on the fastai git…)