2

So I am using the groupby function in pandas to get mean of two columns using conditions based on two other columns. I am having trouble creating the matplotlib plots

An example table is

data_temp = pd.DataFrame([
        [3, 16, 0, 0, 10],
        [3, 20, 0, 1, 11],
        [3, 25, 0, 2, 11],
        [3, 30, 0, 3, 15],
        [4, 30, 0, 0, 0],
        [4, 45, 0, 1, 0],
        [4, 54, 0, 2, 0],
        [4, 54, 0, 3, 0],
        [5, 31, 0, 0, 14],
        [5, 32, 0, 1, 15],
        [5, 45, 0, 2, 0],
        [5, 46, 0, 3, 0],
        [3, 1, 0, 0, 11],
        [3, 5, 0, 1, 12],
        [3, 6, 0, 2, 13],
        [3, 8, 0, 3, 11],
        [4, 35, 0, 0, 0],
        [4, 25, 0, 1, 0],
        [4, 34, 0, 2, 0],
        [4, 24, 0, 3, 0]
    ], columns=list('ABCDE'))


result = data_temp.groupby(['A', 'D']).agg({'B':'mean', 'E':'mean'})
print(result)

I get

        B     E
A D            
3 0   8.5  10.5
  1  12.5  11.5
  2  15.5  12.0
  3  19.0  13.0
4 0  32.5   0.0
  1  35.0   0.0
  2  44.0   0.0
  3  39.0   0.0
5 0  31.0  14.0
  1  32.0  15.0
  2  45.0   0.0
  3  46.0   0.0

​ Now I am trying to plot the data where x axis = A y axis = B mean and I have 4 plots one for each D value

Similarly a plot for E mean on a separate plot

I tried a couple of things but the main issue I face is groupby creates a hash table like structure.

enter image description here

2 Answers 2

1

Use unstack on result:

result2 = result.unstack()
reuslt2

enter image description here

Then B.plot()

result2.B.plot()

enter image description here

And E.plot()

result2.E.plot()

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

That worked like a charm. Just a followup. How do I enable minor ticks in this plot. I don't see an option in pandas library. Also is there a way to not show them as i am trying to save them to a file using plt.savefig
I don't know off top of my head, but I'm sure there are plenty of SO question/answers for it already
0

How about something like this:

import matplotlib.pyplot as pp

data = A.groupby(['D','A'])['E','B'].mean().reset_index()

#For the plot of B vs A
fig, ax = pp.subplots()
for value, groups in data.groupby('D'):
     ax.plot(group.A,group.B)
pp.show()

1 Comment

Hi that just plotted one mode to the plot.. Not sure why

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.