1、用默認設置繪製折線圖html
import matplotlib.pyplot as plt函數
x_values=list(range(11))spa
#x軸的數字是0到10這11個整數.net
y_values=[x**2 for x in x_values]htm
#y軸的數字是x軸數字的平方ip
plt.plot(x_values,y_values,c='green')it
#用plot函數繪製折線圖,線條顏色設置爲綠色class
plt.title('Squares',fontsize=24)import
#設置圖表標題和標題字號變量
plt.tick_params(axis='both',which='major',labelsize=14)
#設置刻度的字號
plt.xlabel('Numbers',fontsize=14)
#設置x軸標籤及其字號
plt.ylabel('Squares',fontsize=14)
#設置y軸標籤及其字號
plt.show()
#顯示圖表
製做出圖表
咱們但願x軸的刻度是0,1,2,3,4……,y軸的刻度是0,10,20,30……,而且但願兩個座標軸的範圍都能再大一點,因此咱們須要手動設置。
2、手動設置座標軸刻度間隔以及刻度範圍
import matplotlib.pyplot as plt
from matplotlib.pyplot import MultipleLocator
#從pyplot導入MultipleLocator類,這個類用於設置刻度間隔
x_values=list(range(11))
y_values=[x**2 for x in x_values]
plt.plot(x_values,y_values,c='green')
plt.title('Squares',fontsize=24)
plt.tick_params(axis='both',which='major',labelsize=14)
plt.xlabel('Numbers',fontsize=14)
plt.ylabel('Squares',fontsize=14)
x_major_locator=MultipleLocator(1)
#把x軸的刻度間隔設置爲1,並存在變量裏
y_major_locator=MultipleLocator(10)
#把y軸的刻度間隔設置爲10,並存在變量裏
ax=plt.gca()無錫婦科醫院哪家好 http://wapyyk.39.net/wx/zonghe/fc96e.html/
#ax爲兩條座標軸的實例
ax.xaxis.set_major_locator(x_major_locator)
#把x軸的主刻度設置爲1的倍數
ax.yaxis.set_major_locator(y_major_locator)
#把y軸的主刻度設置爲10的倍數
plt.xlim(-0.5,11)
#把x軸的刻度範圍設置爲-0.5到11,由於0.5不滿一個刻度間隔,因此數字不會顯示出來,可是能看到一點空白
plt.ylim(-5,110)
#把y軸的刻度範圍設置爲-5到110,同理,-5不會標出來,可是能看到一點空白
plt.show()
繪製結果