-2

I want to use subplot to make the following graph for different variables:

plot

The code I'm using to generate that single graph is:

axis = df.groupby('Hour')[['var1']].mean().plot(figsize = (10,5), marker = 'o', color = 'r')
axis.set_title('var1')

Due to the groupby, I don't know how to use the subplot in this situation to make the multiple graphs for the different variables. I have tried different thing but none of them has worked. Can you help me with this?

Thank you in advance!!

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
  • This question is not reproducible without the **raw dataframe** (or representative example), before the groupby. Please see [How to provide a reproducible copy of your DataFrame using `df.head(30).to_clipboard(sep=',')`](https://stackoverflow.com/q/52413246/7758804), then **[edit] your question**, and paste the clipboard into a code block. Always provide a [mre] **with code, data, errors, current output, and expected output, as [formatted text](https://stackoverflow.com/help/formatting)**. If relevant, plot images are okay. – Trenton McKinney Sep 16 '21 at 03:41
  • Not a Pandas person, but I think you can reuse the same Axes (as in `fig, ax=plt.subplots()` as an argument to the `df.groupby … .plot()` command. – gboffi Sep 18 '21 at 19:33

1 Answers1

0

You may want to checkout pandas.melt()

melt_df = df \
    .groupby('Hour')['var1'] \
    .mean() \
    .reset_index() \
    .melt(id_vars=['Hour'], value_vars=['var1'])

This will result in going from a wide file to a long file where all the variables are within one column.