Subplots - Pandas Visualizations and Grid Charts

Screen Link:

My Code:
plt.subplot(nrows=3, ncols=2, index=1)

Good day!

In Pandas Visualizations and Grid Charts, lesson 10. Grid charts, I don’t really understand what the number of rows and columns are doing in this line of code: plt.subplot(nrows=3, ncols=2, index=1)? What are the number of rows and columns supposed to do?

Any help will be much appreciated!


This is clarified in the content -

Before moving on, we need to mention the table-like structure of a grid chart — it has rows and columns.The grid chart below, for instance, has three rows and two columns. Also, each plot is indexed by a number — the upper left plot has the index 1, its neighboring plot on the right is indexed with 2, and so on.

We can specify the number of rows and columns for our grid chart by using the plt.subplot(nrows, ncols, index) function. For instance, plt.subplot(3, 2, 1) means that we want to build the plot with index 1 on a grid chart that has 3 rows and 2 columns.

Thank you for reply and thank you for your time! I am not understanding what the rows and columns are doing - maybe I am missing something.

Hello @nawaz_quraishi
Check out this to learn more:

Essentially your code won’t work since the method only accepts positional arguments. This is also specified in the DQ lesson

As a sidenote, a quirk of plt.subplot() is that it only takes positional arguments. If we use keyword arguments, we’ll get an error — plt.subplot(nrows=3, ncols=2, index=1) raises an error.

plt.subplot is used to add a plot to a figure, when adding a plot you need to specify the resulting nrows, ncols and the index (the position of the plot, index starts at one). This will be for all the plots you want to add in the figure.
Supposed I want to a 3X2 figure, that is, a figure with 3 rows and 2 columns (altogether forming 6 figures) as shown here:

I’ll first need to create the frames of the plots to be added.

plt.Figure() # For the entire output
plt.subplot(3, 2, 1) # the first plot frame in row 1
plt.subplot(3,2, 2) # second plot frame in row 1
plt.subplot(3, 2, 3) # third plot frame in row 2
. # and so on

All in all, we need to specify the nrow (same for all), ncol (same for all), and index.
Advancing in the next chapter uncovers more


Thank you for the detailed explanation and for taking the time!


1 Like