user3848207
user3848207

Reputation: 4907

How does matplotlib knows what to display in this code?

The python code below will display pair plots of histogram and scatter-plots.

import pandas as pd
import matplotlib.pyplot as plt

from sklearn import datasets

iris_dataset = datasets.load_iris()
X = iris_dataset.data
Y = iris_dataset.target

iris_dataframe = pd.DataFrame(X, columns=iris_dataset.feature_names)
# create a scatter matrix from the dataframe, color by y_train
grr = pd.scatter_matrix(iris_dataframe, c=Y, figsize=(15, 15), marker='o',
                        hist_kwds={'bins': 20}, s=60, alpha=.8)

plt.show()

What puzzles me is how does plt.show() know what to display? grr was not seen to be assigned anywhere in the code into plt. How does plt magically knows what to display?

Upvotes: 3

Views: 147

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339200

Pandas uses matplotlib to create its plots. The complete plot is already created by the pandas.scatter_matrix command.

The return of pandas.scatter_matrix is an array of matplotlib axes. This can be used to adjust the plot after creation.
However, this is not necessary as the complete plot already exist as a matplotlib figure.

When calling plt.show() any figure present in the matplotlib state machine is simply plotted. Since there is a figure present (=the one created by pandas.scatter_matrix), it will be shown.

Upvotes: 4

Related Questions