尝试将 3d 子图添加到 matplotlib 图形中

发布于 2024-12-21 06:27:43 字数 707 浏览 5 评论 0原文

因此,我尝试创建一个图形,该图形呈现数据点的 3D 图,以及其他 3 个子图中的 3 个投影。我可以毫无问题地添加投影的子图,但是当我尝试将 3 维图放入图形中时,事情会适得其反。

这是我的代码:

def plotAll(data):
    fig = plt.figure()
    plot_3d = fig.add_subplot(221)
    ax = Axes3D(plot_3d)  
    for i,traj in enumerate(data.values()):
        ax.plot3D([traj[0][-1]],[traj[1][-1]],[traj[2][-1]],".",color=[0.91,0.39,0.046])    
    #plot_12v13 = fig.add_subplot(222)
    #plot_projections(data,0,1)
    #plot_13v14 = fig.add_subplot(223)
    #plot_projections(data,1,2)
    #plot_12v14 = fig.add_subplot(224)
    #plot_projections(data,0,2)
    #plt.plot()

它返回: 'AxesSubplot' 对象没有属性 'transFigure'

我正在使用 matplotlib 0.99.3,任何帮助将不胜感激,谢谢!

So I'm trying to create a figure that presents a 3d plot from data points, along with the plots 3 projections in 3 other subplots. I can add the subplots for the projections with no problems, but when I try to place the 3 dimensional plot into the figure things backfire.

here's my code:

def plotAll(data):
    fig = plt.figure()
    plot_3d = fig.add_subplot(221)
    ax = Axes3D(plot_3d)  
    for i,traj in enumerate(data.values()):
        ax.plot3D([traj[0][-1]],[traj[1][-1]],[traj[2][-1]],".",color=[0.91,0.39,0.046])    
    #plot_12v13 = fig.add_subplot(222)
    #plot_projections(data,0,1)
    #plot_13v14 = fig.add_subplot(223)
    #plot_projections(data,1,2)
    #plot_12v14 = fig.add_subplot(224)
    #plot_projections(data,0,2)
    #plt.plot()

which throws back:
'AxesSubplot' object has no attribute 'transFigure'

I'm using matplotlib 0.99.3, any help would be greatly appreciated, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

给妤﹃绝世温柔 2024-12-28 06:27:43

如果您想使用 plt.subplots 而不是 plt.subplot (请参阅差异 这里),那么你可以做这样的事情:

import matplotlib.pyplot as plt
from matplotlib import cm # for a scatter plot
from mpl_toolkits.mplot3d import Axes3D

fig, ax = plt.subplots(1,2,figsize=(10,10),subplot_kw=dict(projection='3d'))

sc1 = ax[0].scatter(x,y,z, c = true, cmap=cm.jet)
ax[0].set_title('True solution')

sc2 = ax[1].scatter(x,y,z c = y_pred, cmap=cm.jet)
ax[1].set_title('Predicted Solution')

嗯,我不知道如何使用plt.subplots将各个轴设置为3D。如果有人可以发表评论,那将会很有帮助。

If you would like to use plt.subplots instead of plt.subplot (see the difference here), then you can do something like this one:

import matplotlib.pyplot as plt
from matplotlib import cm # for a scatter plot
from mpl_toolkits.mplot3d import Axes3D

fig, ax = plt.subplots(1,2,figsize=(10,10),subplot_kw=dict(projection='3d'))

sc1 = ax[0].scatter(x,y,z, c = true, cmap=cm.jet)
ax[0].set_title('True solution')

sc2 = ax[1].scatter(x,y,z c = y_pred, cmap=cm.jet)
ax[1].set_title('Predicted Solution')

Well, I don't know how to set individual axes as 3D using plt.subplots. It would be helpful if someone could comment down.

香草可樂 2024-12-28 06:27:43

我正在寻找一种使用漂亮的 fig,axes = plt.subplots(...) 快捷方式创建 3D 绘图的方法,但由于我刚刚浏览了 Matplotlib 的 mplot3d 教程,我想分享本网站顶部的一句话。

版本 1.0.0 中的新增功能:此方法是创建 3D 轴的首选方法。

导入 matplotlib.pyplot 作为 plt
从 mpl_toolkits.mplot3d 导入 Axes3D
图 = plt.figure()
ax = Fig.add_subplot(111, 投影='3d')

注意

在1.0.0版本之前,创建3D轴的方法是不同的。对于使用旧版本 matplotlib 的用户,请将 ax = Fig.add_subplot(111,projection='3d') 更改为 ax = Axes3D(fig)。

因此,如果您必须使用 <1.0.0 版本的 Matplotlib,则应考虑到这一点。

I was searching for a way to create my 3D-plots with the nice fig, axes = plt.subplots(...) shortcut, but since I just browsed Matplotlib's mplot3d tutorial, I want to share a quote from the top of this site.

New in version 1.0.0: This approach is the preferred method of creating a 3D axes.

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

Note

Prior to version 1.0.0, the method of creating a 3D axes was different. For those using older versions of matplotlib, change ax = fig.add_subplot(111, projection='3d') to ax = Axes3D(fig).

So if you have to use the <1.0.0 version of Matplotlib, this should be taken into account.

喜爱皱眉﹌ 2024-12-28 06:27:43

创建 3D 轴的首选方法是提供 projection 关键字:

def plotAll(data):
    fig = plt.figure()
    ax = fig.add_subplot(221, projection='3d')
    for i,traj in enumerate(data.values()):
        ax.plot3D([traj[0][-1]],[traj[1][-1]],[traj[2][-1]],".",color=[0.91,0.39,0.046])    
    plot_12v13 = fig.add_subplot(222)
    plot_projections(data,0,1)
    plot_13v14 = fig.add_subplot(223)
    plot_projections(data,1,2)
    plot_12v14 = fig.add_subplot(224)
    plot_projections(data,0,2)
    plt.plot()

不幸的是,您没有提供包含合适数据的工作示例,因此我无法测试代码。另外,我建议更新到较新版本的 matplotlib。

The preferred way of creating an 3D axis is giving the projection keyword:

def plotAll(data):
    fig = plt.figure()
    ax = fig.add_subplot(221, projection='3d')
    for i,traj in enumerate(data.values()):
        ax.plot3D([traj[0][-1]],[traj[1][-1]],[traj[2][-1]],".",color=[0.91,0.39,0.046])    
    plot_12v13 = fig.add_subplot(222)
    plot_projections(data,0,1)
    plot_13v14 = fig.add_subplot(223)
    plot_projections(data,1,2)
    plot_12v14 = fig.add_subplot(224)
    plot_projections(data,0,2)
    plt.plot()

Unfortunately, you didn't supply a working example with suitable data, so I couldn't test the code. Also, I would recommend updating to a newer version of matplotlib.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文