how to plot multiple categories with different data lengths on the same bar chart in python?
Here is my code:
import pandas as pd
import matplotlib.pyplot as plt
Type_X = [200000, 150000]
Type_Y = [168000, 120000]
Comb = [192000]
Cat = ['With Time Constraints', 'With Production Constraints']
Cat2 = ['Optimised Combination of Devices']
df = pd.DataFrame({'Type X': Type_X,
'Type Y': Type_Y,}, index=Cat)
df2 = pd.DataFrame({'Comb' : Comb}, index=Cat2)
print(df)
print(df2)
ax = plt.subplots()
df.plot.barh(figsize = (15, 8), color={"Type X": "lightblue", "Type Y": "purple"})
df2.plot.barh(figsize = (15, 8), color={'Comb' : 'green'})
#annotate bars in bar plot
for container in ax.containers:
ax.bar_label(container)
plt.title('SEC Weekly Profits of Devices under different Constraints', fontdict = {'fontsize': 20,
'fontweight': 'bold',
'color': 'black',
'verticalalignment': 'baseline',
'horizontalalignment': 'center'})
# export the file
# plt.savefig("SEC_Weekly_Profit_Cons", dpi = 300)
# Hide the right and top spines
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
# plot show
plt.show()
Here is the kind of chart I expect to see: