Skip to content

Commit 328fe7b

Browse files
authored
Add Composition Pie Charts (#50)
1 parent 3943a98 commit 328fe7b

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

tutorials/compositionPlots.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ def determine_carbon_number(compound):
7272
# Apply the function to the column and append as a new column
7373
df["nC"] = df.Compounds.apply(determine_carbon_number)
7474

75+
# Determine relative weight % of each family
76+
family_weights = df.groupby("Family")["Weight %"].sum()
77+
78+
# Print the sum of weight % for each family
79+
print("\n" + "=" * 45)
80+
print("Relative Weight % of Each Compound Family")
81+
print(f"Fuel: {fuel_name}")
82+
print("=" * 45)
83+
for family, weight in family_weights.items():
84+
print(f" {family:<20} {weight:>8.2f}%")
85+
print("-" * 45)
86+
print(f" {'Total':<20} {family_weights.sum():>8.2f}%")
87+
print("=" * 45 + "\n")
88+
7589
# Remove rows <= 0.01 in weight % column at max(nC)
7690
df = df[df["Weight %"] > 0.01]
7791

@@ -112,4 +126,27 @@ def determine_carbon_number(compound):
112126
plt.title("Fuel Composition", fontsize=16, fontweight="bold")
113127
plt.legend(fontsize=14)
114128
plt.tight_layout()
129+
130+
# Plot pie chart of family weights
131+
plt.figure(figsize=(7, 5))
132+
plt.pie(
133+
family_weights,
134+
labels=None,
135+
autopct="%1.1f%%",
136+
startangle=140,
137+
colors=[colors[family] for family in family_names],
138+
textprops={"fontsize": 16, "weight": "bold", "color": "white"},
139+
)
140+
legend_handles = [
141+
plt.Rectangle((0, 0), 1, 1, fc=colors[family]) for family in family_names
142+
]
143+
plt.legend(
144+
legend_handles,
145+
family_names,
146+
loc="center left",
147+
bbox_to_anchor=(1, 0.5),
148+
fontsize=14,
149+
)
150+
plt.axis("equal")
151+
plt.tight_layout()
115152
plt.show()

0 commit comments

Comments
 (0)