Reputation: 87
I'm trying to replicate work done in an excel pivot table. I have 3 variables (idle time, and total time, and customer). I want to make a bar plot that is stacked(overlaps) but is not a sum of all variables.
For example: if..
Total_time = 10
Idle_time = 3
I want the bar graph to show the max value of idle time and total time for each customer.
So far when I try to use stacked=True, it just stacks Total & Idle time and creates a bar graph with a max value of 10+3=13. I want two separate bars overlapping each other where one bar maxes at 10 and the other at 3.
data = df.pivot_table(df,index=['Customer'], aggfunc=np.sum)
ax = data[['Total time [hours]', 'Idle time [hours]']].plot.bar(stacked=True)
ax.set_ylabel("Miles")
ax.set_title("Total Miles Per Customer")
plt.show()
I've uploaded a screenshot of the excel version, and pandas version for further illustration.
Thanks in advance.
UPDATE: Ted with the solution below.
Upvotes: 1
Views: 1295
Reputation: 61967
One simple solution is to plot them separately with the idle time last
df['Total time [hours]'].plot.bar(color='r')
df['Idle time [hours]'].plot.bar(color='b')
Another solution is to create a new column that is equal to the Total time minus the Idle Time and then do a stacked bar plot. You can rename the columns from there
df['Extra_Time'] = df['Total time [hours]'] - df['Idle time [hours]']
data[['Idle time [hours]', 'Extra_Time']].plot.bar(stacked=True)
Upvotes: 3