Reut
Reut

Reputation: 1592

Create 4 scatter subplots from one dataframe

I have database that contains observations and then it has for each one PCA analysis- PCA 1 and PCA 2, that were calculated for different dates: enter image description here

My goal: create 4 scatter subplots, based on those dates,and to color them in different colors, based on column "line_leg".

What I have done so far:

Created 4 scatter subplotplots -

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

enter image description here

and tried to plot the ax[0][0] +color it this way:


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()

But I got error:

TypeError: 'AxesSubplot' object is not subscriptable

My end goal is to have those 4 figures plot with the data from the df, each figure shows different date (19,21,28,03), and colored according to the value of line_leg.

Upvotes: 0

Views: 256

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40697

You array of Axes is calles axes, not ax. You should call axes[0,0].scatter(...)

Upvotes: 1

Related Questions