实验作图的时候出现了一个问题,由于数据范围过大,导致数据变化不明显,出来的折线图如下:
对应代码:
import matplotlib.pyplot as plt
import numpy as np
a_ = np.array(a)
plt.plot(a_[:, 0],-a_[:, 1])
plt.xlabel('number of objective function evaluations')
plt.ylabel('the optimal solution')
plt.show()
所以想到了利用对数坐标,搜了搜官方文档,将y坐标改成了对数坐标。效果如下:
代码如下:
import matplotlib.pyplot as plt
import numpy as np
a_ = np.array(a)
plt.axes(yscale = "log") # 在plot语句前加上该句话即可
plt.plot(a_[:, 0],-a_[:, 1])
plt.xlabel('number of objective function evaluations')
plt.ylabel('the optimal solution')
plt.show()
如上,在plot前加入语句plt.axes(yscale = "log")即可。若要让x坐标也为对数坐标,yscale改为xscale即可。
注意, 这里用的是matplotlib.
pyplot里面的axes,而不是matplotlib.axes里的。
这里使用log关键字,仅用来绘制正数,若要绘制有正数有负数,有symlog关键字可用。
官方文档:https://matplotlib.org/api/_as_gen/matplotlib.pyplot.axes.html#matplotlib.pyplot.axes
附:
这里绘图的数据如下
a = [[1,-536512],
[13,-9950.1],
[24,-9394.13],
[49,-8829.27],
[97,-3496],
[110,-2403.09],
[165,-169.257],
[195,-124.513],
[230,-6.31601],
[245,-7.92737],
[259,-4.78377],
[896,-10.7847],
[1496,-7.73153],
[1679,-6.59467],
[1690,-7.73153],
[2063,-6.59467],
[2064,-7.73153],
[11556,-8.46228],
[16033,-9.08796],
[18108,-17.3727],
[18816,-8.46228],
[19374,-5.23453],
[21550,-4.53278],
[27783,-4.43594],
[27795,-4.53278],
[29177,-4.43594],
[30382,-4.53278],
[46829,-4.2019],
[92000,-4.68217],
[99905,-4.2019],
[148385,-5.23453],
[148501,-4.68217],
[155295,-5.23453],
[155338,-4.86867],
[162962,-5.23453],
[174377,-4.86867],
[256816,-8.46228],
[256879,-4.86867],
[340068,-8.46228],
[340070,-5.23453],
[596774,-5.61897],
[950661,-4.05199],
[951430,-5.61897],
[987355,-4.05199],
[1003312,-5.61897],
[1019746,-4.47187],
[1043041,-5.61897],
[1078970,-4.05199],
[1090274,-5.34352],
[1372887,-2.83073],
[2049744,-2.77323],
[2050511,-2.83073],
[2152576,-2.77323],
[3534190,-4.21561],
[3536974,-2.77323],
[3569699,-4.21561],
[3703846,-2.16943],
[3778351,-4.21561],
[4304789,-2.16943],
[4360274,-1.52345],
[4382029,-2.16943],
[4518296,-4.21561],
[4638560,-2.16943],
[4717278,-4.21561],
[4717910,-2.16943],
[4812802,-4.21561],
[4852083,-2.13751],
[4999685,-4.21561],
[5001974,-1.91082],
[5045584,-4.21561],
[5174315,-2.13751],
[5472105,-4.21561],
[5591650,-2.29429],
[5604518,-4.21561],
[5894536,-1.23585],
[5941052,-4.21561],
[5944566,-1.23585],
[5991252,-4.21561],
[5994778,-1.23585],
[6191307,-2.13751],
[6306965,-2.77323],
[6420408,-1.23585],
[6465015,-2.77323],
[6526275,-1.23585],
[6569227,-2.77323],
[6576564,-1.23585],
[7440708,-2.16943],
[7683774,-3.30138],
[7862138,-1.74225],
[8258342,-3.30138],
[8382625,-1.74225],
[8525884,-3.30138],
[8831033,-1.74225],
[8895660,-3.30138],
[8896294,-1.74225],
[10020372,-1.23585]]