FadeLead / figs /draw /motivation_3d.py
YuhengWu's picture
Upload folder using huggingface_hub
40c9ce2 verified
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import font_manager
font_path = '/Users/yuheng/Library/Fonts/Carlito-Bold.ttf'
prop = font_manager.FontProperties(fname=font_path)
# Create figure and 3D subplot
fig = plt.figure(figsize=(6, 4))
ax = fig.add_subplot(projection='3d')
# Define positions and dimensions for 6 bars - with larger gap between red and blue sections
xpos = np.array([0, 0, 0, 0, 0, 0]) # X positions (all aligned at 0)
ypos = np.array([0, 0.5, 1.3, 1.8, 2.3, 2.8]) # Y positions - larger gap between 0.5 and 1.3
zpos = np.array([0.5, 0.5, 0.5, 0.5, 0.5, 0.5]) # Z positions (base level at 0.5)
# Bar dimensions
dx = np.array([0.2, 0.2, 0.2, 0.2, 0.2, 0.2]) # Width (uniform)
dy = np.array([0.4, 0.4, 0.4, 0.4, 0.4, 0.4]) # Depth (uniform)
dz = np.array([0.3669, 0.3356, 0.3125, 0.3439, 0.317, 0.338]) # Heights adjusted for 0.5 base
#
# Create the 3D bars with different colors
colors = ['#D77071', '#D77071', '#6888F5', '#6888F5', '#6888F5', '#6888F5'] # First 2 red, last 4 blue
# colors = ['#F09BA0', '#F09BA0', '#7399f4', '#7399f4', '#7399f4', '#7399f4']
for i in range(6):
ax.bar3d(xpos[i], ypos[i], zpos[i], dx[i], dy[i], dz[i],
color=colors[i], alpha=0.8, edgecolor='black', linewidth=1)
# Set the viewing angle
ax.view_init(elev=10, azim=10)
ax.set_box_aspect([1, 4, 4]) # [x, y, z] ratios - makes x thinner relative to y and z
# Set axis limits - adjusted for new bar positions
ax.set_xlim(-0, 0.2)
ax.set_ylim(-0, 3.2) # Extended to accommodate new positions
ax.set_zlim(0.5, 1) # Display range from 0.5 to 1.0
# Hide x axis ticks
ax.set_xticks([])
# Set y-axis ticks and custom labels - updated positions
ax.set_yticks([0, 0.5, 1.3, 1.8, 2.3, 2.8]) # Tick positions matching new bar positions
ax.set_yticklabels(["BG", "FG", "0.3%", "1%", "10%", "90%"]) # Custom labels
# Set z-axis ticks to show values from 0.5 to 1.0
ax.set_zticks([0.5, 0.6, 0.7, 0.8, 0.9, 1.0])
for tick in ax.get_xticklabels():
tick.set_fontproperties(prop)
tick.set_fontsize(14)
for tick in ax.get_yticklabels():
tick.set_fontproperties(prop)
tick.set_fontsize(14)
for tick in ax.get_zticklabels():
tick.set_fontproperties(prop)
tick.set_fontsize(14)
# Enable grid
ax.grid(True)
# Set custom pane colors (background of the 3D plot)
ax.xaxis.pane.fill = False
ax.yaxis.pane.fill = False
ax.zaxis.pane.fill = False
# Set pane edge colors
ax.xaxis.pane.set_edgecolor('gray')
ax.yaxis.pane.set_edgecolor('gray')
ax.zaxis.pane.set_edgecolor('gray')
# Fill grid areas with different colors - no gap between sections
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
# FG/BG section (y=0 to y=1.2) - red fill, extended to meet blue section
vertices_fg_bg = [[(0, 0, 0.5), (0, 1.2, 0.5), (0, 1.2, 1.0), (0, 0, 1.0)]]
poly_fg_bg = Poly3DCollection(vertices_fg_bg, alpha=0.2, facecolor='red', edgecolor='red')
ax.add_collection3d(poly_fg_bg)
# Other sections (y=1.2 to y=3.2) - blue fill
vertices_other = [[(0, 1.2, 0.5), (0, 3.2, 0.5), (0, 3.2, 1.0), (0, 1.2, 1.0)]]
poly_other = Poly3DCollection(vertices_other, alpha=0.2, facecolor='blue', edgecolor='blue')
ax.add_collection3d(poly_other)
# Draw custom grid lines with different colors - made transparent
# FG and BG sections (y=0 to y=1.2) - red color, extended
for z in [0.5, 0.6, 0.7, 0.8, 0.9, 1.0]:
ax.plot([0, 0], [0, 1.2], [z, z], color='red', alpha=0.0, linewidth=1)
# Other sections (y=1.2 to y=3.2) - blue color
for z in [0.5, 0.6, 0.7, 0.8, 0.9, 1.0]:
ax.plot([0, 0], [1.2, 3.2], [z, z], color='blue', alpha=0.0, linewidth=1)
# Vertical grid lines
for y in [0, 0.5, 1.3, 1.8, 2.3, 2.8]:
if y <= 1.2: # FG/BG section - extended boundary
ax.plot([0, 0], [y, y], [0.5, 1.0], color='red', alpha=0.0, linewidth=1)
else: # Other sections
ax.plot([0, 0], [y, y], [0.5, 1.0], color='blue', alpha=0.0, linewidth=1)
plt.tight_layout()
# plt.show()
plt.savefig("fig/motivation.pdf", dpi=1200)