Matplotlib坐标轴缩放:如何放大磁场分量中心区域
放大Matplotlib磁场切面图中心区域的解决方法
你修改figsize没用是因为这个参数只调整画布整体尺寸,不会改变显示的数据范围。要放大中心区域,有两种实用方法:
方法一:直接限制坐标轴范围
在每个子图绘制完成后,通过plt.xlim()和plt.ylim()手动设置要显示的中心区域范围。先计算数据的中心坐标,再根据需求截取一定比例的范围:
import numpy as np import matplotlib.pyplot as plt # 原有数据生成代码 x, y, z = np.mgrid[xx[0]:xx[-1]:complex(0, nx), yy[0]:yy[-1]:complex(0, ny), zz[0]:zz[-1]:complex(0, nz)] extent1 = [x.min(), x.max(), z.min(), z.max()] extent2 = [y.min(), y.max(), z.min(), z.max()] extent3 = [x.min(), x.max(), y.min(), y.max()] time_index = 15 plt.figure(figsize=(16, 4)) # x-z平面:添加坐标轴范围限制 plt.subplot(131) plt.imshow(By_side[:, :, time_index], cmap='plasma', origin='lower', extent=extent1, interpolation='nearest') plt.colorbar(label='Magnetic field strength') plt.title('By from the side at t=15 sec.') plt.xlabel('X') plt.ylabel('Z') # 计算中心区域(示例取总范围的20%,可自行调整比例) x_mid = (x.min() + x.max()) / 2 z_mid = (z.min() + z.max()) / 2 x_display_range = (x.max() - x.min()) * 0.1 z_display_range = (z.max() - z.min()) * 0.1 plt.xlim(x_mid - x_display_range, x_mid + x_display_range) plt.ylim(z_mid - z_display_range, z_mid + z_display_range) # y-z平面:同理添加范围限制 plt.subplot(132) plt.imshow(By_front[:, :, time_index].T, cmap='plasma', origin='lower', extent=extent2) plt.colorbar(label='Data') plt.title('By from the front at t=15 sec') plt.xlabel('Y') plt.ylabel('Z') y_mid = (y.min() + y.max()) / 2 z_mid = (z.min() + z.max()) / 2 y_display_range = (y.max() - y.min()) * 0.1 z_display_range = (z.max() - z.min()) * 0.1 plt.xlim(y_mid - y_display_range, y_mid + y_display_range) plt.ylim(z_mid - z_display_range, z_mid + z_display_range) # x-y平面:添加范围限制 plt.subplot(133) plt.imshow(Bz_top[:, :, time_index], cmap='viridis', origin='lower', extent=extent3) plt.colorbar(label='Data') plt.title('Bz component from the top at t=15') plt.xlabel('X') plt.ylabel('Y') x_mid = (x.min() + x.max()) / 2 y_mid = (y.min() + y.max()) / 2 x_display_range = (x.max() - x.min()) * 0.1 y_display_range = (y.max() - y.min()) * 0.1 plt.xlim(x_mid - x_display_range, x_mid + x_display_range) plt.ylim(y_mid - y_display_range, y_mid + y_display_range) plt.tight_layout() plt.show()
方法二:截取中心区域的数组再绘图
如果数据量较大,先截取数组的中心部分再绘制,既能放大细节,又能减少绘图负载:
import numpy as np import matplotlib.pyplot as plt # 原有数据生成代码 x, y, z = np.mgrid[xx[0]:xx[-1]:complex(0, nx), yy[0]:yy[-1]:complex(0, ny), zz[0]:zz[-1]:complex(0, nz)] time_index = 15 plt.figure(figsize=(16, 4)) # x-z平面:截取中心20%的数组(可调整//10的数值改变比例) nx, nz, _ = By_side.shape start_x = nx // 2 - nx // 10 end_x = nx // 2 + nx // 10 start_z = nz // 2 - nz // 10 end_z = nz // 2 + nz // 10 By_side_center = By_side[start_x:end_x, start_z:end_z, time_index] # 更新对应的extent extent1_center = [x[start_x, 0, 0], x[end_x-1, 0, 0], z[0, 0, start_z], z[0, 0, end_z]] plt.subplot(131) plt.imshow(By_side_center, cmap='plasma', origin='lower', extent=extent1_center, interpolation='nearest') plt.colorbar(label='Magnetic field strength') plt.title('By (center) from the side at t=15 sec.') plt.xlabel('X') plt.ylabel('Z') # y-z平面:同理截取中心数组 ny, nz, _ = By_front.shape start_y = ny // 2 - ny // 10 end_y = ny // 2 + ny // 10 start_z = nz // 2 - nz // 10 end_z = nz // 2 + nz // 10 By_front_center = By_front[start_y:end_y, start_z:end_z, time_index].T extent2_center = [y[0, start_y, 0], y[0, end_y-1, 0], z[0, 0, start_z], z[0, 0, end_z]] plt.subplot(132) plt.imshow(By_front_center, cmap='plasma', origin='lower', extent=extent2_center) plt.colorbar(label='Data') plt.title('By (center) from the front at t=15 sec') plt.xlabel('Y') plt.ylabel('Z') # x-y平面:截取中心数组 nx, ny, _ = Bz_top.shape start_x = nx // 2 - nx // 10 end_x = nx // 2 + nx // 10 start_y = ny // 2 - ny // 10 end_y = ny // 2 + ny // 10 Bz_top_center = Bz_top[start_x:end_x, start_y:end_y, time_index] extent3_center = [x[start_x, 0, 0], x[end_x-1, 0, 0], y[0, start_y, 0], y[0, end_y-1, 0]] plt.subplot(133) plt.imshow(Bz_top_center, cmap='viridis', origin='lower', extent=extent3_center) plt.colorbar(label='Data') plt.title('Bz (center) component from the top at t=15') plt.xlabel('X') plt.ylabel('Y') plt.tight_layout() plt.show()
两种方法对比
- 方法一操作简单,无需修改原始数组,适合快速调整显示范围;
- 方法二只绘制中心区域数据,细节显示更清晰,同时降低内存占用,适合大数据量场景。
内容的提问来源于stack exchange,提问作者Stef Athan
相关产品推荐
相关产品推荐

