Matplotlib瀑布图带有表面显示在情节边界上的黑色伪像 我有一个脚本在任意闭合形状中编写热图(或轮廓图)。创建了一个边界网格点的框,并使用掩码来夹紧形状之外的任何点。但是,我...

问题描述 投票:0回答:1

结果是:

这是堆叠的情节脚本:

import numpy as np import matplotlib.pyplot as plt from matplotlib.path import Path from scipy.interpolate import griddata arbitrary_shape_points = [ (0.0, 0.5), (0.1, 0.6), (0.3, 0.55), (0.5, 0.4), (0.6, 0.2), # Top curve (0.65, 0.0), (0.6, -0.2), (0.5, -0.4), (0.3, -0.55), (0.1, -0.6), # Bottom curve right (0.0, -0.5), (-0.1, -0.6), (-0.3, -0.55), (-0.5, -0.4), (-0.6, -0.2), # Bottom curve left (-0.65, 0.0), (-0.6, 0.2), (-0.5, 0.4), (-0.3, 0.55), (-0.1, 0.6), # Top curve left (0.0, 0.5) # Closing point ] shape_path = Path(arbitrary_shape_points) np.random.seed(42) num_points = 100 x_data = np.random.uniform(-0.7, 0.7, num_points) y_data = np.random.uniform(-0.7, 0.7, num_points) fourth_dimension_values = np.linspace(0, 1, 5) shape_xmin = min([p[0] for p in arbitrary_shape_points[:-1]]) shape_ymin = min([p[1] for p in arbitrary_shape_points[:-1]]) shape_xmax = max([p[0] for p in arbitrary_shape_points[:-1]]) shape_ymax = max([p[1] for p in arbitrary_shape_points[:-1]]) grid_resolution = 100 x_grid = np.linspace(shape_xmin, shape_xmax, grid_resolution) y_grid = np.linspace(shape_ymin, shape_ymax, grid_resolution) xx, yy = np.meshgrid(x_grid, y_grid) grid_points = np.column_stack((xx.flatten(), yy.flatten())) mask = shape_path.contains_points(grid_points).reshape(xx.shape) interpolation_method = 'cubic' fig = plt.figure(figsize=(10, 8), facecolor=(0, 0, 0, 0)) ax = fig.add_subplot(111, projection='3d', facecolor=(0, 0, 0, 0)) z_offset = 0 z_step = 1 for i, fd_value in enumerate(fourth_dimension_values): z_data = np.pi*np.sin(np.pi * x_data) + np.exp(-np.pi*y_data) + fd_value zz_interpolated = griddata((x_data, y_data), z_data, (xx, yy), method=interpolation_method) # Mask the grid outside the shape zz_masked = np.where(mask, zz_interpolated, np.nan) # Prepare Z values for 3D plot - constant Z for each slice, offset along z-axis z_surface = np.full_like(xx, z_offset + i * z_step) z_min_slice = np.nanmin(zz_masked) z_max_slice = np.nanmax(zz_masked) if z_max_slice > z_min_slice: zz_normalized = (zz_masked - z_min_slice) / (z_max_slice - z_min_slice) else: zz_normalized = np.zeros_like(zz_masked) # Create facecolors from normalized data facecolors_heatmap = plt.cm.viridis(zz_normalized) # Make masked areas fully transparent facecolors_heatmap[np.isnan(zz_masked)] = [0, 0, 0, 0] # Plot each heatmap slice as a surface surf = ax.plot_surface(xx, yy, z_surface, facecolors=facecolors_heatmap, linewidth=0, antialiased=False, shade=False, alpha=0.8, rstride=1, cstride=1) ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Fourth Dimension Index') ax.set_title('Stacked Heatmaps along Fourth Dimension') ax.view_init(elev=30, azim=-45) ax.set_box_aspect([np.diff(ax.get_xlim())[0], np.diff(ax.get_ylim())[0], np.diff(ax.get_zlim())[0]*0.5]) plt.show() enter image description here 结果是:

我不确定如何阻止黑色边界出现。我试图将这些要点设置为透明,但无能为力。
    

enter image description here旅行渠道:

# Plot each heatmap slice as a surface surf = ax.plot_surface(xx, yy, z_surface, facecolors=facecolors_heatmap, linewidth=0, antialiased=False, shade=False, alpha=0.8, rstride=1, cstride=1)

python numpy matplotlib
1个回答
0
投票
TO:

# Plot each heatmap slice as a surface surf = ax.plot_surface(np.where(mask, xx, np.nan), yy, z_surface, facecolors=facecolors_heatmap, linewidth=0, antialiased=False, shade=False, alpha=0.8, rstride=1, cstride=1)


i我从使用matplotlib的thine matplotlib编译器获取此图片:3.8.4:

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.