简体   繁体   English

如何在matplotlib中删除一个刻度的轴标签

[英]How to delete axis label for just one tick in matplotlib

I am plotting up several adjacent panels with matplotlib.我正在用 matplotlib 绘制几个相邻的面板。 I have the problem that some of the axis labels overlap where the panels meet.我有一些轴标签在面板相遇的地方重叠的问题。 A minimum working example and sample image follow.最小工作示例和示例图像如下。

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(4,8))
ax1 = fig.add_axes([.25,.5,.5,.25])
ax2 = fig.add_axes([.25,.25,.5,.25])

ax1.set_xticklabels([])

fig.savefig("temp.pdf")

在此处输入图片说明

As you can see in the image, the 0.0 of the top panel and the 1.0 of the bottom panel are in the same place.正如您在图像中看到的,顶部面板的 0.0 和底部面板的 1.0 在同一个位置。 I am trying to get the 1.0 of the bottom panel to not show up but still have the remaining labels on the axis show up.我试图让底部面板的 1.0 不显示,但仍然显示轴上的剩余标签。 Nothing I've tried has worked.我尝试过的一切都没有奏效。 Things I have tried:我尝试过的事情:

#This just doesn't do anything
from matplotlib.ticker import MaxNLocator
ax3.xaxis.set_major_locator(MaxNLocator(prune='upper'))

#This produces the image shown below
labels = [item for item in ax2.get_yticklabels()]
labels[-1].text = ''
ax2.set_yticklabels(labels) 

#This also produces the image shown below
labels = ax2.get_yticklabels()
labels[-1].set_text('')
ax2.set_yticklabels(labels) 

在此处输入图片说明

The image above is produced by the last two of the three blocks of code immediately before the image.上图是由紧接在图像之前的三个代码块中的最后两个生成的。 The strange y-axis labels occur whether or not the line labels[-1].text = '' is included.无论是否包含行labels[-1].text = ''都会出现奇怪的 y 轴标签。

You will want to grab all the yticklabels of the axes, and then set them using all but the last one.您将需要获取轴的所有 yticklabels,然后使用除最后一个以外的所有标签进行设置。 An important point here (especially with newer versions of matplotlib) is that you must display the figure and refresh the canvas before fetching the ytick labels so that their default position is already computed.这里的一个重点(尤其是使用较新版本的 matplotlib)是您必须获取 ytick 标签之前显示图形并刷新画布以便已经计算出它们的默认位置。

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(4,8))
ax1 = fig.add_axes([.25,.5,.5,.25])
ax2 = fig.add_axes([.25,.25,.5,.25])

ax1.set_xticklabels([])

# Important to render initial ytick labels
fig.show()
fig.canvas.draw()

# Remove the last ytick label
labels = [tick.get_text() for tick in ax2.get_yticklabels()]
ax2.set_yticklabels(labels[:-1])

# Refresh the canvas to reflect the change
fig.canvas.draw()

Note: In my previous code, I was using a fig.savefig() call before altering the ticks to debug things and this forced the rendering and generation of default ytick labels.注意:在我之前的代码中,我在更改刻度以调试事物之前使用了fig.savefig()调用,这会强制渲染和生成默认的 ytick 标签。

在此处输入图片说明

add this line : plt.setp(ax2.get_yticklabels()[-1], visible=False)添加这一行: plt.setp(ax2.get_yticklabels()[-1], visible=False)

it will make the uppermost tick label on the vertical axis of axis-2 invisible它将使轴 2 垂直轴上最上面的刻度标签不可见

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM