Python 在 Pandas 数据框中显示对图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42592493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Displaying pair plot in Pandas data frame
提问by user3848207
I am trying to display a pair plot by creating from scatter_matrix in pandas dataframe. This is how the pair plot is created:
我正在尝试通过从 pandas 数据框中的 scatter_matrix 创建来显示对图。这是创建配对图的方式:
# Create dataframe from data in X_train
# Label the columns using the strings in iris_dataset.feature_names
iris_dataframe = pd.DataFrame(X_train, columns=iris_dataset.feature_names)
# Create a scatter matrix from the dataframe, color by y_train
grr = pd.scatter_matrix(iris_dataframe, c=y_train, figsize=(15, 15), marker='o',
hist_kwds={'bins': 20}, s=60, alpha=.8, cmap=mglearn.cm3)
I want to display the pair plot to look something like this;
我想显示配对图看起来像这样;
I am using Python v3.6 and PyCharmand am not using Jupyter Notebook.
我正在使用 Python v3.6 和PyCharm,但没有使用 Jupyter Notebook。
回答by Vikash Singh
This code worked for me using Python 3.5.2:
这段代码使用 Python 3.5.2 对我有用:
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
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.plotting.scatter_matrix(iris_dataframe, c=Y, figsize=(15, 15), marker='o',
hist_kwds={'bins': 20}, s=60, alpha=.8)
For pandas version < v0.20.0.
对于熊猫版本 < v0.20.0。
Thanks to michael-szczepaniakfor pointing out that this API had been deprecated.
感谢michael-szczepaniak指出该 API 已被弃用。
grr = pd.scatter_matrix(iris_dataframe, c=Y, figsize=(15, 15), marker='o',
hist_kwds={'bins': 20}, s=60, alpha=.8)
I just had to remove the cmap=mglearn.cm3piece, because I was not able to make mglearn work. There is a version mismatch issue with sklearn.
我只需要移除这个cmap=mglearn.cm3部分,因为我无法让 mglearn 工作。sklearn 存在版本不匹配问题。
To not display the image and save it directly to file you can use this method:
要不显示图像并将其直接保存到文件,您可以使用以下方法:
plt.savefig('foo.png')
Also remove
也删除
# %matplotlib inline
回答by Michael Szczepaniak
Just an update to Vikash's excellent answer. The last two lines should now be:
只是对 Vikash 出色答案的更新。最后两行现在应该是:
grr = pd.plotting.scatter_matrix(iris_dataframe, c=Y, figsize=(15, 15), marker='o',
hist_kwds={'bins': 20}, s=60, alpha=.8)
The scatter_matrixfunction has been moved to the plottingpackage, so the original answer, while correct is now deprecated.
该scatter_matrix功能已经转移到绘图包,所以原来的答案,而正确的现在已经过时。
So the complete code would now be:
所以完整的代码现在是:
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
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.plotting.scatter_matrix(iris_dataframe, c=Y, figsize=(15, 15), marker='o',
hist_kwds={'bins': 20}, s=60, alpha=.8)
回答by Alexander
回答by R.Zhan
I finally know how to do it with PyCharm.
我终于知道如何用 PyCharm 做到这一点了。
Just import matploblib.plottingas pltinstead:
只需导入matploblib.plotting为plt:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import mglearn
from pandas.plotting import scatter_matrix
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
iris_dataset = load_iris()
X_train,X_test,Y_train,Y_test = train_test_split(iris_dataset['data'],iris_dataset['target'],random_state=0)
iris_dataframe = pd.DataFrame(X_train,columns=iris_dataset.feature_names)
grr = scatter_matrix(iris_dataframe,c = Y_train,figsize = (15,15),marker = 'o',
hist_kwds={'bins':20},s=60,alpha=.8,cmap = mglearn.cm3)
plt.show()
Then it works perfect as below:
然后它完美地工作如下:


回答by Rahul Kumar Gupta
first of all use
pip install mglearnthen import the mglearn
首先使用
pip install mglearn然后导入mglearn
the code will be like this...
代码将是这样的......
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
import pandas as pd
import mglearn
import matplotlib.pyplot as plt
iris_dataframe=pd.DataFrame(X_train,columns=iris_dataset.feature_names)
grr=pd.scatter_matrix(iris_dataframe,
c=y_train,figsize=(15,15),marker='o',hist_kwds={'bins':20},
s=60,alpha=.8,cmap=mglearn.cm3)
plt.show()

