Keep order in plot of pandas groupby

qfd

I have a dataframe that looks like the below

univ  date            ms      cs
 C    11/01/1977      0.2     0.1
 C    11/02/1977      0.3     0.4
 B    11/01/1977      0.6     -0.1
 B    11/02/1977      0.55    -0.5
 A    11/01/1977      0.23    0.2
 A    11/02/1977      0.12    0.15
 etc

This is what I currently have

fig, axes = plt.subplots(1, 3, figsize=(15, 5))

for (universe, group), ax in zip(df.groupby([df.univ]), axes.flatten()):
    group.plot(x='ms', y='cs', kind='scatter', ax=ax, title=universe)
    ax.set_xlim(0, 1)
    ax.set_ylim(0, 1)

This produces 3 scatter plots, with limits from 0 to 1. What I would like to do is keep the order of the plots titled as C, B, A, such that it gets ordered by A,B,C.

Thanks for your help!

Alexander

The problem is with the groupby which returns the items in sorted order by default. Just set the flag sort=False.

for (universe, group), ax in zip(df.groupby([df.univ], sort=False), axes.flatten()):
    group.plot(x='ms', y='cs', kind='scatter', ax=ax, title=universe)
    ax.set_xlim(0, 1)
    ax.set_ylim(0, 1)

You could also return them in any order you desired.

gb = df.groupby('univ')
desired_order = ('B', 'A', 'C')
for (universe, group), ax in zip(((u, gb.get_group(u)) for u in desired_order), 
                                 axes.flatten()):
    ...

And btw, you should only have one question per post.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

GroupBy and plot with pandas

From Dev

Pandas groupby and selector order

From Java

Pandas groupby two columns and plot

From Dev

Pandas groupby object in legend on plot

From Dev

Pandas groupby two columns and plot

From Dev

Pandas groupby plot gives first plot twice

From Dev

Pandas groupby plot gives first plot twice

From Dev

Reverse legend order pandas plot

From Dev

Pandas plot bar order categories

From Java

How to GroupBy a Dataframe in Pandas and keep Columns

From Dev

Multiindex pandas groupby + aggregate, keep full index

From Dev

Keep a column with a categorical variable in Pandas with groupby and mean()

From Dev

pandas groupby column to list and keep certain values

From Dev

Keep all indexes in multilevel pandas groupby

From Dev

Python Pandas - Groupby and Mean, but keep column name

From Dev

How to create Pandas groupby plot with subplots?

From Dev

pandas - plot according to groupby index level

From Dev

python pandas groupby plot with sorted date as xtick

From Dev

Pandas GroupBy object is not 'serializable' by Plot.ly

From Dev

Create Contour Plot from Pandas Groupby Dataframe

From Dev

How to plot aggregate results after groupby in Pandas?

From Dev

How to plot aggregate results after groupby in Pandas?

From Dev

Pandas loop over groupby and plot each group

From Dev

Pandas: How to label groupby data in plot?

From Dev

Sort by certain order (Situation: pandas DataFrame Groupby)

From Dev

Sort by certain order (Situation: pandas DataFrame Groupby)

From Dev

Date order in bokeh plot with pandas dataframe

From Dev

pandas - keep only True values after groupby a DataFrame

From Dev

Pandas Groupby, How to keep additional column not in the level or the analysis column?

Related Related

  1. 1

    GroupBy and plot with pandas

  2. 2

    Pandas groupby and selector order

  3. 3

    Pandas groupby two columns and plot

  4. 4

    Pandas groupby object in legend on plot

  5. 5

    Pandas groupby two columns and plot

  6. 6

    Pandas groupby plot gives first plot twice

  7. 7

    Pandas groupby plot gives first plot twice

  8. 8

    Reverse legend order pandas plot

  9. 9

    Pandas plot bar order categories

  10. 10

    How to GroupBy a Dataframe in Pandas and keep Columns

  11. 11

    Multiindex pandas groupby + aggregate, keep full index

  12. 12

    Keep a column with a categorical variable in Pandas with groupby and mean()

  13. 13

    pandas groupby column to list and keep certain values

  14. 14

    Keep all indexes in multilevel pandas groupby

  15. 15

    Python Pandas - Groupby and Mean, but keep column name

  16. 16

    How to create Pandas groupby plot with subplots?

  17. 17

    pandas - plot according to groupby index level

  18. 18

    python pandas groupby plot with sorted date as xtick

  19. 19

    Pandas GroupBy object is not 'serializable' by Plot.ly

  20. 20

    Create Contour Plot from Pandas Groupby Dataframe

  21. 21

    How to plot aggregate results after groupby in Pandas?

  22. 22

    How to plot aggregate results after groupby in Pandas?

  23. 23

    Pandas loop over groupby and plot each group

  24. 24

    Pandas: How to label groupby data in plot?

  25. 25

    Sort by certain order (Situation: pandas DataFrame Groupby)

  26. 26

    Sort by certain order (Situation: pandas DataFrame Groupby)

  27. 27

    Date order in bokeh plot with pandas dataframe

  28. 28

    pandas - keep only True values after groupby a DataFrame

  29. 29

    Pandas Groupby, How to keep additional column not in the level or the analysis column?

HotTag

Archive