Matplotlib_散布図 - ikymrkw/pydepot GitHub Wiki

# アヤメデータの準備
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.datasets import load_iris

data = load_iris()
X = pd.DataFrame(data.data, columns=data.feature_names)
y = pd.DataFrame(data.target, columns=['Species'])
df = pd.concat([X, y], axis=1)

print(df['Species'].unique())
# 単純な散布図
%matplotlib inline
xlabel = 'sepal length (cm)'
ylabel = 'sepal width (cm)'
plt.scatter(df[xlabel], df[ylabel], c=df.Species)
plt.show()
# 軸ラベルを書くには工夫が必要
%matplotlib inline

fig, ax = plt.subplots()
xlabel = 'sepal length (cm)'
ylabel = 'sepal width (cm)'
ax.scatter(df[xlabel], df[ylabel], c=df.Species)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)

fig, ax = plt.subplots()
ax = fig.add_subplot(1,1,1)
xlabel = 'petal length (cm)'
ylabel = 'petal width (cm)'
ax.scatter(df[xlabel], df[ylabel], c=df.Species)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)

plt.show()