Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何关闭matlibplot轴的记号和标记?_Python_Matplotlib_Axes - Fatal编程技术网

Python 如何关闭matlibplot轴的记号和标记?

Python 如何关闭matlibplot轴的记号和标记?,python,matplotlib,axes,Python,Matplotlib,Axes,我想使用matlibplot轴绘制2个子图。因为这两个子地块具有相同的ylabel和ticks,所以我想关闭第二个子地块的ticks和marks。以下是我的短文: import matplotlib.pyplot as plt ax1=plt.axes([0.1,0.1,0.4,0.8]) ax1.plot(X1,Y1) ax2=plt.axes([0.5,0.1,0.4,0.8]) ax2.plot(X2,Y2) 顺便说一句,X轴标记重叠,不确定是否有整洁的解决方案。(解决方案可能是使每个子

我想使用matlibplot轴绘制2个子图。因为这两个子地块具有相同的ylabel和ticks,所以我想关闭第二个子地块的ticks和marks。以下是我的短文:

import matplotlib.pyplot as plt
ax1=plt.axes([0.1,0.1,0.4,0.8])
ax1.plot(X1,Y1)
ax2=plt.axes([0.5,0.1,0.4,0.8])
ax2.plot(X2,Y2)

顺便说一句,X轴标记重叠,不确定是否有整洁的解决方案。(解决方案可能是使每个子批次的最后一个标记不可见,但不确定如何)。谢谢

快速搜索一下,我找到了答案:

plt.setp(ax2.get_yticklabels(), visible=False)
ax2.yaxis.set_tick_params(size=0)
ax1.yaxis.tick_left()

一个稍微不同的解决方案可能是将标签实际设置为“”。以下内容将删除所有y标记和记号标记:

# This is from @pelson's answer
plt.setp(ax2.get_yticklabels(), visible=False)

# This actually hides the ticklines instead of setting their size to 0
# I can never get the size=0 setting to work, unsure why
plt.setp(ax2.get_yticklines(),visible=False)

# This hides the right side y-ticks on ax1, because I can never get tick_left() to work
# yticklines alternate sides, starting on the left and going from bottom to top
# thus, we must start with "1" for the index and select every other tickline
plt.setp(ax1.get_yticklines()[1::2],visible=False)
现在要去掉x轴的最后一个记号和标签

# I used a for loop only because it's shorter
for ax in [ax1, ax2]:
    plt.setp(ax.get_xticklabels()[-1], visible=False)
    plt.setp(ax.get_xticklines()[-2:], visible=False)