Python設(shè)置matplotlib.plot的坐標(biāo)軸刻度間隔以及刻度范圍

import matplotlib.pyplot as plt

x_values=list(range(11))? ?#x軸的數(shù)字是0到10這11個整數(shù)

y_values=[x**2forx inx_values]? ?#y軸的數(shù)字是x軸數(shù)字的平方

plt.plot(x_values,y_values,c='green')??#用plot函數(shù)繪制折線圖,線條顏色設(shè)置為綠色

plt.title('Squares',fontsize=24)? ?#設(shè)置圖表標(biāo)題和標(biāo)題字號

plt.tick_params(axis='both',which='major',labelsize=14)?#設(shè)置刻度的字號

plt.xlabel('Numbers',fontsize=14)??#設(shè)置x軸標(biāo)簽及其字號

plt.ylabel('Squares',fontsize=14)??#設(shè)置y軸標(biāo)簽及其字號

plt.show()





import matplotlib.pyplot as plt

from matplotlib.pyplot import MultipleLocator

#從pyplot導(dǎo)入MultipleLocator類,這個類用于設(shè)置刻度間隔


x_values=list(range(11))

y_values=[x**2forx inx_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軸的刻度間隔設(shè)置為1,并存在變量里

y_major_locator=MultipleLocator(10)?#把y軸的刻度間隔設(shè)置為10,并存在變量里

ax=plt.gca()?#ax為兩條坐標(biāo)軸的實例

ax.xaxis.set_major_locator(x_major_locator)?#把x軸的主刻度設(shè)置為1的倍數(shù)

ax.yaxis.set_major_locator(y_major_locator)?#把y軸的主刻度設(shè)置為10的倍數(shù)

plt.xlim(-0.5,11)??#把x軸的刻度范圍設(shè)置為-0.5到11,因為0.5不滿一個刻度間隔,所以數(shù)字不會顯示出來,但是能看到一點空白

plt.ylim(-5,110)?#把y軸的刻度范圍設(shè)置為-5到110,同理,-5不會標(biāo)出來,但是能看到一點空白

plt.show()

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容