Dendrogram
Food Nutrition Clusters Light
Nutritional similarity clustering of foods
Output
Python
import matplotlib.pyplot as plt
import numpy as np
from scipy.cluster.hierarchy import dendrogram, linkage, set_link_color_palette
np.random.seed(987)
foods = ['Apple', 'Banana', 'Orange', 'Broccoli', 'Spinach', 'Carrot',
'Chicken', 'Beef', 'Salmon', 'Rice', 'Quinoa', 'Oats', 'Almonds', 'Eggs']
nutrition = np.random.rand(len(foods), 6) * 100
Z = linkage(nutrition, method='ward')
fig, ax = plt.subplots(figsize=(14, 7), facecolor='#ffffff')
ax.set_facecolor('#ffffff')
set_link_color_palette(['#6CF527', '#F5B027', '#27D3F5', '#F5276C', '#4927F5'])
dn = dendrogram(Z, labels=foods, leaf_rotation=45, leaf_font_size=11,
color_threshold=0.65*max(Z[:,2]), above_threshold_color='#d1d5db', ax=ax)
ax.set_title('Food Nutritional Profile Clustering', fontsize=15,
color='#1f2937', fontweight='bold', pad=18)
ax.set_xlabel('Food Item', fontsize=11, color='#374151')
ax.set_ylabel('Nutritional Distance', fontsize=11, color='#374151')
ax.tick_params(axis='both', colors='#374151', labelsize=10)
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_color('#e5e7eb')
ax.spines['bottom'].set_color('#e5e7eb')
ax.yaxis.grid(True, color='#f9fafb', linewidth=0.8)
plt.tight_layout()
plt.show()
Library
Matplotlib
Category
Statistical
More Dendrogram examples
☕