【Matplotlib】設置刻度(1)


刻度設置

參考文檔:

以xticks為例:

matplotlib.pyplot.xticks(*args, **kwargs)

獲取或者設置當前刻度位置和文本的 x-limits:

# return locs, labels where locs is an array of tick locations and
# labels is an array of tick labels.
locs, labels = xticks()

# set the locations of the xticks
xticks( arange(6) )

# set the locations and labels of the xticks
xticks( arange(5), ('Tom', 'Dick', 'Harry', 'Sally', 'Sue') )

關鍵字 args ,如果有其他的參數則是 Text 屬性。例如,旋轉長的文本標注。

xticks( arange(12), calendar.month_name[1:13], rotation=17 )

參考文檔

Axis containers

matplotlib.axis.Axis對象負責刻度線、格網線、刻度標注和坐標軸標注的繪制工作。你可以設置y軸的左右刻度或者x軸的上下刻度。 Axis 也存儲了用於自動調整,移動和放縮的數據和視覺間隔;同時 LocatorFormatter 對象控制着刻度的位置以及以怎樣的字符串呈現。

每一個 Axis 對象包含一個 label 屬性以及主刻度和小刻度的列表。刻度是 XTickYTick 對象,其包含着實際線和文本元素,分別代表刻度和注釋。因為刻度是根據需要動態創建的,你應該通過獲取方法 get_major_ticks()get_minor_ticks()以獲取主刻度和小刻度的列表。盡管刻度包含了所有的元素,並且將會在下面代碼中涵蓋,Axis 方法包含了獲取方法以返回刻度線、刻度標注和刻度位置等等:

In [285]: axis = ax.xaxis

In [286]: axis.get_ticklocs()
Out[286]: array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9.])

In [287]: axis.get_ticklabels()
Out[287]: <a list of 10 Text major ticklabel objects>

# note there are twice as many ticklines as labels because by
#  default there are tick lines at the top and bottom but only tick
#  labels below the xaxis; this can be customized
In [288]: axis.get_ticklines()
Out[288]: <a list of 20 Line2D ticklines objects>

# by default you get the major ticks back
In [291]: axis.get_ticklines()
Out[291]: <a list of 20 Line2D ticklines objects>

# but you can also ask for the minor ticks
In [292]: axis.get_ticklines(minor=True)
Out[292]: <a list of 0 Line2D ticklines objects>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM