import matplotlib.pyplot as plt import numpy as np x = np.linspace(-3,3,50)#从-3到3设置50个点 #设置两个函数 y1 = 2 * x + 1 y2 = x ** 2 plt.figure() plt.plot(x,y2) plt.plot(x,y1,color = 'red',linewidth = 1.0,linestyle = '--') plt.xlim((-1,2))#设置坐标轴 plt.ylim((-2,3)) plt.xlabel('I am x')#解释坐标轴信息 plt.ylabel('I am y') new_ticks = np.linspace(-1,2,5) print(new_ticks) plt.xticks(new_ticks)#改变坐标轴的显示 # plt.yticks([-2,-1.8,-1,1.22,3] # ,['really bad','bad','normal','good','really good']) plt.yticks([-2,-1.8,-1,1.22,3] ,[r'$really bad$','$bad$','$normal$','$good$','$really good$'])#设置斜体字体 #方法:加表示方式如数学的 阿尔法 alpha #gca = 'get current axis'获取当前坐标轴 ax = plt.gca() ax.spines['right'].set_color('none')#将右坐标轴设置为空 ax.spines['top'].set_color('none') # ax.xaxis.set_ticks_position('bottom')#将下坐标轴设置为x轴 # ax.xaxis.set_ticks_position('left') ax.spines['bottom'].set_position(('data',0))#设置起点,也可以用axes参数为百分比 ax.spines['left'].set_position(('data',0)) print('123') plt.show()