3D Bar Chart

Nutrient Content 3D

3D visualization of nutrient content by food and vitamin type

Output
Nutrient Content 3D
Python
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.colors import LinearSegmentedColormap
import matplotlib.cm as cm

fig = plt.figure(figsize=(12, 8), facecolor='white')
ax = fig.add_subplot(111, projection='3d')
ax.set_facecolor('white')

foods = 6
nutrients = 4
xpos = np.arange(foods)
ypos = np.arange(nutrients)
xpos, ypos = np.meshgrid(xpos, ypos)
xpos = xpos.flatten()
ypos = ypos.flatten()
zpos = np.zeros_like(xpos)

dx = dy = 0.65
np.random.seed(2222)
dz = np.random.randint(5, 100, size=24)

cmap = LinearSegmentedColormap.from_list('', ['#27F5B0', '#6CF527', '#D3F527'])
norm = plt.Normalize(5, 100)
bar_colors = [cmap(norm(v)) for v in dz]

ax.bar3d(xpos, ypos, zpos, dx, dy, dz, color=bar_colors, alpha=0.9, edgecolor='#000000', linewidth=0.3)

sm = cm.ScalarMappable(cmap=cmap, norm=norm)
sm.set_array([])
cbar = fig.colorbar(sm, ax=ax, shrink=0.5, aspect=15, pad=0.1)
cbar.set_label('% Daily Value', fontsize=10, color='#1f2937')
cbar.ax.tick_params(colors='#000000')

ax.set_xlabel('Food', fontsize=11, color='#1f2937', labelpad=10)
ax.set_ylabel('Nutrient', fontsize=11, color='#1f2937', labelpad=10)
ax.set_zlabel('% DV', fontsize=11, color='#1f2937', labelpad=10)
ax.set_title('Nutrient Content Analysis', fontsize=14, color='#1f2937', fontweight='bold', pad=20)

ax.set_xticks(range(6))
ax.set_xticklabels(['Spinach', 'Salmon', 'Eggs', 'Milk', 'Almonds', 'Chicken'], fontsize=7)
ax.set_yticks(range(4))
ax.set_yticklabels(['Vit A', 'Vit D', 'Iron', 'Calcium'])
ax.tick_params(colors='#000000', labelsize=9)

ax.xaxis.pane.fill = False
ax.yaxis.pane.fill = False
ax.zaxis.pane.fill = False
ax.xaxis.pane.set_edgecolor('#000000')
ax.yaxis.pane.set_edgecolor('#000000')
ax.zaxis.pane.set_edgecolor('#000000')
ax.grid(True, alpha=0.5, linewidth=0.5)
ax.xaxis._axinfo['grid']['color'] = '#000000'
ax.yaxis._axinfo['grid']['color'] = '#000000'
ax.zaxis._axinfo['grid']['color'] = '#000000'
ax.xaxis._axinfo['tick']['color'] = '#000000'
ax.yaxis._axinfo['tick']['color'] = '#000000'
ax.zaxis._axinfo['tick']['color'] = '#000000'
ax.xaxis.line.set_color('#000000')
ax.yaxis.line.set_color('#000000')
ax.zaxis.line.set_color('#000000')

plt.tight_layout()
plt.show()
Library

Matplotlib

Category

3D Charts

Did this help you?

Support PyLucid to keep it free & growing

Support