三维方柱曲面

代码截图恢复与适配 · 来源与修改说明 · 下载完整代码

   1 """三维方柱曲面
   2 Restored/adapted from supplied screenshots [43, 44, 45].
   3 Source: 小明的代码美学 (as shown in supplied screenshots).
   4 This is a runnable restoration, not a byte-for-byte original source file.
   5 See SOURCE.md for missing inputs and documented corrections.
   6 """
   7 import numpy as np
   8 import pandas as pd
   9 import matplotlib.pyplot as plt
  10 from matplotlib.colors import LinearSegmentedColormap, Normalize, to_hex
  11 from matplotlib.cm import ScalarMappable
  12 from pathlib import Path
  13 # Missing .mplstyle files are replaced only by explicit, portable display defaults.
  14 plt.rcParams.update({'font.sans-serif':['Microsoft YaHei','DejaVu Sans'],
  15  'axes.unicode_minus':False,'figure.facecolor':'white','axes.facecolor':'white',
  16  'axes.spines.top':False,'axes.spines.right':False,'font.size':9})
  17 import plotly.graph_objects as go
  18 def hex_to_rgb(hex_color):
  19     h=hex_color.lstrip('#')
  20     return tuple(int(h[i:i+2],16) for i in (0,2,4))
  21 def rgb_to_hex(rgb):
  22     return '#%02x%02x%02x'%tuple(rgb)
  23 def interpolate_color(value,color_list):
  24     if value<=0:return color_list[0]
  25     if value>=1:return color_list[-1]
  26     n=len(color_list)-1
  27     segment=min(int(value*n),n-1)
  28     local_t=(value-segment/n)*n
  29     rgb1,rgb2=map(hex_to_rgb,color_list[segment:segment+2])
  30     return rgb_to_hex(tuple(int((1-local_t)*a+local_t*b) for a,b in zip(rgb1,rgb2)))
  31 def towers(fig,a,e,pos_x,pos_y,color_list,global_min,global_max):
  32     x_vals=np.linspace(pos_x-a/2,pos_x+a/2,2)
  33     y_vals=np.linspace(pos_y-a/2,pos_y+a/2,2)
  34     x,y,z=np.meshgrid(x_vals,y_vals,[0,e])
  35     color=interpolate_color((e-global_min)/(global_max-global_min) if global_max!=global_min else 0,color_list)
  36     fig.add_trace(go.Mesh3d(x=x.flatten(),y=y.flatten(),z=z.flatten(),alphahull=1,flatshading=True,color=color))
  37     vertices=np.array([[pos_x-a/2,pos_y-a/2,0],[pos_x+a/2,pos_y-a/2,0],
  38       [pos_x+a/2,pos_y+a/2,0],[pos_x-a/2,pos_y+a/2,0],
  39       [pos_x-a/2,pos_y-a/2,e],[pos_x+a/2,pos_y-a/2,e],
  40       [pos_x+a/2,pos_y+a/2,e],[pos_x-a/2,pos_y+a/2,e]])
  41     edges=[[0,1],[1,2],[2,3],[3,0],[4,5],[5,6],[6,7],[7,4],[0,4],[1,5],[2,6],[3,7]]
  42     edge_x,edge_y,edge_z=[],[],[]
  43     for a,b in edges:
  44         edge_x.extend([vertices[a,0],vertices[b,0],None])
  45         edge_y.extend([vertices[a,1],vertices[b,1],None])
  46         edge_z.extend([vertices[a,2],vertices[b,2],None])
  47     fig.add_trace(go.Scatter3d(x=edge_x,y=edge_y,z=edge_z,mode='lines',line=dict(color='white',width=3)))
  48 rows,cols=16,11
  49 x,y=np.linspace(-1,1,cols),np.linspace(-1.7,1.3,rows)
  50 X,Y=np.meshgrid(x,y)
  51 Z=np.exp(-((X+.5)**2+(Y+.5)**2)/.7)*1.5+np.exp(-((X-.5)**2+(Y-.5)**2)/.3)
  52 color_list=['#214e81','#8d9eb2','#cccccc','#dc9fb0','#c2768b']
  53 fig=go.Figure()
  54 for xv,yv,zv in zip(X.flatten(),Y.flatten(),Z.flatten()):
  55     towers(fig,.15,zv,xv,yv,color_list,Z.min(),Z.max())
  56 fig.update_layout(showlegend=False,margin=dict(l=0,r=0,t=0,b=0),scene=dict(
  57     xaxis=dict(title='X',backgroundcolor='#f8f1f1'),yaxis=dict(title='Y',backgroundcolor='#f8f1f1'),
  58     zaxis=dict(title='Z',backgroundcolor='#f8f1f1'),aspectmode='data'))
  59 # Export-safe frame leaves room for outer 3D ticks.
  60 fig.update_layout(margin=dict(l=30,r=30,t=30,b=35))
  61 fig.update_layout(scene_camera=dict(eye=dict(x=1.65,y=1.65,z=1.5)))
  62 fig.show()
  63