'How can I solve " module 'pandas' has no attribute 'scatter_matrix' " error?
I'm trying to run pd.scatter_matrix() function in Jupyter Notebook with my code below:
import matplotlib.pyplot as plt
import pandas as pd
# Load some data
iris = datasets.load_iris()
iris_df = pd.DataFrame(iris['data'], columns=iris['feature_names'])
iris_df['species'] = iris['target']
pd.scatter_matrix(iris_df, alpha=0.2, figsize=(10, 10))
plt.show()
But I'm getting
AttributeError: module 'pandas' has no attribute 'scatter_matrix'.
Even after executing conda update pandas and conda update matplotlib commands in Terminal, this is still occurring.
I executed pd.__version__ command to check my pandas version and it's '0.24.2'. What could be the problem?
Solution 1:[1]
Another option is keeping only pandas import and rewriting the command scatter_matrix, like in the example below:
import pandas as pd
pd.plotting.scatter_matrix(iris_df, alpha=0.2, figsize=(10, 10))
Solution 2:[2]
Using
from pandas.plotting._misc import scatter_matrix
don't use pd.scatter_matrix or pandas.scatter_matrix
you can directly call scatter_matrix
e.g.
cmap = cm.get_cmap('gnuplot')
scatter = scatter_matrix(X, c = y, marker = 'o', s=40, hist_kwds={'bins':15},
figsize=(9,9), cmap = cmap)
plt.suptitle('Scatter-matrix for each input variable')
plt.savefig('fruits_scatter_matrix')
plt.show()
Solution 3:[3]
Use:
from pandas.plotting import scatter_matrix
The code becomes:
import matplotlib.pyplot as plt
from pandas.plotting import scatter_matrix
iris = datasets.load_iris()
iris_df = pd.DataFrame(iris['data'], columns=iris['feature_names'])
iris_df['species'] = iris['target']
scatter_matrix(iris_df, alpha=0.2, figsize=(10, 10))
plt.show()
Solution 4:[4]
I used
from pandas.plotting import scatter_matrix
and called scatter_matrix directly worked like charm.
Solution 5:[5]
In our case we were executing below code "axs = pd.scatter_matrix(sampled_data, figsize=(10, 10)) "
so the error clearly says scatter_matrix is not available in pandas
Solution: A bit of google and we found scatter_matrix is available in pandas.plotting
So correct code is "axs = pd.plotting.scatter_matrix(sampled_data, figsize=(10, 10)) "
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 | dayer4b |
| Solution 2 | David Buck |
| Solution 3 | Tharun Addanki |
| Solution 4 | Marcell Kovacs |
| Solution 5 | Saurabh Sinha |


