Matplotlib 3D绘图:方位角切换轴标签与禁止负仰角问题咨询
问题1:方位角旋转90度时修改对应轴标签
你可以通过绑定3D画布的视图更新事件,实时读取当前方位角ax.azim的数值,动态切换轴标签:
- 方位角azim的取值范围是0-360度,默认初始值为-60度
- 当检测到azim落在90±5度的区间内(允许旋转时的小幅误差),就将对应轴标签替换为West,其他角度下恢复原有标签即可
问题2:阻止出现负仰角
同样在视图更新事件里做判断即可,仰角通过ax.elev获取,只要检测到该值小于0,就强制赋值为0,就能完全避免负仰角出现。
以下是修改后的完整可运行代码:
import numpy as np from mpl_toolkits.mplot3d.art3d import Poly3DCollection, Line3DCollection import matplotlib.pyplot as plt points = np.array([[-1, -1, -1], [1, -1, -1 ], [1, 1, -1], [-1, 1, -1], [-1, -1, 1], [1, -1, 1 ], [1, 1, 1], [-1, 1, 1]]) Z = points Z = 10.0*Z fig = plt.figure() ax = fig.add_subplot(111, projection='3d') r = [-1,1] X, Y = np.meshgrid(r, r) ax.scatter3D(Z[:, 0], Z[:, 1], Z[:, 2]) verts = [[Z[0],Z[1],Z[2],Z[3]], [Z[4],Z[5],Z[6],Z[7]], [Z[0],Z[1],Z[5],Z[4]], [Z[2],Z[3],Z[7],Z[6]], [Z[1],Z[2],Z[6],Z[5]], [Z[4],Z[7],Z[3],Z[0]]] ax.add_collection3d(Poly3DCollection(verts, facecolors='cyan', linewidths=1, edgecolors='r', alpha=.20)) ax.set_xlabel('South', fontweight ='bold') ax.set_ylabel('East') # ax.set_zlabel('Z') # 新增:视图更新事件回调函数 def on_view_change(event): # 限制仰角不低于0度 if ax.elev < 0: ax.elev = 0 # 动态修改轴标签 if 85 <= ax.azim % 360 <= 95: ax.set_xlabel('West', fontweight='bold') else: ax.set_xlabel('South', fontweight='bold') # 刷新画布 fig.canvas.draw_idle() # 绑定视图变化事件 fig.canvas.mpl_connect('motion_notify_event', on_view_change) plt.show()
内容的提问来源于stack exchange,提问作者Henfry De Los Santos
相关产品推荐
相关产品推荐

