Arrange matplotlib trisurf plots in subplots

83 Views Asked by At

I want to properly present results of a 2D-FEA using matplotlib.

Plots, consisting of matplotlib trisurf plots, should be arranged in a gird of subplots and use the same colorbar. In some subplots, it will be necessary to zoom into specific parts of the structure.

The problem is, that my graphs have a lot of white space between the subplots (I already experimented with gridspec's width_ratio etc.). Also does the zoom not exactly show the intended area (x in [0,1]). See this image.

How can I get rid of these issues?

Here my code:

# Imports
import matplotlib.pyplot as plt
import matplotlib.tri as tri

# Figure configuration
fig = plt.figure(figsize=(8,4))
gs = gridspec.GridSpec(3, 2, width_ratios=[1, 0.1])

# Data
x_cor = np.array([0, 1, 2, 3, 0, 1, 2, 3]) # x-coordinate of nodes
y_cor = np.array([0, 0, 0, 0, 1, 1, 1, 1]) # y-coordinate of nodes
z_values = np.array([0, 1, 2, 3, 0, 1, 2, 3]) # value at nodes
triangles = np.array([[0, 1, 4],
                     [1, 2, 5],
                     [2, 3, 6],
                     [4, 1, 5],
                     [5, 2, 6],
                     [6, 3, 7]])

# Generate triangulation object
triang = tri.Triangulation(x_cor, y_cor, triangles=triangles)

# Refine triangulation and interpolate
refinement = tri.UniformTriRefiner(triang)
fine_triang, fine_z_values = refinement.refine_field(z_values)

# Plot
for i in range(3):
    ax = fig.add_subplot(gs[i, 0], projection='3d')

    if i == 0: # normal mesh
        surf_x = ax.plot_trisurf(triang, z_values, vmin=0, vmax=3, cmap='viridis', linewidth=0, antialiased=False)
    elif i == 1: # try to zoom
        surf_x = ax.plot_trisurf(triang, z_values, vmin=0, vmax=3, cmap='viridis', linewidth=0, antialiased=False)
        ax.set_xlim([0, 1])
    elif i == 2: # refined mesh
        surf_x = ax.plot_trisurf(fine_triang, fine_z_values, vmin=0, vmax=3, cmap='viridis', linewidth=0, antialiased=False)
    
    ax.view_init(90, -90, 0)
    ax.set_proj_type('ortho')
    ax.set_axis_off()

# Color bar
cbar_ax = fig.add_subplot(gs[:, 1])
cbar = fig.colorbar(surf_x, cax=cbar_ax)
0

There are 0 best solutions below