您可以为同一轴上的 4 个子组绘制散点图。代码如下:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# x, y
xy = np.random.randn(100, 2)
# label1, label2
labels = np.random.choice([True, False], size=(100, 2)).astype(int)
# construct data
data = np.concatenate([xy, labels], axis=1)
df = pd.DataFrame(data, columns=['x', 'y', 'label1', 'label2'])
# group into 4 sub-groups according to labels
mask1 = (df.label1 == 1) & (df.label2 == 0)
mask2 = (df.label1 == 0) & (df.label2 == 0)
mask3 = (df.label1 == 1) & (df.label2 == 1)
mask4 = (df.label1 == 0) & (df.label2 == 1)
# do scatter plots for 4 sub-groups individually on the same axes, add label info
fig, ax = plt.subplots(figsize=(12, 8))
ax.scatter(df.x[mask1], df.y[mask1], marker='o', c='r', label='label1 == 1, label2 == 0')
ax.scatter(df.x[mask2], df.y[mask2], marker='x', c='b', label='label1 == 0, label2 == 0')
ax.scatter(df.x[mask3], df.y[mask3], marker='.', c='g', label='label1 == 1, label2 == 1' )
ax.scatter(df.x[mask4], df.y[mask4], marker='<', c='k', label='label1 == 0, label2 == 1')
# show the legend
ax.legend(loc='best')