Why do we use fig variable for pyplot?

Hey guys!. So was going through the functions code and at few places I see this fig variable of type matplotlib.pyplot but doesn’t seem to use it anywhere throughout the entire function definition.
A simple one being

def plt_house_x(x, y,f_wb=None, ax=None):
    ''' plot house with axis '''
    if not ax:
        fig, ax = plt.subplots(1,1)
    ax.scatter(x, y, marker='x', c='r', label="Actual Value")

    ax.set_title("Housing Prices")
    ax.set_ylabel('Price (in 1000s of dollars)')
    ax.set_xlabel(f'Size (1000 sqft)')
    if f_wb is not None:
        ax.plot(x, f_wb,  c=dlblue, label="Our Prediction")
    ax.legend()

And another being plt_gradients
Can anyone please explain as to why they have structured it this way?

1 Like

Hello @Tarun_Kumar_S,

We need to have a Figure object to contain those subplots. plt.subplots can create a figure that contains one or more than one subplots, so while ax can be a single Axes object or an array of Axes, we always have only one Figure object. For more on what you can configure with the Figure object, this page will give you a list.

If the function doesn’t use it, it simply means the author of the function didn’t need to use it, but the Figure object has to be there, and so we can just leave it.

Also note that it is matplotlib that is giving us the Figure handle, and this course uses matplotlib, so we see it there.

Cheers,
Raymond

1 Like

Thanks a lot @rmwkwok!.

First of I had this weird assumption that when I say a, b = 5 it means a = 5, b = 5. But that’s not the case. That is not a valid syntax. So based on this assumption I had come to the conclusion that fig and axes are the same. I was wrong of course. Thanks for clarifying!

I have a follow up question :
can I change the name of the variable or the object fig to say something like fg and similarly ax to say axes(if its not a reserved keyword)

1 Like

Hello @Tarun_Kumar_S,

Yes, you can change those names! I usually keep fig as fig, but change ax to axes when I am getting more than one subplot. fig is the usual name, and the good thing about sticking to the convention is everyone (probably including the future you) knows what it is right the way.

By the way, spend 20 seconds to open this page, and scroll to the “return” section, where you will see two items. It means the function returns a tuple of two things, and that’s why we unpack such tuple into two variables - fig and ax. I hope you see how reading documentation helps you understand the code. I almost read some documentation everyday.

Cheers,
Raymond

1 Like

Thanks a lot mate!. You were really helpful.
Will ping you if encounter any doubts in the future.
Once again thanks for your service sir :saluting_face:

1 Like

You are welcome, @Tarun_Kumar_S. There are many good mentors and fellow learners here from whom I can learn, and we keep having new mentors too, so any one of us can have an inspiring discussion with you next time :raised_hands:

Cheers,
Raymond