从matplotlib`ax`obj提取记号参数

2024-04-25 14:29:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望能够使用ax.tick_params来识别刻度线是否设置为'off'。有没有一种方法可以检查ax对象,看看它们是否已关闭?在

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.spines['top'].set_visible(False)
ax.spines['bottom'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['right'].set_visible(False)


ax.tick_params(
    axis='both',  
    which='both',      

    bottom='off',     
    top='off',         
    left='off',
    right='off') 

# This is essentially what I want to be able to do
assert ax.xticks == 'off'

Tags: torightfalsetoppltparamsaxleft
1条回答
网友
1楼 · 发布于 2024-04-25 14:29:32

要迭代每个轴的刻度,可以使用以下代码

print "Major ticks of y axis"                                              
for tick in ax.yaxis.get_major_ticks():
    print tick.tick1On, tick.tick2On, tick.gridOn                                        
print "Minor ticks of y axis"
for tick in ax.yaxis.get_minor_ticks():
    print tick.tick1On, tick.tick2On, tick.gridOn   

要在x轴的刻度上导航,例如,只需替换

^{pr2}$

通过

ax.xaxis.get_major_ticks()

关于Tick对象的公共可访问属性,请阅读此处:http://matplotlib.org/api/axis_api.html#matplotlib.axis.Tick

相关问题 更多 >