matplotlib绘图如何设置坐标轴刻度⼤⼩和刻度总结matplotlib绘图如何设置坐标轴刻度⼤⼩和刻度。
上代码:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24from pylab import*
from matplotlib.ticker import MultipleLocator, FormatStrFormatter xmajorLocator  =MultipleLocator(20) #将x主刻度标签设置为20的倍数xmajorFormatter =FormatStrFormatter('%1.1f') #设置x轴标签⽂本的格式xminorLocator  =MultipleLocator(5) #将x轴次刻度标签设置为5的倍数ymajorLocator  =MultipleLocator(0.5) #将y轴主刻度标签设置为0.5的倍数ymajorFormatter =FormatStrFormatter('%1.1f') #设置y轴标签⽂本的格式yminorLocator  =MultipleLocator(0.1) #将此y轴次刻度标签设置为0.1的倍数t =arange(0.0, 100.0, 1)
s =sin(0.1*pi*t)*exp(-t*0.01)
ax =subplot(111) #注意:⼀般都在ax中设置,不再plot中设置
plot(t,s,'--b*')matplotlib中subplot
#设置主刻度标签的位置,标签⽂本的格式
ax.xaxis.set_major_locator(xmajorLocator)
ax.xaxis.set_major_formatter(xmajorFormatter)
ax.yaxis.set_major_locator(ymajorLocator)
ax.yaxis.set_major_formatter(ymajorFormatter)
#显⽰次刻度标签的位置,没有标签⽂本
ax.xaxis.set_minor_locator(xminorLocator)
ax.yaxis.set_minor_locator(yminorLocator)
id(True, which='major') #x坐标轴的⽹格使⽤主刻度
id(True, which='minor') #y坐标轴的⽹格使⽤次刻度
show()