python - 可以将 Matplotlib Axed3D 轴设置为所有相交为零的数学教科书绘制它们的方式吗?
问题描述
通常,在二维 matplotlib 图中,您会得到笛卡尔平面的第一个象限,轴线位于图形的左侧和底部区域。因为我是为了数学笔记的目的而绘制的,所以我想要可以用这个来完成的四象限布局:
x = np.arange(-10, 10, 0.1)
y = np.cos(x)
f = plt.figure()
ax = f.add_subplot(1, 1, 1)
ax.spines['left'].set_position('center')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_position('center')
ax.spines['top'].set_color('none')
ax.plot(x, y, c='black', linewidth=1)
ax.fill_between(x, y, where=(x > -1e-10) & (x < np.pi), color='gray')
ax.set_xbound(-2*np.pi, 2*np.pi)
ax.set_ybound(-3, 3)
f.suptitle("Area under a cosine function", size='x-large', weight='bold')
f.show()
现在我想用 3D 绘图来做,其中 x、y、z 轴线都在原点相交,这样“刺”(我知道 Axes3D 对象实际上没有刺)将空间分成 8 个象限。我怎样才能做到这一点?
编辑:当前 3d 绘图代码:
x = np.arange(-5, 5)
y = np.arange(-5, 5)
z = np.arange(-5, 5)
class Arrow3D(FancyArrowPatch):
def __init__(self, xs, ys, zs, *args, **kwargs):
FancyArrowPatch.__init__(self, (0,0), (0,0), *args, **kwargs)
self._verts3d = xs, ys, zs
def draw(self, renderer):
xs3d, ys3d, zs3d = self._verts3d
xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, renderer.M)
self.set_positions((xs[0],ys[0]),(xs[1],ys[1]))
FancyArrowPatch.draw(self, renderer)
f = plt.figure()
ax = f.add_subplot(1, 1, 1, projection='3d')
ax.set_axis_off()
ax.add_artist(Arrow3D([-5, 5], [0, 0], [0, 0]))
ax.add_artist(Arrow3D([0, 0], [-5, 5], [0, 0]))
ax.add_artist(Arrow3D([0, 0], [0, 0], [-5, 5]))
ax.scatter(x, y, z)
f.show()
现在的问题是数据,数据似乎没有排列;轴在每个方向上设置为(我认为)-5 到 5,但是数据也从 -5 到 5,看起来它超出了轴的“边界”?
解决方案
试试这段代码,把它放在后面ax.scatter()
:
ax.add_artist(Arrow3D([-5, 5], [0, 0], [0, 0], mutation_scale=20, lw=1, arrowstyle="-|>", color="r"))
ax.add_artist(Arrow3D([0, 0], [-5, 5], [0, 0], mutation_scale=20, lw=1, arrowstyle="-|>", color="b"))
ax.add_artist(Arrow3D([0, 0], [0, 0], [-5, 5], mutation_scale=20, lw=1, arrowstyle="-|>", color="g"))
# set viewing angle
ax.azim = 40 # z rotation (default=270)
ax.elev = 25 # x rotation (default=0)
ax.dist = 10 # zoom (define perspective)
这将替换ax.add_artist()
您代码中的所有内容。
推荐阅读
- elasticsearch - 使用时间字段名称中的点在 grafana 中定义新弹性数据源的问题
- wordpress - 会话数据以错误的访问者结尾?这怎么可能?
- r - 主面板中的过滤器水平
- c++ - 尝试运行 C++ 应用程序时权限被拒绝
- javascript - React Native Carousel OnPress
- firebase - Show pins of a subcollection of firebase on map - Flutter
- css - 如何更改 hr 标签的长度?
- r - expss 调用返回空白表
- typescript - 如何将类的函数和变量传递给 setInterval?
- amazon-web-services - 在 AWS 中动态控制退出 IP