首页 > 解决方案 > Python 3D sublplots 具有多个 İimages 和一个颜色条

问题描述

我有一些数据,当我尝试使用 subplot 将它们并排绘制时,我遇到了麻烦。这是我的代码

fig = plt.figure()
ax = fig.add_subplot(212, projection='3d')
surf = ax.plot_trisurf(X, Y, Z, cmap=cm.inferno,
                    linewidth=0, shade=True, antialiased=True)

ax.set_zlim3d(None,76)
ax.set_xlabel("x")  
ax.set_ylabel("y")
ax.set_zlabel("z")

ax = fig.add_subplot(221, projection='3d')
surf = ax.plot_trisurf(X, Y, Z, cmap=cm.inferno,
                    linewidth=0, shade=True, antialiased=True)
ax.set_xlabel("x")  
ax.set_ylabel("y")
ax.set_zlabel("z")


ax = fig.add_subplot(222, projection='3d')
surf = ax.plot_trisurf(X, Y, Z, cmap=cm.inferno,
                    linewidth=0, shade=True, antialiased=True)

ax.set_xlabel("x")  
ax.set_ylabel("y")
ax.set_zlabel("z")

plt.colorbar(surf)
plt.show()

这就是我在 此处输入图像描述所看到的, 但我想要 在此处输入图像描述之类的内容 我怎样才能使颜色条和图表更大

标签: pythonmatplotlib

解决方案


import matplotlib
import matplotlib.pyplot as plt

# create mockup data
X, Y = [], []
for x in range(10):
    for y in range(10):
        X.append(x)
        Y.append(y)
Z = [1] * len(X)

# color settings
cmap = matplotlib.cm.inferno
norm = matplotlib.colors.Normalize(vmin=5, vmax=10)
colorbar = matplotlib.cm.ScalarMappable(norm=norm, cmap=cmap)

# allocate subplots
fig = plt.figure()
for cell in [231, 233, 235]:
    ax = fig.add_subplot(cell, projection='3d')
    surf = ax.plot_trisurf(X, Y, Z, cmap=cmap)

# create colorbar
fig.subplots_adjust(right=0.7)
cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
fig.colorbar(colorbar, cax=cbar_ax)

plt.show()

输出: 输出


推荐阅读