设置坐标轴刻度范围
matplotlib.pyplot.xlim() : 设置/获取当前Axes对象的x轴范围设定
xmin, xmax = xlim() # return the current xlim
xlim((xmin, xmax)) # set the xlim to xmin, xmax
xlim(xmin, xmax) # set the xlim to xmin, xmax
matplotlib.pyplot.ylim() : 设置/获取当前Axes对象的y轴范围设定
xmin, xmax = plt.xlim()
print(xmin, xmax)
plt.xlim(0, 0.5)
设置刻度线和刻度标签
XTick/YTick : 对横轴/纵轴的刻度标记进行各种格式设定。
刻度线可不等距设定。
可使用自定义标签替换默认的刻度数值进行显示。
完整参考信息:https://matplotlib.org/api/axis_api.html
设置刻度格式
matplotlib.pyplot.xticks/yticks() : 设置/获取当前Axes对象的数轴刻度/标签设定
locs, labels = xticks() # Get locations and labels
xticks(locs, [labels], **kwargs) # Set locations and labels
plt.xticks(rotation = 'vertical') # 使用了matplotlib.text类的方法
plt.yticks([-2, -1.5, -1, 1.5, 3],
['really bad', 'bad', 'normal', 'good', 'really good'],
color = 'green')
# 使用转义字符串进行格式化
# 可进一步使用LeTex转义字符(注意和中文不兼容)
plt.xticks(rotation = 'vertical') # 使用了matplotlib.text类的方法
plt.yticks([-2, -1.5, -1, 1.5, 3],
['$really\ bad$', '$bad$', r'$normal\ \alpha\ level$',
'$good$', '$really\ good$'],
color = 'green')
设置刻度显示方向
ax = plt.gca()
# ACCEPTS: [ 'top' | 'bottom' | 'both' | 'default' | 'none' ]
ax.xaxis.set_ticks_position('top') # 选择下方的边界用于显示刻度
ax.yaxis.set_ticks_position('right') # 选择右侧的边界用于显示刻度