如何将x轴不同、点数各异的列表数据绘制到同一固定坐标轴?
问题描述
我有一组字典数据如下:
my_dict_1 = {"x=0": 0.33282876064333017, "x=50": 0.3466414380321665, "x=110": 0.3540208136234626, "x=120": 0.350236518448439, "x=130": 0.35042573320719017, "x=140": 0.33415326395458844, "x=150": 0.33245033112582784, "x=450": 0.33345033112582784,} my_dict_2 = {"x=0": 0.24751431153962036, "x=60": 0.2752335040674902, "x=130": 0.2799035854172944, "x=140": 0.28321783669780054, "x=170": 0.31048508586923773, "x=180": 0.3110876770111479, "x=200": 0.2946670683940946, "x=210": 0.30491111780656827, "x=220": 0.29873455860198855, "x=230": 0.299939740885809, "x=240": 0.3005423320277192, "x=260": 0.29873455860198855, "x=270": 0.2933112383247966, "x=280": 0.2963241940343477, "x=290": 0.2927086471828864, "x=300": 0.2927086471828864, "x=310": 0.2937631816812293, "x=320": 0.2960228984633926, "x=330": 0.2889424525459476, "x=340": 0.28984633925881287, "x=350": 0.28969569147333535, "x=360": 0.2770412774932208, "x=370": 0.26303103344380835, "x=380": 0.27628803856583306, "x=390": 0.2146730943055137, "x=400": 0.2124133775233504, "x=410": 0.2124133775233504} my_dict_3 = {"x=0": 0.248, "x=50": 0.26, "x=110": 0.281, "x=120": 0.282, "x=130": 0.281, "x=140": 0.292, "x=150": 0.28, "x=160": 0.268, "x=170": 0.274, "x=180": 0.295, "x=190": 0.307, "x=210": 0.28, "x=230": 0.291, "x=240": 0.299, "x=250": 0.266, "x=260": 0.269, "x=270": 0.288, "x=290": 0.283, "x=300": 0.285, "x=310": 0.28, "x=320": 0.243, "x=330": 0.253, "x=340": 0.256, "x=350": 0.246, "x=360": 0.24, "x=370": 0.23, "x=380": 0.228, "x=390": 0.23, "x=400": 0.221, "x=410": 0.222, "x=420": 0.239, "x=430": 0.24, "x=440": 0.222, "x=450": 0.219, "x=460": 0.219, "x=470": 0.219, "x=480": 0.217, "x=490": 0.203, "x=500": 0.214, "x=510": 0.227, "x=520": 0.227, "x=530": 0.209, "x=540": 0.22, "x=550": 0.246, "x=560": 0.226, "x=570": 0.247, "x=580": 0.241, "x=590": 0.267}
我可以通过my_dict.values()将每个字典转换为列表,并使用plt.plot(my_dict.values())进行绘制。但如何设置一个从0到500、步长为10的固定x轴,将每个列表的值对应到正确的x轴位置,并将所有列表绘制在同一图中?
解决方案
核心思路是解析字典中的x坐标,生成固定步长的x轴序列,再为每个固定x点匹配对应y值(无数据位置填充NaN),最后批量绘图。
完整实现代码
import matplotlib.pyplot as plt import numpy as np # 生成固定步长的x轴序列(0到500,步长10) fixed_x = np.arange(0, 510, 10) # 左闭右开特性,用510才能包含500 def process_dict(input_dict): # 将字典键解析为整数x,转为{x: y}格式 num_dict = {int(k.split('=')[1]): v for k, v in input_dict.items()} # 为每个固定x点匹配y值,无数据则填充NaN(matplotlib会自动跳过这些点) y_values = [num_dict.get(x, np.nan) for x in fixed_x] return y_values # 处理所有字典数据 y1 = process_dict(my_dict_1) y2 = process_dict(my_dict_2) y3 = process_dict(my_dict_3) # 绘制所有曲线 plt.figure(figsize=(10, 6)) plt.plot(fixed_x, y1, label='Dataset 1') plt.plot(fixed_x, y2, label='Dataset 2') plt.plot(fixed_x, y33, label='Dataset 3') # 设置图表属性 plt.xlabel('X Value') plt.ylabel('Y Value') plt.title('Multiple Datasets on Fixed X Axis (0-500, Step 10)') plt.xlim(0, 500) # 强制x轴范围为0到500 plt.legend() plt.grid(True) plt.show()
关键代码说明
fixed_x生成:用np.arange(0,510,10)生成目标x轴序列,确保覆盖0到500的所有步长为10的点。process_dict函数:把字典中x=xxx格式的键解析为整数x,再遍历固定x序列匹配对应y值,缺失数据用np.nan填充,避免绘图时出现错误连接。- 绘图逻辑:直接使用固定x序列和处理后的y值列表绘图,添加图例、坐标轴标签和网格,确保图表清晰可读。
内容的提问来源于stack exchange,提问作者Penguin
相关产品推荐
相关产品推荐

