簡體   English   中英

在matplotlib中更改刻度線的capstyle

[英]Changing the capstyle of ticks in matplotlib

我想使用圓形capstyle標記。 我嘗試了以下操作,但是不起作用。

import matplotlib.pyplot as plt   
# better tick visiblity to check the capstyle
plt.rcParams.update({
           'figure.dpi': 150,
          u'xtick.major.size': 5.0,
          u'xtick.major.width': 2,
          })

fig, ax = plt.subplots()
ax.plot([1, 2, 3, 4])

tl = ax.xaxis.get_ticklines()    
for i in tl:
    i.set_solid_capstyle('round')
plt.show()

因此,這樣做不起作用的原因是,刻度線是通過將刻度線繪制為標記而實現的單點線。

In [127]: [i.get_linestyle() for i in ax.xaxis.get_ticklines()]
Out[127]: 
['None',
 'None',
 'None',
 'None',
 'None',
 'None',
 'None',
 'None',
 'None',
 'None',
 'None',
 'None',
 'None',
 'None']

In [128]: [i.get_marker() for i in ax.xaxis.get_ticklines()]
Out[128]: [2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3]

請參閱文檔以了解int-> shape之間的映射,這是交替的“ tickup”和“ tickdown”。

這確實可以讓您做半可愛的事情,例如

[i.set_marker('o') for i in ax.xaxis.get_ticklines()]

這使 圈勾


如果使用此功能,則上游中斷時將是您的問題

要獲得所需的內容,您可以深入了解並在刻度線上設置capstyle:

for i in ax.xaxis.get_ticklines():
    # reach in and poke the guts 
    # USE AT YOUR OWN RISK
    i._marker._capstyle = 'round' 
    # this is not officially supported

請注意,它使用的不是1個私有屬性,而是2個私有屬性,並且被來年的traittrait重構破壞(針對v2.1)。 還將在_recache調用中將其重新設置為'butt'的硬編碼值(我不記得確切的什么時候被調用)。

我不認為您真的想這樣做:

圓帽刻度

當它使刻度線的兩端都變圓時,不僅是指尖的一面(而且可能是較小的刻度線,圓角的“向外”面將被書脊隱藏)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM