(完结部分 对数和非线性坐标系)
matplotlib.pyplot不仅仅支持线型坐标轴(axis)刻度(scale)。也支持对数和逻辑回归(logit)坐标刻度。这种坐标通常用在数据跨越多个数量级的情况(order of magnitude)。修改坐标轴刻度是非常容易的,如下所示:
plt.XScale('log')#修改为对数坐标
下面例子展示了四个具有相同数据的图形,但是他们的y坐标刻度是不同的:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import NullFormatter
np.random.seed(19680801)
y=np.random.normal(loc=0.5,scale=0.4,size=1000)
y = y[(y>0) & (y < 1)]
y.sort()
x=np.arange(len(y))
plt.figure(1)
plt.subplot(221)
plt.plot(x,y)
plt.yscale('linear')
plt.title('linear')
plt.grid(True)
plt.subplot(222)
plt.plot(x,y)
plt.yscale('log')
plt.title('log')
plt.grid(True)
plt.subplot(223)
plt.plot(x,y-y.mean())
plt.yscale('symlog',linthreshy=0.01)
plt.title('symlog')
plt.grid(True)
plt.subplot(224)
plt.plot(x,y)
plt.yscale('logit')
plt.title('logit')
plt.grid(True)
plt.gca().yaxis.set_minor_formatter(NullFormatter())
plt.subplots_adjust(top=0.92,bottom=0.08,left=0.10,right=0.95,hspace=0.25,wspace=0.35)
plt.show()
运行结果如下:
也可以添加你自己的坐标刻度。查看 Developer’s guide for creating scales and transformation 了解详细信息。