三维体绘制

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

   1 """三维体绘制
   2 Restored/adapted from supplied screenshots [36].
   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 from scipy import ndimage
  19 np.random.seed(0)
  20 l=30
  21 X,Y,Z=np.mgrid[:l,:l,:l]
  22 vol=np.zeros((l,l,l))
  23 pts=(l*np.random.rand(3,15)).astype(int)
  24 vol[tuple(indices for indices in pts)]=1
  25 vol=ndimage.gaussian_filter(vol,4)
  26 vol/=vol.max()
  27 fig=go.Figure(go.Volume(x=X.flatten(),y=Y.flatten(),z=Z.flatten(),value=vol.flatten(),
  28     isomin=.2,isomax=.7,opacity=.1,surface_count=25,colorscale=[[0.,'#3768b0'],[.5,'#e9d1ab'],[1.,'#c3476a']] ))
  29 fig.update_layout(scene={a:dict(title=a[0].upper(),backgroundcolor='#f2e2e4') for a in ['xaxis','yaxis','zaxis']},margin=dict(l=0,r=0,t=0,b=0))
  30 # Export-safe frame leaves room for outer 3D ticks.
  31 fig.update_layout(margin=dict(l=30,r=30,t=30,b=35))
  32 fig.update_layout(scene_camera=dict(eye=dict(x=1.65,y=1.65,z=1.5)))
  33 fig.show()
  34