当前位置:网站首页>Matplotlib set axis scale interval

Matplotlib set axis scale interval

2022-06-22 20:00:00 Just one word

I wanted the horizontal axis to show 1-20 The integer of , Interval between scales 1, But the plot shows floating point numbers

plt.title('train loss')
plt.plot(np.arange(1,len(losses)+1), losses)
plt.plot(np.arange(1,len(eval_losses)+1), eval_losses)

 Insert picture description here
After checking the data , Find two ways :
The first is through MultipleLocator Class to achieve

from matplotlib.pyplot import MultipleLocator # Import this class , Set the axis spacing 
plt.title('train loss')

x_major_locator=MultipleLocator(1)
ax=plt.gca()
ax.xaxis.set_major_locator(x_major_locator)

plt.plot(np.arange(1,len(losses)+1), losses)
plt.plot(np.arange(1,len(eval_losses)+1), eval_losses)

 Insert picture description here
The second is to convert the horizontal axis coordinate range into a string

plt.title('train loss')
plt.plot(np.arange(1,len(losses)+1).astype(np.str), losses)
plt.plot(np.arange(1,len(eval_losses)+1), eval_losses)

 Insert picture description here

原网站

版权声明
本文为[Just one word]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221828272762.html