1. #!/usr/local/bin/python3
    2. import numpy as np
    3. import matplotlib.pyplot as plt
    4. import pandas as pd
    5. from matplotlib.ticker import MultipleLocator
    6. x = np.arange(-5,5,0.1)
    7. y = x*3
    8. fig1 = plt.figure(num=1,figsize=(8,5),dpi=80)#窗口,大小,分辨率
    9. xmajorLocator = MultipleLocator(2)#设置坐标轴刻度为2的倍数
    10. ymajorLocator = MultipleLocator(3)#设置坐标轴刻度为3的倍数
    11. for i in range(2):
    12. name = 'sub{0}'.format(i+1)
    13. name = plt.subplot(1,2,i+1)#创建12列子图,子图从左至右,从上之下分别为12,。。。
    14. name.set_title('title{0}'.format(i+1))#设置子图的title
    15. name.set_xlabel('x-name{0}'.format(i+1))#设置子图的x轴名称
    16. name.set_ylabel('y-name{0}'.format(i+1))#设置子图的y轴名称
    17. name.xaxis.set_major_locator(xmajorLocator)
    18. name.yaxis.set_major_locator(ymajorLocator)
    19. plt.plot(x,y,marker='o',color='b',label='data')#点图
    20. plt.plot(x,y,color='r',linestyle='-',label='line',alpha=0.5)#线图
    21. plt.legend(loc='upper left')#显示图列,位置为upper left
    22. plt.text(2,4,r'y=x')#指定位置写入文本
    23. plt.annotate('text',xy=(1,3),xytext=(3,1.5),arrowprops=dict(facecolor='black',shrink=0.01))#添加箭头标注
    24. name.xaxis.grid(True,which='major')#设置x轴网格线,用x轴的刻度
    25. name.yaxis.grid(True)#设置y轴网格线,默认为用y轴的刻度
    26. if i == 1:
    27. name.set_xticks([])#去除x坐标轴刻度
    28. name.set_yticks((-15,-10,-5,0,5,10,15))#设置y坐标轴刻度
    29. name.set_yticklabels(labels=['y1','y2','y3','y4','y5','y6','y7'],rotation=-30,fontsize='small')
    30. #设置刻度的文本,文本旋转,文本字体字号
    31. plt.savefig('{0}.png'.format(name),dpi=400)
    32. plt.show()