2

I have a dataframe that contains X & Y position data and has 3 grouping variables:

  1. obsScenario (0, 1 or 2)
  2. startPos (1 or 2)
  3. targetPos (1, 2, or 3).

Thus there are 18 combinations of these grouping variables: 3 x 2 x 3

The X and Y data is approx 300-500 data points in length per participant (it varies).

The dataframe looks like this:

              X         Y  participantNum  obsScenario  startPos  targetPos
0    -16.000000  5.000000         6432024            0         1          1
1    -16.000000  5.000000         6432024            0         1          1
2    -15.833450  5.000000         6432024            0         1          1
3    -15.667200  5.000000         6432024            0         1          1
4    -15.500100  5.000000         6432024            0         1          1
        ...       ...             ...          ...       ...        ...
2185  -1.572058 -3.982638         7830381            2         2          2
2186  -1.406996 -3.958967         7830381            2         2          2
2187  -1.242231 -3.935339         7830381            2         2          2
2188  -1.077516 -3.911718         7830381            2         2          2
2189  -0.912604 -3.888069         7830381            2         2          2

I need to plot the X, Y data separately for each of these 18 combinations.

Im trying to use something like this, but this just plots all the XY trajectories on the same plot:

for aid, grp in df.groupby(['obsScenario', 'startPos', 'targetPos']):
    plt.plot(grp[0].values, grp[1].values) 
plt.show() 

And using something like this doesnt take the different combinations of grouping variables into account:

fig, axs = plt.subplots(nrows=3, ncols=6)

for ax in axs.flat:
    plotxy(ax,x,y)
CentauriAurelius
  • 504
  • 3
  • 21
  • If you will put `plt.show()` inside the loop then you will get plot for each row. else use subplots – Pygirl Feb 02 '21 at 08:23
  • Does this answer your question? [How to plot a list of image in loop using matplotlib?](https://stackoverflow.com/questions/48435229/how-to-plot-a-list-of-image-in-loop-using-matplotlib) – Pygirl Feb 02 '21 at 08:23
  • No, as it doesnt take the different combinations of grouping variables into account. And i dont want to plot each row as that would output thousands of plots, and I need the XY trajectories separated into the 18 diff combinations of grouping variables. My full dataset actually has 106 total combinations. – CentauriAurelius Feb 02 '21 at 08:34
  • may be this will help a bit: https://stackoverflow.com/a/48358380/6660373 – Pygirl Feb 02 '21 at 08:43
  • 1
    You are so close. You just have to combine both strategies - define the number of subplots, group by variables, zip the ax object with the groupby variables, then plot into different subplots with `ax.plot(grp[0].values, grp[1].values) ` – Mr. T Feb 02 '21 at 10:06
  • Ah good to know I am close! But how do I zip the ax object with the groupby variables? – CentauriAurelius Feb 02 '21 at 10:13

1 Answers1

2

This does what I was looking for:

fig, axs = plt.subplots(6,3)
grp = df.groupby(['obsScenario', 'startPos', 'targetPos'])
for (name, df), ax in zip(grp, axs.flat):
    df.plot(x='X',y='Y', ax=ax)
CentauriAurelius
  • 504
  • 3
  • 21
  • Yup, that's what I meant. However, in the long run, it might be preferable to plot with `ax.plot(gf.X, gf.Y)` (don't reuse df - it will overwrite your original df). The reason is that pandas and matplotlib sometimes conflict in their axis treatment - so, if you need additional functionality from matplotlib, it is better to not use the pandas plot functions. They are great for what they are - making life easier for standard plots, though. – Mr. T Feb 02 '21 at 10:36
  • Yes i noticed that was happening! Thanks again. – CentauriAurelius Feb 02 '21 at 10:46