import numpy as np 
import matplotlib.pyplot as plt
from matplotlib.pyplot import MultipleLocator, FormatStrFormatter
#MultipleLocator    用于设置刻度间隔
#FormatStrFormatter 用于设置刻度格式
plt.rcParams['font.family'] = ['Microsoft YaHei']
plt.rcParams['axes.unicode_minus'] = False

#setting
xmajorLocator  = MultipleLocator(10)  # x轴刻度间隔 10
ymajorLocator  = MultipleLocator(5)    # y轴刻度间隔 5
ymajorFormatter = FormatStrFormatter('%1.2f') # y轴刻度格式为两位小数

#
fig, ax = plt.subplots(figsize=(25, 25))
colors = ['r', 'g', 'b']
for idx in range(3):
    x = np.arange(100)
    data = x*(idx+1) + np.random.randint(10)
    color = colors[idx]
    ax.plot(x, data, color, label=color.upper(), linewidth=2)
#
ax.xaxis.set_major_locator(xmajorLocator) 
ax.yaxis.set_major_locator(ymajorLocator) 
ax.yaxis.set_major_formatter(ymajorFormatter) 
legend = ax.legend(loc='best', shadow=False, fontsize='x-large')
plt.xlabel('M', fontsize=12)
plt.ylabel('N', fontsize=12)
plt.xlim(0, 100)  #x轴最大100, 最小0
plt.ylim(-5, 300) #y轴最大300, 最小-5
plt.show()
Last modification:February 11th, 2022 at 08:09 pm