你可以使用
scatter它,但是这需要为你提供数值
key1,并且你不会注意到图例。
最好只
plot对像这样的离散类别使用。例如:
import matplotlib.pyplot as pltimport numpy as npimport pandas as pdnp.random.seed(1974)# Generate Datanum = 20x, y = np.random.random((2, num))labels = np.random.choice(['a', 'b', 'c'], num)df = pd.Dataframe(dict(x=x, y=y, label=labels))groups = df.groupby('label')# Plotfig, ax = plt.subplots()ax.margins(0.05) # Optional, just adds 5% padding to the autoscalingfor name, group in groups: ax.plot(group.x, group.y, marker='o', linestyle='', ms=12, label=name)ax.legend()plt.show()如果你希望外观看起来像默认pandas样式,则只需rcParams使用pandas样式表进行更新,并使用其颜色生成器即可。(我也略微调整了图例):
import matplotlib.pyplot as pltimport numpy as npimport pandas as pdnp.random.seed(1974)# Generate Datanum = 20x, y = np.random.random((2, num))labels = np.random.choice(['a', 'b', 'c'], num)df = pd.Dataframe(dict(x=x, y=y, label=labels))groups = df.groupby('label')# Plotplt.rcParams.update(pd.tools.plotting.mpl_stylesheet)colors = pd.tools.plotting._get_standard_colors(len(groups), color_type='random')fig, ax = plt.subplots()ax.set_color_cycle(colors)ax.margins(0.05)for name, group in groups: ax.plot(group.x, group.y, marker='o', linestyle='', ms=12, label=name)ax.legend(numpoints=1, loc='upper left')plt.show()


