0

I have a Dataframe and I slice the Dataframe into three subsets. Each subset has 3 to 4 rows of data. After I slice the data frame into three subsets, I plot them using Matplotlib.

The problem I have is I am not able to create a plot where each subplot is plotted using sliced DataFrame. For example, in a group of three in a set, I have only one of the plots (last subplot) plotted where there is no data for the remaining two plots initial sets in a group. it looks like the 'r' value does not pass to 'r.plot' for all three subplots.

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
df = pd.DataFrame(np.random.randn(10, 4), columns=list('ABCD'))

df['key1'] = 0
df.key1.iloc[0:3] = 1
df.key1.iloc[3:7] = 2
df.key1.iloc[7:] = 3

df_grouped = df.groupby('key1')

for group_name, group_value in df_grouped:
     rows, columns = group_value.shape
     fig, axes = plt.subplots(rows, 1, sharex=True, sharey=True, figsize=(15,20))
    for i,r in group_value.iterrows():
        r = r[0:columns-1]
        r.plot(kind='bar', fill=False, log=False)
5
  • thanks for providing a reproduceable example with random data. however, I can't quite reproduce this because the rows variable is not defined within this example. can you please clarify the value of rows? Commented Jun 12, 2017 at 23:33
  • The rows are the values in the index A,B,C and D created by the random number generator. This code should produce an output with just one of the subplot filled in each subset. The first subset is made of 3 rows of data, the second set contains four rows and third set contains three rows of data. Each row of data has 4 values, with the index of a, b, c and d. Commented Jun 12, 2017 at 23:34
  • sorry I should have been more clear. you define your rows variable in this line: rows, columns = group_value.shape, which doesn't come until three lines after you try to use it with this line: fig, axes = plt.subplots(rows, 1, sharex=True, sharey=True, figsize=(15,20)). To get this code example to run, rows has to be defined before its used. That's the error I'm getting trying to run your code. Commented Jun 12, 2017 at 23:36
  • Yes you are right. It should be moved as you suggest. However, in my subplots only one of them are plotted. That is the issue I am trying to solve. Thanks. Commented Jun 12, 2017 at 23:39
  • I also added pyplot import Commented Jun 12, 2017 at 23:49

1 Answer 1

1

I think you might want what I call df_subset to be summarized in some way, but here's a way to plot each group in its own panel.

# Your Code Setting Up the Dataset
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

df = pd.DataFrame(np.random.randn(10, 4), columns=list('ABCD'))

df['key1'] = 0
df.key1.iloc[0:3] = 1
df.key1.iloc[3:7] = 2
df.key1.iloc[7:] = 3


# My Code to Plot in Three Panels
distinct_keys = df['key1'].unique()
fig, axes = plt.subplots(len(distinct_keys), 1, sharex=True, figsize=(3,5)) 

for i, key in enumerate(distinct_keys):
    df_subset = df[df.key1==key]

    # {maybe insert a line here to summarize df_subset somehow interesting?}

    # plot
    axes[i] = df_subset.plot(kind='bar', fill=False, log=False)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this works in the test case. But my data set needs a bit of work. I will work on it and report back here shortly.

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.