'Getting TypeError: 'Index' object cannot be interpreted as an integer Error

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



Solution 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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1