I got a problem trying to plot the larger version of this data (it has weeks till today) having all data in that plot space makes not legible the bars labels (3rd image as reference), I don't know what the problem is because it also plot a void figure with the size I gave. Pd. This was my best trial to make a combination of stacked and grouped chart, if someona has a better way to make it, please tell
Chart to resize
Void figure
import pandas as pd
import matplotlib.pyplot as plt
# Provided data
data = {
'TYPE': ['a', 'b', 'b', 'a', 'a', 'b', 'a'],
'PRODUCT': ['Helium', 'Silver', 'Copper', 'Hidrogen', 'Helium', 'Silver', 'Silver'],
'Amount': [62, 15, 70, 4, 3, 4, 25],
'WEEK': [1, 1, 1, 1, 2, 2, 1]
}
# Create DataFrame
df_ini = pd.DataFrame(data)
# Plotting the bar chart grouped by week and showing all the products
df_pivot = df_ini.pivot_table(
index=['WEEK','TYPE'],
columns='PRODUCT',
values='Amount',
aggfunc='sum',
fill_value=0
)
# Plotting the bar chart
plt.figure(figsize=(10, 6))
ax = df_pivot.plot(kind='bar', stacked=True)
# Custom x-axis labels
new_labels = [f'{week} / {type}' for week, type in df_pivot.index]
ax.set_xticklabels(new_labels,rotation=45,ha='right')
# Adding labels and title
plt.title('Claims by week')
plt.xlabel('Week')
plt.ylabel('Claims')
plt.legend(title='Product')
plt.tight_layout()
plt.show()
Remove the below line from your code. It is creating one extra blank Figure.
plt.figure(figsize=(10, 6))