Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách vẽ hai Seaborn lmplots cạnh nhau (Matplotlib)

Dùng plt.subplots(1,2) tạo 2 axes, vẽ countplot lên từng axes.

  • f, axes = plt.subplots(1, 2).
  • df = DataFrame col1, col2.
  • sns.countplot(df.col1, x='col1', color="red", ax=axes[0]).
  • sns.countplot(df.col2, x="col2", color="green", ax=axes[1]).
  • show().

Ví dụ

import pandas as pd
import numpy as np
import seaborn as sns
from matplotlib import pyplot as plt
plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True
f, axes = plt.subplots(1, 2)
df = pd.DataFrame(dict(col1=np.linspace(1, 10, 5), col2=np.linspace(1, 10, 5)))
sns.countplot(df.col1, x='col1', color="red", ax=axes[0])
sns.countplot(df.col2, x="col2", color="green", ax=axes[1])
plt.show()

Kết quả

Hai countplot cạnh nhau: bên trái cột đỏ, bên phải cột xanh, cùng trục X giá trị 1-10.