如何解决Pluto Notebook中ThreeBodyTB晶体结构绘图转换错误?
解决ThreeBodyTB晶体结构无法用Plots绘图的问题
你遇到的错误是因为Plots.jl没有内置支持ThreeBodyTB的crystal自定义类型的绘图接口,直接调用plot()会无法识别该类型的数据。以下是两种可行的解决方案:
方案1:使用ThreeBodyTB内置的绘图函数
ThreeBodyTB库提供了专门用于绘制晶体结构的plot_crystal函数,替换原生plot()即可:
using Plots, ThreeBodyTB begin A = [0 2.73 2.73; 2.73 0 2.73; 2.73 2.73 0] #angstrom lattice vectors pos = [0 0 0; 0.25 0.25 0.25] #crystal coordinates atoms = ["Al", "P"] #strings or symbols are fine c = makecrys(A, pos, atoms) end c222 = c * [2,2,2] # 调用库内置的晶体绘图函数 plot_crystal(c222)
方案2:手动提取坐标用Plots绘制
如果需要更自定义的绘图样式,可以手动从crystal结构体中提取原子的笛卡尔坐标,再用Plots的散点图和线图绘制:
using Plots, ThreeBodyTB begin A = [0 2.73 2.73; 2.73 0 2.73; 2.73 2.73 0] #angstrom lattice vectors pos = [0 0 0; 0.25 0.25 0.25] #crystal coordinates atoms = ["Al", "P"] #strings or symbols are fine c = makecrys(A, pos, atoms) end c222 = c * [2,2,2] # 转换晶体坐标为笛卡尔坐标 cart_coords = [c222.latvecs * pos for pos in eachcol(c222.positions)] x = [coord[1] for coord in cart_coords] y = [coord[2] for coord in cart_coords] z = [coord[3] for coord in cart_coords] # 绘制原子散点 scatter(x, y, z, label=reshape(c222.atoms, :), markersize=10, legend=:outerright) # 绘制晶格框架 lat = c222.latvecs for i in 0:1, j in 0:1, k in 0:1 start = [i,j,k]' * lat for vec in eachcol(lat) end_pt = start + vec plot!([start[1], end_pt[1]], [start[2], end_pt[2]], [start[3], end_pt[3]], color=:black, label="") end end
内容的提问来源于stack exchange,提问作者İdil Bali
相关产品推荐
相关产品推荐

