```python import matplotlib.pyplot as plt import numpy as np # 定義數據 x = np.linspace(-3, 3, 50) y1 = 2*x + 1 y2 = x**2 # 定義figure plt.figure() # 繪圖(x,y2) plt.plot(x, y2) # 繪圖(x,y1) plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--') # 設置坐標返回 plt.xlim((-1, 2)) plt.ylim((-2, 3)) # 設置x軸刻度 這里分為5個小刻度 new_ticks = np.linspace(-1, 2, 5) print(new_ticks) plt.xticks(new_ticks) # 設置y軸刻度,這里將刻度以自定義的數字表示出來,比如-2顯示為really bad # 使用$來更好的匹配文本以及字體,$\pi$則會顯示Pi plt.yticks([-2, -1.8, -1, 1.22, 3], [r'$really\ bad$', r'$bad$', r'$normal$', r'$good$', r'$really\ good$']) # 得到坐標軸信息 ax = plt.gca() # .spines設置邊框;set_color設置顏色 # 將上方和右邊的坐標線取消 ax.spines['right'].set_color('none') ax.spines['top'].set_color('none') # 設置x軸刻度位置,這里刻度在下方, # 具體有[ 'top' | 'bottom' | 'both' | 'default' | 'none' ] ax.xaxis.set_ticks_position('bottom') # 設置邊框位置,下方的邊框位於y=0處, # 具體有'outward' | 'axes' | 'data' # axes: percentage of y axis # data: depend on y data ax.spines['bottom'].set_position(('data', 0)) # y軸刻度在左側 # 具體有[ 'left' | 'right' | 'both' | 'default' | 'none' ] ax.yaxis.set_ticks_position('left') # 左側邊框在x=0處 ax.spines['left'].set_position(('data', 0)) plt.show() ```