Problem: I want to make a stacked bar chart where each value of the stack is sorted by maximum (bottom) to minimum.
I have a DataFrame that contains a time series in one direction and 12 different categories in the other, something like this:
bz = ['NO1','NO2','NO3','NO4','NO5','DK1','DK2','FI','SE1','SE2','SE3','SE4']
df = pd.DataFrame(columns = bz, index = range(0,24,1), data=np.random.randint(0,100,size=(24, 12)))
I was unable to sort and plot the values in the same line of code so I reseorted to hard coding each hour and sorting them by highest to lowest value like so:
hour1 = df.loc[:,0].sort_values(ascending = True)
hour2 = df.loc[:,1].sort_values(ascending = True)
hour3 = df.loc[:,2].sort_values(ascending = True)
...
But then I couldn't figure out how to plot them in a stack properly.
Desired outcome:
Each category in bz
is stacked and sorted by value (max at the bottom, min at the top) for each successive hour. Where each x-value is one of the variables hour1,hour2
etc.