-3

I don't understand how the n +=1 variable below made the distplot visualization into 3 columns

n=0
for x in ['Age', 'Annual Income (k$)', 'Spending Score (1-100)']:
    n +=1
    plt.subplot(1,3,n)
    plt.subplots_adjust(hspace=0.5,wspace = 0.5)
    sns.distplot(df[x], bins = 20)
    plt.title('Displot of {}'.format(x))
plt.show()

pls see attached photo of distplotenter image description here

Kat
  • 19
  • 4
  • As a side note, you could enumerate instead - `for n, x in enumerate(['Age', 'Annual Income (k$)', 'Spending Score (1-100)']):` – tdelaney Aug 13 '20 at 04:33
  • Does this answer your question? [What does += mean in Python?](https://stackoverflow.com/questions/823561/what-does-mean-in-python) – Trenton McKinney Aug 13 '20 at 04:35

1 Answers1

1

The third parameter of plt.subplot represents the index. This may be helpful to you: https://matplotlib.org/3.3.0/api/_as_gen/matplotlib.pyplot.subplot.html

"The position of the subplot described by one of

Three integers (nrows, ncols, index). The subplot will take the index position on a grid with nrows rows and ncols columns. index starts at 1 in the upper left corner and increases to the right. index can also be a two-tuple specifying the (first, last) indices (1-based, and including last) of the subplot, e.g., fig.add_subplot(3, 1, (1, 2)) makes a subplot that spans the upper 2/3 of the figure.

A 3-digit integer. The digits are interpreted as if given separately as three single-digit integers, i.e. fig.add_subplot(235) is the same as fig.add_subplot(2, 3, 5). Note that this can only be used if there are no more than 9 subplots."

funyuns
  • 229
  • 1
  • 9