-1

I have a dataset and I want to draw scatterplots for all the columns with the last column (Result), using subplots.

fig, ax = plt.subplots(figsize=(10, 6))
for i in range(len(columns)-1):
    columns = df.columns[i]
    ax.scatter(x = df[columns], y = df['Result'])
    plt.xlabel(columns[i])
    plt.ylabel("Result")

plt.show

I tried using subplots to make the scatter plots but its giving me this error

IndexError                                Traceback (most recent call last)
/var/folders/pn/ly3sp5n508v11pnffllvyl6h0000gn/T/ipykernel_34526/1242862654.py in <module>
     18 fig, ax = plt.subplots(figsize=(10, 6))
     19 for i in range(len(columns)-1):
---> 20     columns = df.columns[i]
     21     ax.scatter(x = df[columns], y = df['Result'])
     22     plt.xlabel(columns[i])

~/opt/anaconda3/lib/python3.9/site-packages/pandas/core/indexes/base.py in __getitem__(self, key)
   5051             # GH#44051 exclude bool, which would return a 2d ndarray
   5052             key = com.cast_scalar_indexer(key, warn_float=True)
-> 5053             return getitem(key)
   5054 
   5055         if isinstance(key, slice):

IndexError: index 7 is out of bounds for axis 0 with size 7
mb611
  • 9
  • 3
  • Verify how many columns you really have in your data frame. Probably you are accessing a column in IndexList that does not exist. Okay, so you are doing a very very wrong thing. you are assigning `columns` as `df.columns[i]` so now the length of `columns` becomes the number of characters in the string assigned to `columns` and NOT the number of columns in dataframe. – Mathpdegeek497 Mar 04 '23 at 10:17

1 Answers1

0

You are assigning columns as df.columns[i] so now the length of columns becomes the number of items in each column and NOT the number of columns in the data frame. Let's say you have a data frame with 3 columns namely price, quantity, and amount (in order). Now, the code

columns = df.columns[0]

makes columns='price and hence, now len(columns) which should ideally be the number of columns in the data frame is now the length of string 'price' which is 5 (as opposed to desired value of 3). Try this way:

fig, ax = plt.subplots(figsize=(10, 6))
for i in range(len(columns)-1):
    columns = df.columns
    ax.scatter(x = df[columns[i]], y = df['Result'])
    plt.xlabel(columns[i])
    plt.ylabel("Result")

plt.show