matplotlib刻度设置数量

问题介绍

根据循环代码生成了四张图,因为有可能每次数据会有区别,因此需要y轴刻度自适应为5-6个,此时传统的xticks方法不再适用。
matplotlib刻度设置数量_第1张图片

解决方法

参考:
https://www.delftstack.com/zh/howto/matplotlib/matplotlib-set-number-of-ticks/
https://stackoverflow.com/questions/62785542/module-matplotlib-pyplot-has-no-attribute-yaxis

只需要添加2行代码

from matplotlib.ticker import MaxNLocator
plt.gca().yaxis.set_major_locator(MaxNLocator(6)) 

matplotlib刻度设置数量_第2张图片

你可能感兴趣的:(python,matplotlib,python,机器学习)