I aim to create a plot similar to the image where the '2020 Q4' data is in the same column as '2020'.

So far I was only able to place the 2020 Q4 data simply as an extra column.
The data is provided as a DataFrame like in the code below:
# DataFrame using arrays.
import pandas as pd
# initialize data of lists.
data = {'A':[10, 15, 20, 26, 27, 35, 15],
'B':[20, 25, 32, 33, 50, 52, 8],
'C':[30, 35, 41, 49, 52, 53, 25]}
# Creates pandas DataFrame.
df = pd.DataFrame(data, index =['2015',
'2016',
'2017',
'2018',
'2019',
'2020',
'2020 Q4',])
# plotting the data
df.plot(kind='bar',stacked=True)
CodePudding user response:
You can introduce Q4 values for A, B and C rows and initialise it to zero for all years except 2020. This should give you the desired result.
For example, see this updated code:
# DataFrame using arrays.
import pandas as pd
# initialize data of lists.
data = {'A':[10, 15, 20, 26, 27, 35, 15],
'B':[20, 25, 32, 33, 50, 52, 8],
'C':[30, 35, 41, 49, 52, 53, 25]}
# additional data initialise with zero for all years
add_data = {'AQ4':[0]*5,
'BQ4':[0]*5,
'CQ4':[0]*5}
# take the last element in list A, B, C and append it to add_data dict
add_data['AQ4'].append(data['A'].pop())
add_data['BQ4'].append(data['B'].pop())
add_data['CQ4'].append(data['C'].pop())
# concatenate the 2 dicts
data.update(add_data)
# Creates pandas DataFrame.
df = pd.DataFrame(data, index =['2015',
'2016',
'2017',
'2018',
'2019',
'2020'])
# plotting the data
df.plot(kind='bar',stacked=True)
Note that I have removed 2020 Q4 index when creating the data frame.
CodePudding user response:
Two problems have to be addressed here. First, one has to transpose the Q4 data into the row above. Second, the corresponding columns A A-Q4 etc. need similar colors to make it clear that they belong to the same category. 
Restrictions: It relies on your data structure that the rows are already sorted and the last two rows are "Year" and "Year Q4". Tab20 limits the use to 10 columns A, B, C,...,J because beginning with K, the colors will not be unique. However, stacked bar graphs with more than 10 categories should be outlawed anyhow.
