import numpy as np import matplotlib.pyplot as plt from common.consts import AU from sim_scenes.solar_system.halley_comet_lib import HalleyCometParams from sim_lab.halley_comet_research_calc import calc_simulator, target_function # 支持中文 plt.rcParams['font.sans-serif'] = ['SimHei'] # 用来正常显示中文标签 plt.rcParams['axes.unicode_minus'] = False # 用来正常显示负号 def plt_draw_3d(): # idx = 0 r = 1 xs = [] ys = [] zs = [] a_s = [] b_s = [] c_s = [] for xi in range(-r, r + 1): for yi in range(-r, r + 1): for zi in range(-r, r + 1): x, y, z = -2.47 + (xi / 100), 5.1 + (yi / 100), 8.73 + (zi / 100) xs.append(x) ys.append(y) zs.append(z) a, b, c = target_function(x, y, z) # a,b,c = x,y,z a_s.append(a) b_s.append(b) c_s.append(c) # 把 x y z 和 结果 a b c 进行可视化,完成代码,方便查看 x y z 和 结果 a b c 之间的关系 fig = plt.figure() ax = fig.add_subplot(111, projection='3d') ax.plot(xs, ys, zs, label="IN") ax.plot(a_s, b_s, c_s, label="OUT") # ax.scatter(xs, ys, zs, c='r', marker='o') # 绘制三维散点图,使用红色标记,可以根据需要更改颜色和标记类型 # ax.scatter(a_s, b_s, c_s, c='b', marker='o') # 在同样的图上,使用蓝色标记绘制函数的结果点 ax.legend() plt.show() import matplotlib.pyplot as plt def plot_twin(t, _y1, _y2, _y3, xlabel, _ylabel1, _ylabel2, _ylabel3): fig, ax1 = plt.subplots() color = 'tab:blue' ax1.set_xlabel(xlabel) ax1.set_ylabel(_ylabel1, color=color) ax1.plot(t, _y1, color=color) ax1.tick_params(axis='y', labelcolor=color) ax2 = ax1.twinx() # 创建共用x轴的第二个y轴 color = 'tab:red' ax2.set_ylabel(_ylabel2, color=color) ax2.plot(t, _y2, color=color) ax2.tick_params(axis='y', labelcolor=color) ax3 = ax1.twinx() # 创建共用x轴的第二个y轴 color = 'tab:green' ax3.set_ylabel(_ylabel3, color=color) ax3.plot(t, _y3, color=color) ax3.tick_params(axis='y', labelcolor=color) # fig.tight_layout() plt.show() def plt_draw_2d(): import numpy as np t = np.array([1, 2, 3]) # 创建模拟数据 data1 = np.array([35.032, 35.033, 35.034]) data2 = np.array([0.477, 0.477, 0.477]) data3 = np.array([13615, 13615, 13615]) plot_twin(t, data1, data2, data3, 'exp', 'sine', 'year') # target_function(-2.835, 4.71, 8.86) # 远日点:35.243 AU, 近日点:0.584 AU, 天数: 13809 # target_function(-2.835, 4.73, 8.85) # OK 远日点:35.251 AU, 近日点:0.585 AU, 天数: 13815 R = 3 X, Y, Z = -2.835, 4.73, 8.85 X, Y, Z = -2.835, 4.72, 8.847 # [-2.816, 4.725, 8.84] def plt_draw_x(): x_s = [] a_s = [] b_s = [] c_s = [] for x in range(-R, R + 1): x, y, z = X + (x / 100), Y, Z x_s.append(x) a, b, c = target_function(x, y, z) a_s.append(a) b_s.append(b) c_s.append(c) plot_twin(x_s, a_s, b_s, c_s, 'X', '远日点', '近日点', '天数') def plt_draw_y(): y_s = [] a_s = [] b_s = [] c_s = [] for y in range(-R, R + 1): x, y, z = X, Y + (y / 100), Z y_s.append(y) a, b, c = target_function(x, y, z) a_s.append(a) b_s.append(b) c_s.append(c) plot_twin(y_s, a_s, b_s, c_s, 'Y', '远日点', '近日点', '天数') def plt_draw_z(): z_s = [] a_s = [] b_s = [] c_s = [] for z in range(-R, R + 1): x, y, z = X, Y, Z + (z / 100) z_s.append(z) a, b, c = target_function(x, y, z) a_s.append(a) b_s.append(b) c_s.append(c) plot_twin(z_s, a_s, b_s, c_s, 'Z', '远日点', '近日点', '天数') if __name__ == '__main__': plt_draw_x() plt_draw_y() plt_draw_z()