python - 在 Matplotlib 中,如何将 3D 图作为插图包含在内?

标签 python matplotlib

我目前可以做插图,但只能在 2D 中做。在下面的代码中将 projection='3d' 添加到 fig1.add_axes()inset_axes() 将不起作用。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d
from mpl_toolkits.axes_grid.inset_locator import inset_axes

x = np.linspace(0,2.0*np.pi,51)
y = np.sin(x)

def make_plot(x,y):
    fig = plt.figure(figsize=(10,7))
    rect = [0.18,0.18,0.25,0.25]
    ax = fig.add_subplot(111)
    ax.grid()
    ax.set_xlim(x.min(),x.max())
    ax.plot(x,y,'bo-')

    fig1 = plt.gcf()
    ax_inset1 = fig1.add_axes(rect,anchor='NW',axisbg=None)  # , projection='3d')
    ax_inset2 = inset_axes(ax, width="40%", height=1.8, loc=1)  # , projection='3d')
    ax_inset1.plot(x,y,'r--'); ax_inset1.set_xlim(x.min(),x.max())
    ax_inset2.plot(x,y,'g--'); ax_inset2.set_xlim(x.min(),x.max())

    X, Y, Z = axes3d.get_test_data(0.05)
    #ax_inset1.plot_wireframe(X, Y, Z, rstride=10, cstride=10)

    plt.show()

make_plot(x,y)

上面代码生成的图片如下:

inset ] 1

如何更改上面的代码,使其中一个(或两个)插图具有 3D 图?

最佳答案

晚了几年,但这里是我希望在这里找到的答案。 从 matplotlib 3.1.3 开始,这是可行的。

将下面的代码与 fig 参数一起使用应该也允许在 3D 投影上进行“插入”。

import numpy as np
import matplotlib.pyplot as plt
# required for "3d" projection even though not explicitly used
from mpl_toolkits.mplot3d import axes3d
from matplotlib.transforms import Bbox

def add_inset_axes(rect, units="ax", ax_target=None, fig=None, projection=None, **kw):
    """
    Wrapper around `fig.add_axes` to achieve `ax.inset_axes` functionality
    that works also for insetting 3D plot on 2D ax/figures
    """
    assert ax_target is not None or fig is not None, "`fig` or `ax_target` must be provided!"
    _units = {"ax", "norm2ax", "norm2fig"}
    assert {units} <= _units, "`rect_units` not in {}".format(repr(_units))

    if ax_target is not None:
        # Inspired from:
        # https://stackoverflow.com/questions/14568545/convert-matplotlib-data-units-to-normalized-units
        bb_data = Bbox.from_bounds(*rect)
        trans = ax_target.transData if units == "ax" else ax_target.transAxes
        disp_coords = trans.transform(bb_data)
        fig = ax_target.get_figure()
        fig_coord = fig.transFigure.inverted().transform(disp_coords)
    elif fig is not None:
        if ax_target is not None and units != "norm2fig":
            bb_data = Bbox.from_bounds(*rect)
            trans = ax_target.transData if units == "ax" else ax_target.transAxes
            disp_coords = trans.transform(bb_data)
        else:
            fig_coord = Bbox.from_bounds(*rect)

    axin = fig.add_axes(
        Bbox(fig_coord),
        projection=projection, **kw)

    return axin

x = np.linspace(0,2.0*np.pi,51)
y = np.sin(x)

fig = plt.figure(figsize=(10,7))
rect = [0.1, 0.15, 0.4, 0.4]
rect2 = [3.5, 0, 2.5, 1]

ax = fig.add_subplot(111)
ax.grid()
ax.set_xlim(x.min(),x.max())
ax.plot(x,y,'bo-')

# This works actually
ax_inset1 = fig.add_axes(rect, anchor='NW', projection='3d')
ax_inset1.plot(x,y,'r--')

# In case you wanted to make the background of the 3d plot transparent 
ax_inset1.patch.set_alpha(0.)

ax_inset1.xaxis.set_alpha(0.)
ax_inset1.yaxis.set_alpha(0.)
ax_inset1.zaxis.set_alpha(0.)

# This one does not work because the inset is assumed to be the same type of plot...
# ax_inset2 = ax.inset_axes(rect, projection='3d')

# Here is a convenient helper function to achieve the same functionaly
# plus some extra sugar

# Convenient wrapper around `fig.add_axes`
ax_inset2 = add_inset_axes(rect2, units="ax", ax_target=ax, projection="3d")
ax_inset2.plot(x,y,'g--')

save_fig(fig, "Stackoverflow.png")
plt.show()

结果:

2D plot with 3D insets

关于python - 在 Matplotlib 中,如何将 3D 图作为插图包含在内?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36331478/

相关文章:

Python 将回车解码为换行

python - 使用 matplotlib + errorbar 进行动画

python - 如何在 matplotlib 直方图中选择 bin

python - 如何绘制 3D 曲面?

python - 使用 matplotlib 中的 set_text 将逗号分隔符添加到 seaborn 热图中,并保留美元符号

python - python中的对数y轴箱

python - 虚拟助理的 POS 标记器

用于抓取 html 输出的 Python 脚本有时有效,有时无效

python - 如何在 matplotlib 配置文件中定义次要刻度数

python - 读取 Flask Web 服务的参数