python_matplotlib改變橫坐標(biāo)和縱坐標(biāo)上的刻度(ticks)方式
用matplotlib畫二維圖像時(shí),默認(rèn)情況下的橫坐標(biāo)和縱坐標(biāo)顯示的值有時(shí)達(dá)不到自己的需求,需要借助xticks()和yticks()分別對(duì)橫坐標(biāo)x-axis和縱坐標(biāo)y-axis進(jìn)行設(shè)置。
import numpy as npimport matplotlib.pyplot as pltx = range(1,13,1)y = range(1,13,1)plt.plot(x,y)plt.show()
x坐標(biāo)和y坐標(biāo)都表示1到12的整數(shù),不進(jìn)行坐標(biāo)設(shè)置時(shí),執(zhí)行效果為:
此時(shí)的x軸和y軸都是只顯示偶數(shù),其它的奇數(shù)未顯示,這樣在展示實(shí)驗(yàn)效果或放入文章中都會(huì)影響其可讀性。
為了設(shè)置坐標(biāo)軸的值,增加其可讀性,有多種方法。這里介紹的是matplotlib的函數(shù)xticks()和yticks()。
參考文檔:xticks()函數(shù)介紹 yticks()函數(shù)介紹
xticks()中有3個(gè)參數(shù):
xticks(locs, [labels], **kwargs) # Set locations and labels
locs參數(shù)為數(shù)組參數(shù)(array_like, optional),表示x-axis的刻度線顯示標(biāo)注的地方,即ticks放置的地方,上述例子中,如果希望顯示1到12所有的整數(shù),就可以將locs參數(shù)設(shè)置為range(1,13,1),第二個(gè)參數(shù)也為數(shù)組參數(shù)(array_like, optional),可以不添加該參數(shù),表示在locs數(shù)組表示的位置添加的標(biāo)簽,labels不賦值,在這些位置添加的數(shù)值即為locs數(shù)組中的數(shù)。
如下圖
import numpy as npimport matplotlib.pyplot as pltx = range(1,13,1)y = range(1,13,1)plt.plot(x,y)plt.xticks(x)plt.show()
xticks()函數(shù)中,locs參數(shù)為數(shù)組x,即1到12所有的整數(shù), 即畫出的圖像會(huì)在這12個(gè)位置畫出ticks,即上圖中的刻度線。
當(dāng)賦予labels的值為空時(shí),則在locs決定的位置上雖然會(huì)畫出ticks,但不會(huì)顯示任何值。
import numpy as npimport matplotlib.pyplot as pltx = range(1,13,1)y = range(1,13,1)plt.plot(x,y)plt.xticks(x,())plt.show()
其效果為:
該例子中,會(huì)明顯看到locs和labels的關(guān)系,locs表示位置,labels決定這些位置上的標(biāo)簽,labels的默認(rèn)值為和locs相同。
所以,對(duì)于labels參數(shù),我們可以賦予其任意其它的值,如人名,月份等等。
import numpy as npimport matplotlib.pyplot as pltx = range(1,13,1)y = range(1,13,1)plt.plot(x,y)plt.xticks(x, (’Tom’,’Dick’,’Harry’,’Sally’,’Sue’,’Lily’,’Ava’,’Isla’,’Rose’,’Jack’,’Leo’,’Charlie’))plt.show()
在每個(gè)標(biāo)簽會(huì)依次顯示labels中的人名:
還可以顯示月份:
import numpy as npimport matplotlib.pyplot as pltimport calendarx = range(1,13,1)y = range(1,13,1)plt.plot(x,y)plt.xticks(x, calendar.month_name[1:13],color=’blue’,rotation=60)plt.show()
顯示效果為:
這里添加了 calendar 模塊,用于顯示月份的名稱。calendar.month_name[1:13]即1月份到12月份每個(gè)月份的名稱的數(shù)組。后面的參數(shù)color=’blue’表示將標(biāo)簽顏色置為藍(lán)色,rotation表示標(biāo)簽?zāi)鏁r(shí)針旋轉(zhuǎn)60度。
通過上個(gè)示例,可看出第3個(gè)參數(shù)關(guān)鍵字參數(shù)**kwargs用于控制labels,具體可通過Text屬性中的定義,添加到該參數(shù)中,關(guān)于其定義可參考在 Text 查詢。
另外,通過第1個(gè)參數(shù)locs可以看出,xticks()函數(shù)還可以用來設(shè)置使x軸上ticks隱藏,即將空數(shù)組賦予它,則沒有tick會(huì)顯示在x軸上,此處參考:x軸數(shù)值隱藏。
import numpy as npimport matplotlib.pyplot as pltimport calendarx = range(1,13,1)y = range(1,13,1)plt.plot(x,y)plt.xticks([])plt.show()
可看出x軸上沒有tick顯示:
同理,對(duì)于yticks()函數(shù)定義和xticks()函數(shù)定義完全相同。對(duì)于第一個(gè)例子,如果希望在y軸上的刻度線也顯示1到12所有的整數(shù),則將lens(1,13,1)賦予yticks()的locs參數(shù)即可:
import numpy as npimport matplotlib.pyplot as pltimport calendarx = range(1,13,1)y = range(1,13,1)plt.plot(x,y)plt.xticks(x)plt.yticks(y)plt.show()
其效果為:
綜上,可以設(shè)計(jì)一個(gè)x軸為月份,y為星期的圖像:
import numpy as npimport matplotlib.pyplot as pltimport calendarfrom datetime import *x = range(1,13,1)y = range(1,13,1)plt.plot(x,y)plt.xticks(x, calendar.month_name[1:13],color=’blue’,rotation=60)today = datetime(2018, 9, 10)a=[]for i in range(12): a.append(calendar.day_name[today.weekday()+(i%7)])plt.yticks(y,a,color=’red’)plt.show()
對(duì)應(yīng)一月份選擇星期一,二月份選擇星期二,往后依次類推,直至將12個(gè)月安排完。
以上這篇python_matplotlib改變橫坐標(biāo)和縱坐標(biāo)上的刻度(ticks)方式就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 基于 Python 實(shí)踐感知器分類算法2. Python如何批量生成和調(diào)用變量3. ASP.Net Core對(duì)USB攝像頭進(jìn)行截圖4. ajax動(dòng)態(tài)加載json數(shù)據(jù)并詳細(xì)解析5. Python 中如何使用 virtualenv 管理虛擬環(huán)境6. python利用opencv實(shí)現(xiàn)顏色檢測(cè)7. 通過CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫特效8. ASP.Net Core(C#)創(chuàng)建Web站點(diǎn)的實(shí)現(xiàn)9. ASP.NET MVC實(shí)現(xiàn)橫向展示購(gòu)物車10. windows服務(wù)器使用IIS時(shí)thinkphp搜索中文無效問題
