我正在尝试使用matplotlib绘制一个三维笛卡尔坐标系,将原点居中,用箭头表示3个方向,类似于下面的图形。
我已经用这段代码绘制了一个2D版本,基于这个帖子。
def build_cartesian_plane(max_quadrant_range):
""" The quadrant range controls the range of the quadrants"""
l = []
zeros = []
f, ax = plt.subplots(figsize=(5,5))
plt.grid(True, color='grey', zorder=0,alpha=.5)
head_width = float(0.05) * max_quadrant_range
head_length = float(0.1) * max_quadrant_range
ax.arrow(0, 0, max_quadrant_range, 0, head_width=head_width, head_length=head_length, fc='k', ec='k',zorder=100)
ax.arrow(0, 0, 0, max_quadrant_range, head_width=head_width, head_length=head_length, fc='k', ec='k', zorder=100)
counter_dash_width = max_quadrant_range * 0.02
dividers = [0,.1,.2,.3,.4, .5, .6, .7, .8, .9, 1]
ax.spines['top'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['left'].set_position('zero')
ax.spines['right'].set_color('none')
for i in dividers:
ax.plot([-counter_dash_width, counter_dash_width], [i*max_quadrant_range, i*max_quadrant_range], color='k')
ax.plot([i * max_quadrant_range, i*max_quadrant_range], [-counter_dash_width, counter_dash_width], color='k')
ax.plot([-counter_dash_width, counter_dash_width], [-i * max_quadrant_range, -i * max_quadrant_range], color='k')
ax.plot([-i * max_quadrant_range, -i * max_quadrant_range], [-counter_dash_width, counter_dash_width], color='k')
l.append(i * max_quadrant_range)
l.append(-i * max_quadrant_range)
zeros.append(0)
zeros.append(0)
build_cartesian_plane(10)
plt.show()
看起来ax.arrow
不支持3D,因此我必须使用quiver绘制简单的3D版本。
ax.quiver(0, 0, 0, 0, 3, 0,
arrow_length_ratio=0.1)
ax.quiver(0, 0, 0, 3, 0, 0,
arrow_length_ratio=0.1)
ax.quiver(0, 0, 0, 0, 0, 3,
arrow_length_ratio=0.1)
limt = 2
ax.set_xlim([-limt, limt])
ax.set_ylim([-limt, limt])
ax.set_zlim([-limt, limt])
然后得到了这个
我不熟悉quiver,所以我不确定是否可以使用matplotlib绘制3D笛卡尔坐标系。
任何提示将不胜感激。