python - 在 Pandas 数据框中显示对图

标签 python python-3.x pandas plot

我正在尝试通过在 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)

我想显示成对图,看起来像这样;

Enter image description here

我正在使用 Python v3.6 和 PyCharm并且我没有使用 Jupyter Notebook。

最佳答案

此代码适用于使用 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)

对于 pandas 版本 < v0.20.0。

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

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 存在版本不匹配问题。

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

plt.savefig('foo.png')

同时移除

# %matplotlib inline

Enter image description here

关于python - 在 Pandas 数据框中显示对图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42592493/

相关文章:

Python 3 IPv6 组播

矩阵的 Python Scipy spearman 相关性与双数组相关性不匹配,也不匹配 pandas.Data.Frame.corr()

python - 比较两个 Pandas 列并设置为标签

python - 如何增加 tkinter(Python) 中输入(显示)框的大小(高度)?

python - "Merge"替换 Keras/Tensorflow/Python3

python - 为什么 Pytz 对纽约和底特律有不同的时区列表?

python - 将项目添加到优先级队列中

python - 写入 CSV 文件,其中值是字典,并且值需要与键位于同一行

python - 我应该如何通过函数传递 matplotlib 对象;作为 Axis、Axes 还是 Figure?

python - 最简单的方法来 "store"一个类上的函数而不绑定(bind)它?