Matplotlib堆叠曲面瀑布图边缘出现黑色伪影问题求助
Matplotlib堆叠曲面瀑布图边缘出现黑色伪影问题求助
我最近在做一个可视化需求:在自定义的闭合形状内绘制热力图,并且把多个这样的热力图堆叠起来,用来展示数据随第四维度的变化。单图的实现很顺利,通过网格掩码过滤掉形状外的点,画出的热力图边缘干净,完全符合预期。但改成3D堆叠曲面图后,每个切片的边缘都出现了讨厌的黑色伪影——我明明已经把掩码区域的颜色设为全透明了,却一点效果都没有,实在不知道该怎么解决这个问题。
单图实现代码
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) z_data = np.pi*np.sin(np.pi * x_data) + np.exp(-np.pi*y_data) # Bounding box 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 grid_resolution = 500 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) # Interpolate data interpolation_method = 'cubic' zz_interpolated = griddata((x_data, y_data), z_data, (xx, yy), method=interpolation_method) # Mask the grid outside the shape grid_points = np.column_stack((xx.flatten(), yy.flatten())) mask = shape_path.contains_points(grid_points).reshape(xx.shape) zz_masked = np.where(mask, zz_interpolated, np.nan) # Plot Heatmap using pcolormesh plt.figure(figsize=(8, 6)) heatmap = plt.pcolormesh(xx, yy, zz_masked, cmap='viridis', shading='auto') plt.colorbar(heatmap, label='Z Value') plt.legend() plt.title('Heatmap within Arbitrary Shape') plt.xlabel('X') plt.ylabel('Y') x_shape_original, y_shape_original = zip(*arbitrary_shape_points) plt.plot(x_shape_original, y_shape_original, 'k-', linewidth=1) # create whitespace around the bounding box whitespace_factor = 1.2 plt.xlim(shape_xmin * whitespace_factor, shape_xmax * whitespace_factor) plt.ylim(shape_ymin * whitespace_factor, shape_ymax * whitespace_factor) plt.gca().set_aspect('equal', adjustable='box') plt.show()
单图运行效果
运行上述代码后,得到的热力图完全符合预期:自定义闭合形状内的热力数据展示清晰,形状边缘线条干净,没有多余的黑色区域。
堆叠图实现代码
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()
堆叠图出现的问题
运行堆叠图代码后,每个切片的边缘都出现了明显的黑色伪影,看起来像是掩码区域没有被完全透明化,或者曲面绘制时产生了边缘渲染问题。我已经尝试把掩码对应的facecolors_heatmap设置为[0,0,0,0](全透明),但这个黑色边缘依然存在,实在找不到解决办法。
备注:内容来源于stack exchange,提问作者noobquestionsonly
相关产品推荐
相关产品推荐

