问题描述
下面的python代码将显示直方图和散点图的成对图.
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()
让我感到困惑的是,plt.show()
如何知道要显示什么?在代码plt
中的任何位置都没有看到grr
被分配. plt
如何神奇地知道要显示什么?
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?
推荐答案
Pandas使用matplotlib创建其图.完整的图已经通过pandas.scatter_matrix
命令创建.
Pandas uses matplotlib to create its plots. The complete plot is already created by the pandas.scatter_matrix
command.
pandas.scatter_matrix
的返回是matplotlib轴的数组.创建后可用于调整图.
但是,这不是必需的,因为完整的图已经作为matplotlib图形存在.
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.
调用plt.show()
时,会简单地绘制matplotlib状态机中存在的任何图形.由于存在一个图形(= pandas.scatter_matrix
创建的图形),因此将显示它.
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.
这篇关于matplotlib如何知道在此代码中显示什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!