简体   繁体   English

如何使用 matplotlib 在子图中重新缩放 plot

[英]How to rescale a plot in a subplot with matplotlib

I have 4 subplots with a different 3D plot with a colorbar.我有 4 个子图,带有不同的 3D plot 和颜色条。 I want to plot a XY view of my 3D plot, remove the x,y,z axis and resize my plot to use all the space available in the subplot such that the XY view has the same height as the colorbar.我想要 plot 我的 3D plot 的 XY 视图,删除 x、y、z 轴并调整我的 plot 的大小以使用子图中的所有可用空间,使 XY 视图具有与颜色条相同的高度。 I can remove the axis but I do not know how to resize the image.我可以删除轴,但我不知道如何调整图像大小。 I attached a working code to illustrate this.我附上了一个工作代码来说明这一点。

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import matplotlib.tri as mtri
import matplotlib 
import numpy as np

# Create 3D function
n_radii = 8
n_angles = 36
radii = np.linspace(0.125, 1.0, n_radii)
angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False)[..., np.newaxis]
x = np.append(0, (radii*np.cos(angles)).flatten())
y = np.append(0, (radii*np.sin(angles)).flatten())
z = np.sin(-x*y)

fig = plt.figure()
for ii in range(1, 4):
    #Plot
    # ============================================================================ 
    ax = fig.add_subplot(2,2, ii, projection='3d')
    cs =ax.plot_trisurf(x, y, z, linewidth=0.2, antialiased=True)

    ax.view_init(90, 0)
    plt.title(ii)
    # ax.axis('off')
    plt.grid(b=None)

    # Create color bar
    # ============================================================================ 
    norm = matplotlib.colors.Normalize(vmin = 0, vmax = 1, clip = False)
    m = plt.cm.ScalarMappable(norm=norm)
    m.set_array([])
    plt.colorbar(m)

plt.tight_layout()
plt.show()
#plt.savefig("test.pdf",bbox_inches='tight')

Any idea how can I do this?知道我该怎么做吗?

I have added我已经添加了

plt.gca().set_axis_off()
plt.axis([0.6 * x for x in plt.axis()])

to your code which hides the axes and sets the view to 60% of its previous value.到隐藏轴并将视图设置为其先前值的 60% 的代码。 The result looks like this:结果如下所示:

图1

Full code:完整代码:

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import matplotlib.tri as mtri
import matplotlib 
import numpy as np

# Create 3D function
n_radii = 8
n_angles = 36
radii = np.linspace(0.125, 1.0, n_radii)
angles = np.linspace(0, 2*np.pi, n_angles, endpoint=False)[..., np.newaxis]
x = np.append(0, (radii*np.cos(angles)).flatten())
y = np.append(0, (radii*np.sin(angles)).flatten())
z = np.sin(-x*y)

fig = plt.figure()
for ii in range(1, 4):
    #Plot
    # ============================================================================ 
    ax = fig.add_subplot(2,2, ii, projection='3d')
    cs =ax.plot_trisurf(x, y, z, linewidth=0.2, antialiased=True)

    ax.view_init(90, 0)
    plt.title(ii)
    # ax.axis('off')
    plt.grid(b=None)

    # Create color bar
    # ============================================================================ 
    norm = matplotlib.colors.Normalize(vmin = 0, vmax = 1, clip = False)
    m = plt.cm.ScalarMappable(norm=norm)
    m.set_array([])
    plt.colorbar(m)

    plt.gca().set_axis_off()
    plt.axis([0.6 * x for x in plt.axis()])

plt.tight_layout()
plt.show()
#plt.savefig("test.pdf",bbox_inches='tight')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM