Stacked Bar Chart in Python
A stacked bar chart is a type of data visualization used to display the distribution of multiple categorical variables within different groups. It is essentially a bar chart where the bars are divided into segments, each representing a different category within a single group.
In a stacked bar chart, each bar represents a specific group or category, and the length of the bar is divided into segments, each corresponding to a different sub-category or level within that group. The height of the segments represents the relative proportion or value of each sub-category within the group. By stacking these segments on top of each other, the chart visually illustrates how the proportions of different sub-categories contribute to the overall value of each group.
Stacked bar charts are particularly useful when you want to show how different categories are composed of sub-categories, and how these compositions vary across different groups. They allow for easy comparison of both the overall values of groups as well as the relative contributions of sub-categories within those groups.
However, it’s important to note that stacked bar charts can sometimes be challenging to interpret accurately when dealing with a large number of sub-categories or when the proportions are very similar, as it might become difficult to differentiate between the segments. Additionally, if the total values of the groups vary significantly, it can lead to misleading visualizations. In such cases, alternatives, like grouped bar charts or other types of visualizations, might be more suitable.
You can find the codes for the graph above. This graph is from an article written by me. For sure, you should organize the code according to your data. You can also find the codes on Github.
import pandas as pd
import matplotlib.pyplot as plt
path= "Path of your document"
df = pd.read_excel(path, sheet_name='All_1')
df_specific = pd.read_excel(path, sheet_name='Enzyme_1', usecols = [6])
df_specific_2= df_specific.dropna()
Specific_Error = df_specific_2['St_Dev_S'].tolist()
df_total = pd.read_excel(path, sheet_name='Enzyme_1', usecols = [3])
df_total_2= df_total.dropna()
Total_Error = df_total_2['St_Dev_T'].tolist()
plt.figure(figsize=(15,7));
width = 0.65
p1 = plt.bar(df['Gene'], df['Specific_Activity'], width, yerr = Specific_Error, color=['blue'], capsize=5)
p2 = plt.bar(df['Gene'], df['Total_Activity'], width, yerr = Total_Error, color='r', capsize=5)
plt.xlabel("Enzymes")
plt.ylabel("Enzyme Activity")
plt.legend(["Total Activity (U/ml)", "Specific Activity (U/mg)"])
plt.bar_label(p1, padding=5, fmt='%.1f', fontsize=9)
plt.bar_label(p2, padding=6, fmt='%.1f', color='w', fontsize=9)
plt.axhline(y=35, color='red', linestyle='dotted', linewidth=1.5)
plt.savefig("Enzyme_Avtivity.svg", dpi=300)
plt.show()
To inspire me to create more content, kindly support me by following me on both Medium and Instagram.