2

This is related to post Rolling Difference using Pandas

Now that I have this dataframe below, i am trying to visualize this.

Item    Add Subtracts   Month   Net_Items   Monthly_Available_Items
C   68  30  1   38  38
C   58  34  2   24  62
C   64  47  3   17  79
C   263 81  4   182 261
C   95  104 5   -9  252
C   38  63  6   -25 227
C   115 95  7   20  247
C   97  112 8   -15 232

The code and graph are below:

plt.figure(figsize=(20,10))
fig, ax1 = plt.subplots(figsize = (20,10))
ax1 = sns.pointplot(x='Month', y='value', hue='variable',data=stack_df)
ax1.legend(loc = 'upper left')
ax2 = sns.barplot(x = 'Month', y = 'Monthly_Available_Items', data = 
stack_df, color = 'purple')
ax1.set_ylabel("Count of Items")

Comparison of Add and subtracts vs available monthly inventory: Comparison of Add and subtracts vs available monthly inventory

Questions:

  1. How can i add the legend to the ax2 axis. This represent the Monthly Available items for each month. i tried

    ax2.legend() but it not work

  2. How can i create similar plots for each item(A,B,C,D,E)

  • Does this help re: question 1?: https://stackoverflow.com/questions/5484922/secondary-axis-with-twinx-how-to-add-to-legend – ZaxR Aug 31 '18 at 02:06

1 Answers1

0

You can add a legend by specifying the label

ax2 = sns.barplot(x = 'Month', y = 'Monthly_Available_Items', 
                             data = stack_df, color = 'purple', 
                             label = "Monthly_Available_Items")
ax2.legend() # will show the legend for the barplot

If you want to plot multiple plots based on Item column you could use groupby to plot.

Assuming this is our dataframe df:

  Item  Add  Subtracts  Month   Net_Items   Monthly_Available_Items
0   C   68     30        1        38             38
1   D   58     34        2        24             62
2   C   64     47        3        17             79
3   C   263    81        4       182            261
4   D   95    104        5        -9            252
5   D   38     63        6       -25            227
6   D   115    95        7        20            247
7   C   97    112        8       -15            232

The easiest way to plot multiple plots for each unique value from Item column would be using pandas plot method. We'll first use melt and then groupby.

melt = df.melt(id_vars=('Item', 'Month', 'Monthly_Available_Items'), 
               value_vars=['Add','Subtracts'])

# sort the melted df by item column
melt.sort_values("Item", inplace=True)

# using groupby to plot by item column.
ax = df.groupby("Item").plot(x='Month', y = "Monthly_Available_Items",
                        kind='bar', color='purple')

# list of axes generated by ax
axes = [i for i in ax] 

# list of unique items from item column eg.,C,D
items = melt.Item.unique()

for i in range(len(c)):
    sns.pointplot(x='Month', y='value', hue='variable', 
                       data=melt[melt.Item == c[i]], ax=axes[i])

# customize
axes[0].set_title("Plot for Item C")
axes[1].set_title("Plot for Item D")
plt.show()

enter image description here enter image description here

Abhi
  • 4,068
  • 1
  • 16
  • 29
  • This worked great! Just one more question, how can i make these bigger? – user10297084 Sep 04 '18 at 21:45
  • @user10297084 You're welcome. Just use `figsize` parameter `ax = df.groupby("Item").plot(x='Month', y = "Monthly_Available_Items", kind='bar', color='purple', figsize = (width, height))` – Abhi Sep 04 '18 at 22:34
  • Here width and height are integers. Try something like `figsize = (9, 7)` – Abhi Sep 04 '18 at 22:36