6

I'm following the tutorial in the book "introduction to machine learning with python" and i've copied the following code:

#create dataframe from data in X_train
#label the colums using the strings in iris_dataset.features_names
iris_dataframe = pd.DataFrame(X_train, columns = iris_dataset.feature_names)
#create a scatter matrix from the dataframe, color by y_train
pd.plotting.scatter_matrix(iris_dataframe,c=y_train,
                           figsize=(15,15), marker='o', 
                           hist_kwds={'bins':20},s=60, 
                           alpha=.8,
                           cmap=mglearn.cm3)

It should plot a graph,but it only print those lines:

array([[<matplotlib.axes._subplots.AxesSubplot object at 0x7f0d073934a8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x7f0d07352908>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x7f0d07376e48>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x7f0d0732ee48>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x7f0d072e3f28>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x7f0d072e3f60>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x7f0d07308ac8>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x7f0d07211400>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x7f0d071ca470>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x7f0d07183470>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x7f0d071be470>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x7f0d07165e80>],
       [<matplotlib.axes._subplots.AxesSubplot object at 0x7f0d07127390>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x7f0d070e5390>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x7f0d0709d390>,
        <matplotlib.axes._subplots.AxesSubplot object at 0x7f0d07047da0>]], dtype=object)

Can someone tell me a reason?

EDIT-SOLVED:I do not know why but a after a re-run of the cells, the graph apperead..

5
  • 1
    Maybe look through the Pandas documentation for plotting and see if your missing something? pandas.pydata.org/pandas-docs/stable/visualization.html Commented Nov 29, 2017 at 19:20
  • 3
    I think all those examples in docs expect to be ran in something like Jupyter, which automatically shows the graphs. If you're running this in a regular python interpreter, you'll need to call matplotlib.pyplot.show yourself. Commented Nov 29, 2017 at 19:29
  • 2
    Sometimes in Jupyter you have to turn on inline plots, %matplotlib inline Commented Nov 30, 2017 at 5:41
  • if you solved it yourself, then post an answer to your own question instead of editing the original Commented Nov 30, 2017 at 15:11
  • You can suppress the lines of text output, which are the str() or repr() (I forget which) version of the value returned by the scatter _matrix() function. To do so, just set the output to a variable: axes = pd.plotting.scatter_matrix(iris_dataframe,c=y_train, ... Commented Nov 30, 2017 at 17:54

2 Answers 2

11

pandas does not have a plotting library in itself. most users normally utilize the matplotlib library when plotting which has a different set of commands. Assuming you have all of the correct libraries installed including matplotlib I would end your code with:

import matplotlib.pyplot as plt
#assuming you got the correct conversion to a pandas dataframe  
pd.plotting.scatter_matrix(df,c=y_train,
                       figsize=(15,15), marker='o', 
                       hist_kwds={'bins':20},s=60, 
                       alpha=.8,
                       cmap=mglearn.cm3)


plt.show()

Here is a link to the matplotlib library that goes into the plt.show() There is also methods to save the figure, which can be found in the documentation here

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

Comments

0

SOLVED! I do not know why but after a re-run of all the cells of the jupyter notebook, the graph apperead correctly.

1 Comment

you have to ensure that you use the statement "import matplotlib.pyplot as plt" in your cell for this to work. if you are calling a function that will plot the graph, then ensure that you use the statement inside the function's definition

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.