在Pandas数据框中显示对图

在Pandas数据框中显示对图

本文介绍了在Pandas数据框中显示对图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过在pandas数据框中从scatter_matrix创建显示对图.这是创建对图的方式:

I am trying to display a pair plot by creating from scatter_matrix in pandas dataframe. This is how the pair plot is created:

# 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;

我正在使用Python v3.6和 PyCharm ,并且未使用Jupyter Notebook.

I am using Python v3.6 and PyCharm and am not using Jupyter Notebook.

推荐答案

此代码在使用Python 3.5.2的情况下对我有效:

This code worked for me using 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)

对于熊猫版本< v0.20.0.

For pandas version < v0.20.0.

感谢 michael-szczepaniak 指出此API已被弃用.

Thanks to michael-szczepaniak for pointing out that this API had been deprecated.

grr = pd.scatter_matrix(iris_dataframe, c=Y, figsize=(15, 15), marker='o',
                        hist_kwds={'bins': 20}, s=60, alpha=.8)

我只需要删除cmap=mglearn.cm3件,因为我无法使mglearn正常工作. sklearn存在版本不匹配的问题.

I just had to remove the cmap=mglearn.cm3 piece, because I was not able to make mglearn work. There is a version mismatch issue with sklearn.

要不显示图像并将其直接保存到文件中,可以使用以下方法:

To not display the image and save it directly to file you can use this method:

plt.savefig('foo.png')

也删除

# %matplotlib inline

这篇关于在Pandas数据框中显示对图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:11