Vishal Jain
Vishal Jain

Reputation: 483

What is the relevance of the first line fig,ax=plt.subplots(), when creating a matplotlib plot?

I'm going through a data visualisation in python course to help with my lab reports and I cant seem to understand the purpose of the second line in this example of creating a scatter plot:

import matplotlib.pyplot as plt
fig,ax = plt.subplots()
ax.scatter([1,2,3,4,5],[1,2,3,4,5])
ax.set_xlabel('X')
ax.set_ylabel('Y')
plt.show()

Can someone explain what that first line is doing here. I have only started using python and I haven't seen this sort of syntax before(fig,ax = plt.subplots()). I tried to test if it was a way to assign 2 variables to the same thing by writing x,y=1, I ended up getting an error "int object is not iterable".

Another thing I dont understand is where is fig being used anywhere in the body of code? My current understanding is that the top line defined what fig and ax are, I can see that ax is used in the body of the code to define the scatter plot, but where is fig used? I tried to delete it and run the code, but I got this error:

'tuple' object has no attribute 'scatter'

If someone could please explain the above misconceptions.

Upvotes: 2

Views: 906

Answers (2)

Sheldore
Sheldore

Reputation: 39072

As per the official docs, subplots creates a figure and a set of subplots. Specifically,

Returns:

fig : Figure

ax : axes.Axes object or array of Axes objects. ax can be either a single Axes object or an array of Axes objects if more than one subplot was created. The dimensions of the resulting array can be controlled with the squeeze keyword, see above.

Now when you do

fig, ax = plt.subplots()

the figure object is assigned to the variable fig and the axis object is assigned to the variable ax.

The fig will then give you access to the attributes on a figure-level, for instance, the figure title. The ax will give you access to the attributes on individual subplot level, such as the legends, axis-labels, ticks, of each individual subplot. It will be as array of Axes objects in case you have more than one subplot.

Upvotes: 2

Arthur.V
Arthur.V

Reputation: 706

I tried to test if it was a way to assign 2 variables to the same thing by writing x,y=1, I ended up getting an error "int object is not iterable".

You are almost right. That is syntax to assign multiple variables at the same time, but what you are missing is that plt.subplots() returns a tuple - of two values paired together.

If you want to better understand it you can run:

a, b = (1, 4)

or

a,b = 1, 4

(it's the same as far as python is concerns, it packs/unpacks values to a tuple if multiple values are used or returned)

I tried to delete it and run the code, but I got this error:

'tuple' object has no attribute 'scatter'

This is also related to why you got this error. The figure is indeed not in use in your code snippet, but you need it for python to understand you want to use part of the tuple and not the tuple itself. For example: a=(1,2) will result in a holding a tuple, but in a, b = 1, 2 each of the created variables will hold an integer.

In your case, the axis object has a method scatter, which the tuple object does not have, hence your error.

Upvotes: 1

Related Questions