0

I'm trying to plot a correlation graph with the dataset.I have written the following function for the same

def plot_corr(diabetes_df,size=11):
        corr=diabetes_df.corr()
        fig, ax= plt.subplots(figsize=(size,size))
        ax.matshow(corr)
        plt.xticks(range(len(corr.columns),corr.columns))
        plt.yticks(range(len(corr.columns),corr.columns))

when this gets executed I'm thrown an error Getting TypeError: 'Index' object cannot be interpreted as an integer Error after that a graph get printed with no column ways. Can anyone correct me where I went wrong. Thanks in advance

In case someone wants to review the whole code https://www.kaggle.com/code/akhilkrishnathinna/diabetes-ml-model/edit

2
  • def plot_corr(diabetes_df,size=11): corr=diabetes_df.corr() fig, ax= plt.subplots(figsize=(size,size)) ax.matshow(corr) plt.xticks(range(len(corr.columns),corr.columns)) plt.yticks(range(len(corr.columns),corr.columns)) Commented Apr 26, 2022 at 16:19
  • There's no reason to put code in comments. Your code is already in your post so I don't see what the point is of putting a big unformatted single line of jumbled up code in a follow-up comment. Commented Apr 26, 2022 at 16:22

1 Answer 1

1

Here:

plt.xticks(range(len(corr.columns),corr.columns))
plt.yticks(range(len(corr.columns),corr.columns))

You are passing corr.columns as the second argument to range, and range requires integers, not an Index object (which is the type of corr.columns). You probably meant to pass corr.columns as the second argument to plt.xticks and plt.yticks respectively:

plt.xticks(range(len(corr.columns)),corr.columns)
plt.yticks(range(len(corr.columns)),corr.columns)

A decent editor will help you avoid mistakes like this by automatically flashing the matching opening parenthesis when you type a closing parenthesis, so you can see what each function's arguments actually are.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.