# 数据分析与预测（三）——散点图的绘制

## 一、二维散点图的绘制

### 1.采用pandas.plotting.scatter_matrix函数绘制

``````   pd.plotting.scatter_matrix(iris_data, figsize=(10, 10), alpha=1, hist_kwds={"bins": 20})
``````

### 2. 采用seaborn进行绘制

``````    # No. 1
# 全部变量都放进去
sns.pairplot(iris_data)

``````

``````    # No.2
#kind:用于控制非对角线上图的类型，可选'scatter'与'reg'
#diag_kind:用于控制对角线上的图分类型，可选'hist'与'kde'

sns.pairplot(iris_data, kind='reg', diag_kind='kde')
sns.pairplot(iris_data, kind='reg', diag_kind='hist')
``````

``````    # No 3
# hue：针对某一字段进行分类
sns.pairplot(iris_data, hue='species', kind='reg', diag_kind='hist')
``````

``````    # No 4
# vars：研究某2个或者多个变量之间的关系vars,
# x_vars,y_vars：选择数据中的特定字段，以list形式传入需要注意的是，x_vars和y_vars要同时指定
sns.pairplot(iris_data, vars=["sepal length (cm)", "sepal width (cm)"])
sns.pairplot(iris_data, x_vars=["petal length (cm)", "sepal width (cm)"], y_vars=["petal width (cm)", "sepal length (cm)"])
``````

## 二、 三维散点图绘制

``````
# No. 5 3d
from mpl_toolkits.mplot3d import Axes3D
dims={'x':'petal length (cm)', 'y':'petal width (cm)', 'z':'sepal length (cm)'}

types=iris_data.species.value_counts().index.tolist()
print(dims, types)
flg=plt.figure()
ax=Axes3D(flg)
for iris_type in types:
tmp_data=iris_data[iris_data.species ==iris_type]
x,y,z = tmp_data[dims['x']], tmp_data[dims['z']],tmp_data[dims['z']]
ax.scatter(x, y, z, label=iris_type)

ax.legend(loc='upper left')
ax.set_zlabel(dims['z'])
ax.set_xlabel(dims['x'])
ax.set_ylabel(dims['y'])
plt.show()
``````

``````import pandas as pd
from sklearn import datasets
import seaborn as sns
import matplotlib.pyplot as plt

def function():
iris = datasets.load_iris()
iris_data = pd.DataFrame(iris.data, columns=iris.feature_names)
iris_data['species'] = iris.target_names[iris.target]

# No. 0
pd.plotting.scatter_matrix(iris_data, figsize=(10, 10), alpha=1, hist_kwds={"bins": 20})

# No. 1
# 全部变量都放进去
sns.pairplot(iris_data)

# No.2
sns.pairplot(iris_data, kind='reg', diag_kind='kde')
sns.pairplot(iris_data, kind='reg', diag_kind='hist')

# No 3
# hue：针对某一字段进行分类
sns.pairplot(iris_data, hue='species', kind='reg', diag_kind='hist')

# No 4
# vars：研究某2个或者多个变量之间的关系vars,
# x_vars,y_vars：选择数据中的特定字段，以list形式传入需要注意的是，x_vars和y_vars要同时指定

sns.pairplot(iris_data, vars=["sepal length (cm)", "sepal width (cm)"])
sns.pairplot(iris_data, x_vars=["petal length (cm)", "sepal width (cm)"], y_vars=["petal width (cm)", "sepal length (cm)"])

# No. 5 3d
from mpl_toolkits.mplot3d import Axes3D
dims={'x':'petal length (cm)', 'y':'petal width (cm)', 'z':'sepal length (cm)'}

types=iris_data.species.value_counts().index.tolist()
print(dims, types)
flg=plt.figure()
ax=Axes3D(flg)
for iris_type in types:
tmp_data=iris_data[iris_data.species ==iris_type]
x,y,z = tmp_data[dims['x']], tmp_data[dims['z']],tmp_data[dims['z']]
ax.scatter(x, y, z, label=iris_type)

ax.legend(loc='upper left')
ax.set_zlabel(dims['z'])
ax.set_xlabel(dims['x'])
ax.set_ylabel(dims['y'])

print( iris_data)

plt.show()

if __name__ == '__main__':
function()

``````

THE END