今天测试一个画图时,有一个很神奇的发现
import matplotlib.pyplot as plt import numpy as np import pandas as pd x = [10,20,30,40,50,60,70,80] y=[-23.99534833975495, -23.9999998600783, -24.000000070633167, -24.000000068469788, -24.00000006672905, -24.000000065189436, -24.00000006373562, -24.00000006232896] plt.plot(x,y,color="r") plt.show()
结果如下
看左上角那个标识,-2.399e1到底是什么鬼,我的y值波动很小的呀,为什么还是会出现这种突然下降的情况,这个就是matplotlib默认的问题了,我查了之后大致是可以认为的,-2.399e1就是顶部的标度(即x=10)时,往下的-0.006代表的真实值就是-2.399e1-0.006,依次类推,因为最大值和最小值刚好差0.005左右
正确的画图思路是下面的,假如y轴的显示范围
import matplotlib.pyplot as plt import numpy as np import pandas as pd x = [10,20,30,40,50,60,70,80] y=[-23.99534833975495, -23.9999998600783, -24.000000070633167, -24.000000068469788, -24.00000006672905, -24.000000065189436, -24.00000006373562, -24.00000006232896] plt.ylim([-25,-23]) plt.plot(x,y,color="r") plt.show()
结果如下
这个结果才是我的预期
注意这种标识不是所有情况都会出现
import matplotlib.pyplot as plt import numpy as np import pandas as pd x = [5,10,20,30,40,50,60,70,80] y=[-3.99534833975495, -3.99534833975495, -3.9999998600783, -4.000000070633167, -4.000000068469788, -4.00000006672905, -4.000000065189436, -4.00000006373562, -4.00000006232896] #plt.ylim([-25,-23]) plt.plot(x,y,color="r") plt.show()
结果如下
import matplotlib.pyplot as plt import numpy as np import pandas as pd x = [5,10,20,30,40,50,60,70,80] y=[-13.99534833975495, -13.99534833975495, -13.9999998600783, -14.000000070633167, -14.000000068469788, -14.00000006672905, -14.000000065189436, -14.00000006373562, -14.00000006232896] #plt.ylim([-25,-23]) plt.plot(x,y,color="r") plt.show()
结果如下
散点图也会出现这种情况
import matplotlib.pyplot as plt import numpy as np import pandas as pd x = [10,20,30,40,50,60,70,80] y=[-3.99534833975495, -3.9999998600783, -24.000000070633167, -24.000000068469788, -24.00000006672905, -24.000000065189436, -24.00000006373562, -24.00000006232896] plt.scatter(x,y,color="r") plt.show()
norm_vector=[2.663689859547342e-08, 7.999154916053879e-09, 7.99915525716199e-09, 7.999155333718048e-09] plt.figure(figsize=(10,12)) plt.grid() plt.yscale('log') plt.scatter([1,2,3,4],norm_vector)