import numpy as np import matplotlib import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D # 使用Agg后端(无GUI) matplotlib.use("Agg") # 定义x轴和y轴的数据 x_values = np.array([1, 3, 5, 7, 9]) # init value y_values = np.array([0.1, 0.3, 0.5, 0.7, 0.9, 1.1, 1.3, 1.5, 1.7, 1.9, 2.1, 2.3, 2.5, 2.7, 2.9]) # temperature # 创建网格 X, Y = np.meshgrid(x_values, y_values) # Z轴数据(performance)- 这里使用示例数据 # 请将这里替换为你的实际数据,应该是一个15x5的数组 # 示例:使用一个简单的函数生成数据 # Z = np.sin(np.sqrt(X**2 + Y**2)) * 100 + np.random.randn(15, 5) * 5 # 如果你有实际的performance数据,请按照以下格式替换: Z = np.array([ [11.9, 14.8, 13.7, 13.0, 10.8], # temperature=0.1时,对应5个init value的performance [17.5, 10.8, 10.3, 14.3, 13.0], # temperature=0.3时,对应5个init value的performance [29.2, 31.7, 13.5, 10.1, 14.8], # temperature=0.5时,对应5个init value的performance [20.0, 30.8, 23.3, 14.2, 11.1], # temperature=0.7时,对应5个init value的performance [10.9, 23.3, 25.0, 24.2, 10.9], # temperature=0.9时,对应5个init value的performance [23.3, 24.2, 23.3, 29.2, 21.7], # temperature=1.1时,对应5个init value的performance [21.7, 27.5, 22.5, 25.8, 25.0], # temperature=1.3时,对应5个init value的performance [11.5, 23.3, 17.5, 26.7, 27.5], # temperature=1.5时,对应5个init value的performance [24.2, 24.2, 16.7, 23.3, 29.2], # temperature=1.7时,对应5个init value的performance [18.3, 24.2, 27.5, 20.0, 25.8], # temperature=1.9时,对应5个init value的performance [25.8, 25.8, 26.7, 21.7, 28.3], # temperature=2.1时,对应5个init value的performance [28.3, 26.2, 23.3, 20.8, 26.7], # temperature=2.3时,对应5个init value的performance [20.0, 24.2, 20.0, 20.0, 28.3], # temperature=2.5时,对应5个init value的performance [23.3, 26.2, 21.7, 24.2, 24.2], # temperature=2.7时,对应5个init value的performance [15.8, 22.5, 20.8, 25.8, 23.3], # temperature=2.9时,对应5个init value的performance ]) # 创建3D图形 fig = plt.figure(figsize=(12, 8)) ax = fig.add_subplot(111, projection='3d') # 绘制3D表面图(不显示散点) surf = ax.plot_surface(X, Y, Z, cmap='RdYlBu_r', alpha=0.8, edgecolor='none') # 设置轴标签 ax.set_xlabel('Init Value', fontsize=12, labelpad=10) ax.set_ylabel('Temperature', fontsize=12, labelpad=10) ax.set_zlabel('Performance', fontsize=12, labelpad=10) # 设置标题(字体更大,间隙更小) ax.set_title('Performance vs Init Value and Temperature', fontsize=18, pad=10) # 添加颜色条(更窄,离图更近) colorbar = fig.colorbar(surf, ax=ax, shrink=0.5, aspect=10, pad=0.05) colorbar.set_label('Performance', rotation=270, labelpad=15) # 设置视角(可以调整以获得最佳视图) ax.view_init(elev=30, azim=45) # 明确设置x轴和y轴的刻度 ax.set_xticks(x_values) ax.set_yticks(y_values) # 如果需要,也可以设置z轴的刻度 # ax.set_zticks(np.linspace(Z.min(), Z.max(), 5)) # 移除网格线 ax.grid(False) # 设置透明背景 ax.xaxis.pane.fill = False ax.yaxis.pane.fill = False ax.zaxis.pane.fill = False ax.xaxis.pane.set_edgecolor('none') ax.yaxis.pane.set_edgecolor('none') ax.zaxis.pane.set_edgecolor('none') # 保存3D图(透明背景) plt.tight_layout() plt.savefig('3d_surface_plot.png', dpi=300, bbox_inches='tight', transparent=True) plt.close() print("3D表面图已保存为: 3d_surface_plot.png") # 创建等高线图 plt.figure(figsize=(7, 5)) num_levels = 15 # 使用 'RdYlBu_r' 颜色映射 contourf = plt.contourf(X, Y, Z, levels=num_levels, cmap='RdYlBu_r', alpha=0.7) # 加粗的黑色等高线,不贴数值标签 contour_lines = plt.contour(X, Y, Z, levels=num_levels, colors='black', alpha=0.5, linewidths=1.5) # 添加颜色条(更窄,离图更近) cbar = plt.colorbar(contourf, shrink=0.8, aspect=15, pad=0.02) cbar.set_label("Performance", rotation=270, labelpad=15) # 设置轴标签 plt.xlabel('Init Value', fontsize=12) plt.ylabel('Temperature', fontsize=12) # 设置标题(字体更大,间隙更小) plt.title('Performance Contour Plot', fontsize=18, pad=10) # 移除网格 plt.grid(False) # 显示x和y轴的刻度 plt.xticks(x_values) plt.yticks(y_values) # 保存等高线图(透明背景) plt.tight_layout() plt.savefig('contour_plot.png', dpi=300, bbox_inches='tight', transparent=True) plt.close() print("等高线图已保存为: contour_plot.png") print("所有图片已成功保存!")