从一个数据帧创建 4 个散点子图

Create 4 scatter subplots from one dataframe

我有包含观察结果的数据库,然后它有每个 PCA 分析 - PCA 1 和 PCA 2,它们是针对不同日期计算的:

我的目标: 根据这些日期创建 4 个散点子图,并根据列 "line_leg".

将它们着色为不同的颜色

到目前为止我做了什么:

创建了 4 个散点子图 -

fig, axes = plt.subplots(nrows=2, ncols=2,figsize=(16, 10))

并尝试绘制斧头[0][0] +这样着色:


targets = ['100 D','100 C','70 D','70 C','40 D', '40 C']
colors = ['tab:blue','tab:green','tab:purple','tab:cyan','tab:red','tab:orange']
#colors = ['lawngreen','royalblue','tab:green','midnightblue','tab:red','tab:orange']
for target, color in zip(targets,colors):
    #goes to line. and check of 1,2,3,4,5,6 are true or false, 6 times total, and if true gives the color.
    indicesToKeep = df_PCA_all['line_leg'] == target
    ax[0][0].scatter(df_PCA_all.iloc[:,3:5].loc[indicesToKeep, 'PCA1_19']
               , df_PCA_all.iloc[:,3:5].loc[indicesToKeep, 'PCA2_19']
               , c = color
               , s = 100
               , label=target)
ax.legend(targets)
ax.grid()

但是我得到了错误:

TypeError: 'AxesSubplot' object is not subscriptable

我的最终目标是用 df 中的数据绘制这 4 个数字,每个数字显示不同的日期 (19,21,28,03),并根据 line_leg.

您的 Axes 数组调用的是 axes,而不是 ax。你应该打电话给 axes[0,0].scatter(...)