33

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?

1
  • 1
    it's under plotting: see the docs please check the api against your version Commented Mar 28, 2019 at 9:24

6 Answers 6

66

This method is under pandas.plotting - docs and pandas.plotting.scatter_matrix:

from pandas.plotting import scatter_matrix

scatter_matrix(iris_df, alpha=0.2, figsize=(10, 10))
Sign up to request clarification or add additional context in comments.

2 Comments

I'm just fresh in python but shouldn't pandas.plotting be already included in pandas? I'm running a tutorial right now where exactly the code in the question is given and I wondered, too, why it didn't work. But when using "from pandas.plotting ..." it works. Nevertheless, I would like to know why the plotting command is so special it has to be imported manually?
Still getting error 'AttributeError: module 'pandas' has no attribute 'scatter_matrix''
22

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))

1 Comment

Best answer. You don't need to reimport panda modules.
5

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()

1 Comment

Please read the editing guide
2

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()

Comments

1

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

enter image description here

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)) "

enter image description here

Comments

0

I used

from pandas.plotting import scatter_matrix

and called scatter_matrix directly worked like charm.

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.