matplotlib 绘制几乎恒定的列表时出现奇怪的绘图

ha5z0ras  于 2023-03-19  发布在  其他
关注(0)|答案(1)|浏览(122)

我试图绘制这个简单的列表energy_list,如下所示

energy_list = [-31.996423234368955, -32.00000000000772, -32.000000000000014, -31.99500000000001,
               -31.999999999999982, -32.0, -32.000000000000014, -32.00000000000001,
               -32.00000000000002, -32.00000000000001, -32.0, -31.99500000000002,
               -32.000000000000014, -32.0, -32.000000000000036, -32.00000000000001, -32.0,
               -32.00000000000001, -32.000000000023576, -32.000000000000014, -32.00000000000007,
               -31.999999999999996, -32.0, -32.00000000000002, -32.0, -31.999999999999993,
               -32.00000000000001, -32.000000000000014, -31.995000000000005, -32.000000000429914,
               -32.0, -32.0, -31.99999999999999, -31.999999999999996, -31.99999999999999, -32.0,
               -31.999999999999996, -32.0, -32.00000000000001, -32.00000000000007,
               -32.00000000000001, -31.999999999999996, -32.000000000000014, -31.995000000000005,
               -32.00000000000001, -32.000000000000014, -32.00000000000001, -32.00000000000001,
               -32.000000000000014, -32.00000000000565]

这个图应该是一个常数函数,但是当我用plt.plot(energy_list)绘制它时,我得到了这个烦人的图:

到底发生了什么?

7vux5j2d

7vux5j2d1#

注意到坐标轴左上角的小数字-3.199e1了吗?这告诉你线的y值是由视y坐标指定的值加上-31.99。因为你没有指定y轴的界限,matplotlib会自动从数据中计算出来,并放大到这个水平,所以你的数据 * 不是 * 常数。
指定y限制后,数据看起来像一条常量线:

plt.plot(energy_list)
plt.ylim(-34, -30)

如果您不想硬编码这些限制,您可以只对数据的最大值/最小值加上/减去一个设定值:

upper_lim = round(max(energy_list) + 2)
lower_lim = round(min(energy_list) - 2)
plt.ylim(lower_lim, upper_lim)

相关问题